Advertisement
ArcaniSGK507

Untitled

Mar 27th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services\Email;
  4.  
  5. use PHPMailer\PHPMailer\PHPMailer;
  6. use PHPMailer\PHPMailer\Exception as PHPMailerException;
  7.  
  8. /**
  9. * Email Security Manager
  10. *
  11. * Handles security aspects of email sending like headers and authentication.
  12. *
  13. * @package App\Services\Email
  14. */
  15. class EmailSecurityManager
  16. {
  17. /**
  18. * Add security headers to email to prevent spoofing
  19. *
  20. * @param PHPMailer $mailer PHPMailer instance
  21. * @return void
  22. * @throws PHPMailerException
  23. */
  24. public function addSecurityHeaders(PHPMailer $mailer): void
  25. {
  26. $mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
  27. $mailer->SMTPOptions = [
  28. 'ssl' => [
  29. 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT,
  30. 'verify_peer' => true,
  31. 'verify_peer_name' => true,
  32. 'allow_self_signed' => false,
  33. ],
  34. ];
  35.  
  36. // Add DKIM signature if keys are available
  37. if (defined('MAIL_DKIM_DOMAIN') && defined('MAIL_DKIM_PRIVATE') && defined('MAIL_DKIM_SELECTOR')) {
  38. $mailer->DKIM_domain = MAIL_DKIM_DOMAIN;
  39. $mailer->DKIM_private = MAIL_DKIM_PRIVATE;
  40. $mailer->DKIM_selector = MAIL_DKIM_SELECTOR;
  41. $mailer->DKIM_identity = $mailer->From;
  42. }
  43.  
  44. // Add Message-ID header for traceability
  45. $mailer->MessageID = $this->generateMessageId();
  46.  
  47. // Add X-Mailer header to identify the sending system
  48. $mailer->XMailer = APP_NAME . ' Mailer';
  49.  
  50. // Add List-Unsubscribe header
  51. $unsubscribeEmail = 'unsubscribe@' . parse_url(APP_URL, PHP_URL_HOST);
  52. $unsubscribeUrl = APP_URL . '/unsubscribe?email=' . urlencode($mailer->getToAddresses()[0][0] ?? '');
  53. $mailer->addCustomHeader('List-Unsubscribe', "<mailto:$unsubscribeEmail>, <$unsubscribeUrl>");
  54. $mailer->addCustomHeader('List-Unsubscribe-Post', 'List-Unsubscribe=One-Click');
  55. $mailer->addCustomHeader('Precedence', 'bulk');
  56. $mailer->addCustomHeader('X-Auto-Response-Suppress', 'OOF, AutoReply');
  57. }
  58.  
  59. /**
  60. * Generate a unique Message-ID for email tracking
  61. *
  62. * @return string Message ID
  63. */
  64. private function generateMessageId(): string
  65. {
  66. $domain = parse_url(APP_URL, PHP_URL_HOST) ?: 'uknp.dock';
  67. return '<' . uniqid(mt_rand(), true) . '@' . $domain . '>';
  68. }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement