Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services\Email;
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\Exception as PHPMailerException;
- /**
- * Email Security Manager
- *
- * Handles security aspects of email sending like headers and authentication.
- *
- * @package App\Services\Email
- */
- class EmailSecurityManager
- {
- /**
- * Add security headers to email to prevent spoofing
- *
- * @param PHPMailer $mailer PHPMailer instance
- * @return void
- * @throws PHPMailerException
- */
- public function addSecurityHeaders(PHPMailer $mailer): void
- {
- $mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
- $mailer->SMTPOptions = [
- 'ssl' => [
- 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT,
- 'verify_peer' => true,
- 'verify_peer_name' => true,
- 'allow_self_signed' => false,
- ],
- ];
- // Add DKIM signature if keys are available
- if (defined('MAIL_DKIM_DOMAIN') && defined('MAIL_DKIM_PRIVATE') && defined('MAIL_DKIM_SELECTOR')) {
- $mailer->DKIM_domain = MAIL_DKIM_DOMAIN;
- $mailer->DKIM_private = MAIL_DKIM_PRIVATE;
- $mailer->DKIM_selector = MAIL_DKIM_SELECTOR;
- $mailer->DKIM_identity = $mailer->From;
- }
- // Add Message-ID header for traceability
- $mailer->MessageID = $this->generateMessageId();
- // Add X-Mailer header to identify the sending system
- $mailer->XMailer = APP_NAME . ' Mailer';
- // Add List-Unsubscribe header
- $unsubscribeEmail = 'unsubscribe@' . parse_url(APP_URL, PHP_URL_HOST);
- $unsubscribeUrl = APP_URL . '/unsubscribe?email=' . urlencode($mailer->getToAddresses()[0][0] ?? '');
- $mailer->addCustomHeader('List-Unsubscribe', "<mailto:$unsubscribeEmail>, <$unsubscribeUrl>");
- $mailer->addCustomHeader('List-Unsubscribe-Post', 'List-Unsubscribe=One-Click');
- $mailer->addCustomHeader('Precedence', 'bulk');
- $mailer->addCustomHeader('X-Auto-Response-Suppress', 'OOF, AutoReply');
- }
- /**
- * Generate a unique Message-ID for email tracking
- *
- * @return string Message ID
- */
- private function generateMessageId(): string
- {
- $domain = parse_url(APP_URL, PHP_URL_HOST) ?: 'uknp.dock';
- return '<' . uniqid(mt_rand(), true) . '@' . $domain . '>';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement