PHPMailerLoader.php
4 years ago
WordPressMailer.php
8 months ago
WordpressMailerReplacer.php
3 years ago
index.php
3 years ago
WordPressMailer.php
131 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Mailer\WordPress; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\Html2Text; |
| 9 | use MailPoet\Mailer\MailerFactory; |
| 10 | use MailPoet\Mailer\MetaInfo; |
| 11 | use MailPoet\Subscribers\SubscribersRepository; |
| 12 | use PHPMailer\PHPMailer\Exception as PHPMailerException; |
| 13 | use PHPMailer\PHPMailer\PHPMailer; |
| 14 | |
| 15 | PHPMailerLoader::load(); |
| 16 | |
| 17 | class WordPressMailer extends PHPMailer { |
| 18 | /** @var MailerFactory */ |
| 19 | private $mailerFactory; |
| 20 | |
| 21 | /** @var MetaInfo */ |
| 22 | private $mailerMetaInfo; |
| 23 | |
| 24 | /** @var SubscribersRepository */ |
| 25 | private $subscribersRepository; |
| 26 | |
| 27 | public function __construct( |
| 28 | MailerFactory $mailerFactory, |
| 29 | MetaInfo $mailerMetaInfo, |
| 30 | SubscribersRepository $subscribersRepository |
| 31 | ) { |
| 32 | parent::__construct(true); |
| 33 | $this->mailerFactory = $mailerFactory; |
| 34 | $this->mailerMetaInfo = $mailerMetaInfo; |
| 35 | $this->subscribersRepository = $subscribersRepository; |
| 36 | } |
| 37 | |
| 38 | public function send() { |
| 39 | // We need this so that the PHPMailer class will correctly prepare all the headers. |
| 40 | $originalMailer = $this->Mailer; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 41 | $this->Mailer = 'mail'; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 42 | |
| 43 | // Prepare everything (including the message) for sending. |
| 44 | $this->preSend(); |
| 45 | |
| 46 | $email = $this->getEmail(); |
| 47 | $address = $this->formatAddress($this->getToAddresses()); |
| 48 | $subscriber = $this->subscribersRepository->findOneBy(['email' => $address]); |
| 49 | $extraParams = [ |
| 50 | 'meta' => $this->mailerMetaInfo->getWordPressTransactionalMetaInfo($subscriber), |
| 51 | ]; |
| 52 | |
| 53 | try { |
| 54 | // we need to build fresh mailer for every single WP e-mail to make sure reply-to is set |
| 55 | $replyTo = $this->getReplyToAddress(); |
| 56 | $mailer = $this->mailerFactory->buildMailer(null, null, $replyTo); |
| 57 | $result = $mailer->send($email, $address, $extraParams); |
| 58 | if (!$result['response']) { |
| 59 | throw new \Exception($result['error']->getMessage()); |
| 60 | } |
| 61 | } catch (\Exception $ePrimary) { |
| 62 | // In case the sending using MailPoet's mailer fails continue with sending using original parent PHPMailer::sent method. |
| 63 | // But if anything fails we still want tho throw the error from the primary MailPoet mailer. |
| 64 | try { |
| 65 | // Restore original settings for mailer. Some sites may use SMTP and we needed to reset it to mail |
| 66 | $this->Mailer = $originalMailer; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 67 | return parent::send(); |
| 68 | } catch (\Exception $eFallback) { |
| 69 | throw new PHPMailerException($ePrimary->getMessage(), $ePrimary->getCode(), $ePrimary); |
| 70 | } |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | private function getEmail() { |
| 76 | // phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 77 | $email = [ |
| 78 | 'subject' => $this->Subject, |
| 79 | 'body' => [], |
| 80 | ]; |
| 81 | |
| 82 | if (strpos($this->ContentType, 'text/plain') === 0) { |
| 83 | $email['body']['text'] = $this->Body; |
| 84 | } elseif (strpos($this->ContentType, 'text/html') === 0) { |
| 85 | // @phpstan-ignore-next-line mb_convert_encoding will not return false, when we use mb_list_encodings(). It contains special encoding 'pass' ensuring it will always succeed. |
| 86 | $text = @Html2Text::convert(strtolower($this->CharSet) === 'utf-8' ? $this->Body : mb_convert_encoding($this->Body, 'UTF-8', mb_list_encodings())); |
| 87 | $email['body']['text'] = $text; |
| 88 | $email['body']['html'] = $this->Body; |
| 89 | } elseif (strpos($this->ContentType, 'multipart/alternative') === 0) { |
| 90 | $email['body']['text'] = $this->AltBody; |
| 91 | $email['body']['html'] = $this->Body; |
| 92 | } else { |
| 93 | throw new PHPMailerException('Unsupported email content type has been used. Please use only text or HTML emails.'); |
| 94 | } |
| 95 | return $email; |
| 96 | // phpcs:enable |
| 97 | } |
| 98 | |
| 99 | private function formatAddress($wordpressAddress) { |
| 100 | $data = $wordpressAddress[0]; |
| 101 | $result = [ |
| 102 | 'address' => $data[0], |
| 103 | ]; |
| 104 | if (!empty($data[1])) { |
| 105 | $result['full_name'] = $data[1]; |
| 106 | } |
| 107 | return $result; |
| 108 | } |
| 109 | |
| 110 | private function getReplyToAddress(): ?array { |
| 111 | $replyToAddress = null; |
| 112 | $addresses = $this->getReplyToAddresses(); |
| 113 | |
| 114 | if (!empty($addresses)) { |
| 115 | // only one reply-to address supported by \MailPoet\Mailer |
| 116 | $address = array_shift($addresses); |
| 117 | $replyToAddress = []; |
| 118 | |
| 119 | if ($address[1]) { |
| 120 | $replyToAddress['name'] = $address[1]; |
| 121 | } |
| 122 | |
| 123 | if ($address[0]) { |
| 124 | $replyToAddress['address'] = $address[0]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $replyToAddress; |
| 129 | } |
| 130 | } |
| 131 |