Common
1 year ago
ErrorMappers
1 year ago
AmazonSES.php
1 year ago
MailPoet.php
2 years ago
MailerMethod.php
3 years ago
PHPMail.php
4 years ago
PHPMailerMethod.php
1 year ago
SMTP.php
3 weeks ago
SendGrid.php
1 year ago
index.php
3 years ago
PHPMailerMethod.php
127 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Mailer\Methods; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Mailer\Mailer; |
| 9 | use MailPoet\Mailer\Methods\Common\BlacklistCheck; |
| 10 | use MailPoet\Mailer\WordPress\PHPMailerLoader; |
| 11 | use MailPoet\Util\Url; |
| 12 | use PHPMailer\PHPMailer\PHPMailer; |
| 13 | |
| 14 | PHPMailerLoader::load(); |
| 15 | |
| 16 | abstract class PHPMailerMethod implements MailerMethod { |
| 17 | /** @var string[] */ |
| 18 | public $sender; |
| 19 | /** @var string[] */ |
| 20 | public $replyTo; |
| 21 | /** @var string */ |
| 22 | public $returnPath; |
| 23 | /** @var PHPMailer */ |
| 24 | public $mailer; |
| 25 | |
| 26 | protected $errorMapper; |
| 27 | |
| 28 | /** @var Url */ |
| 29 | protected $urlUtils; |
| 30 | |
| 31 | /** @var BlacklistCheck */ |
| 32 | protected $blacklist; |
| 33 | |
| 34 | public function __construct( |
| 35 | $sender, |
| 36 | $replyTo, |
| 37 | $returnPath, |
| 38 | $errorMapper, |
| 39 | Url $urlUtils |
| 40 | ) { |
| 41 | $this->sender = $sender; |
| 42 | $this->replyTo = $replyTo; |
| 43 | $this->returnPath = $returnPath; |
| 44 | $this->mailer = $this->buildMailer(); |
| 45 | $this->errorMapper = $errorMapper; |
| 46 | $this->urlUtils = $urlUtils; |
| 47 | $this->blacklist = new BlacklistCheck(); |
| 48 | } |
| 49 | |
| 50 | public function send($newsletter, $subscriber, $extraParams = []): array { |
| 51 | if ($this->blacklist->isBlacklisted($subscriber)) { |
| 52 | $error = $this->errorMapper->getBlacklistError($subscriber); |
| 53 | return Mailer::formatMailerErrorResult($error); |
| 54 | } |
| 55 | try { |
| 56 | $mailer = $this->configureMailerWithMessage($newsletter, $subscriber, $extraParams); |
| 57 | $result = $mailer->send(); |
| 58 | } catch (\Exception $e) { |
| 59 | return Mailer::formatMailerErrorResult($this->errorMapper->getErrorFromException($e, $subscriber)); |
| 60 | } |
| 61 | if ($result === true) { |
| 62 | return Mailer::formatMailerSendSuccessResult(); |
| 63 | } else { |
| 64 | $error = $this->errorMapper->getErrorForSubscriber($subscriber); |
| 65 | return Mailer::formatMailerErrorResult($error); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | abstract public function buildMailer(): PHPMailer; |
| 70 | |
| 71 | public function configureMailerWithMessage($newsletter, $subscriber, $extraParams = []) { |
| 72 | $mailer = $this->mailer; |
| 73 | $mailer->clearAddresses(); |
| 74 | $mailer->clearCustomHeaders(); |
| 75 | $mailer->isHTML(!empty($newsletter['body']['html'])); |
| 76 | $mailer->CharSet = 'UTF-8'; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 77 | $mailer->setFrom($this->sender['from_email'], $this->sender['from_name'], false); |
| 78 | $mailer->addReplyTo($this->replyTo['reply_to_email'], $this->replyTo['reply_to_name']); |
| 79 | $subscriber = $this->processSubscriber($subscriber); |
| 80 | $mailer->addAddress($subscriber['email'], $subscriber['name']); |
| 81 | $mailer->Subject = (!empty($newsletter['subject'])) ? $newsletter['subject'] : ''; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 82 | $mailer->Body = (!empty($newsletter['body']['html'])) ? $newsletter['body']['html'] : $newsletter['body']['text']; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 83 | if ($mailer->ContentType !== 'text/plain') { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 84 | $mailer->AltBody = (!empty($newsletter['body']['text'])) ? $newsletter['body']['text'] : ''; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 85 | } |
| 86 | $mailer->Sender = $this->returnPath; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 87 | |
| 88 | // unsubscribe header |
| 89 | $unsubscribeUrl = $extraParams['unsubscribe_url'] ?? null; |
| 90 | $oneClickUnsubscribeUrl = $extraParams['one_click_unsubscribe'] ?? null; |
| 91 | if ($unsubscribeUrl) { |
| 92 | $isHttps = $this->urlUtils->isUsingHttps($unsubscribeUrl); |
| 93 | $url = $isHttps && $oneClickUnsubscribeUrl ? $oneClickUnsubscribeUrl : $unsubscribeUrl; |
| 94 | if ($isHttps) { |
| 95 | $mailer->addCustomHeader('List-Unsubscribe-Post', 'List-Unsubscribe=One-Click'); |
| 96 | } |
| 97 | $mailer->addCustomHeader('List-Unsubscribe', '<' . $url . '>'); |
| 98 | } |
| 99 | |
| 100 | // Enforce base64 encoding when lines are too long, otherwise quoted-printable encoding |
| 101 | // is automatically used which can occasionally break the email body. |
| 102 | // Explanation: |
| 103 | // The bug occurs on Unix systems where mail() function passes email to a variation of |
| 104 | // sendmail command which expects only NL as line endings (POSIX). Since quoted-printable |
| 105 | // requires CRLF some of those commands convert LF to CRLF which can break the email body |
| 106 | // because it already (correctly) uses CRLF. Such CRLF then (wrongly) becomes CRCRLF. |
| 107 | if (PHPMailer::hasLineLongerThanMax($mailer->Body)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 108 | $mailer->Encoding = 'base64'; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 109 | } |
| 110 | |
| 111 | return $mailer; |
| 112 | } |
| 113 | |
| 114 | public function processSubscriber($subscriber) { |
| 115 | preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriberData); |
| 116 | if (!isset($subscriberData['email'])) { |
| 117 | $subscriberData = [ |
| 118 | 'email' => $subscriber, |
| 119 | ]; |
| 120 | } |
| 121 | return [ |
| 122 | 'email' => $subscriberData['email'], |
| 123 | 'name' => (isset($subscriberData['name'])) ? $subscriberData['name'] : '', |
| 124 | ]; |
| 125 | } |
| 126 | } |
| 127 |