Methods
2 weeks ago
WordPress
8 months ago
Mailer.php
2 years ago
MailerError.php
2 years ago
MailerFactory.php
1 year ago
MailerLog.php
1 year ago
MetaInfo.php
2 months ago
SubscriberError.php
3 years ago
index.php
3 years ago
MailerFactory.php
171 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Mailer; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\DI\ContainerWrapper; |
| 9 | use MailPoet\InvalidStateException; |
| 10 | use MailPoet\Mailer\Methods\AmazonSES; |
| 11 | use MailPoet\Mailer\Methods\ErrorMappers\AmazonSESMapper; |
| 12 | use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper; |
| 13 | use MailPoet\Mailer\Methods\ErrorMappers\PHPMailMapper; |
| 14 | use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper; |
| 15 | use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper; |
| 16 | use MailPoet\Mailer\Methods\MailPoet; |
| 17 | use MailPoet\Mailer\Methods\PHPMail; |
| 18 | use MailPoet\Mailer\Methods\SendGrid; |
| 19 | use MailPoet\Mailer\Methods\SMTP; |
| 20 | use MailPoet\Services\AuthorizedEmailsController; |
| 21 | use MailPoet\Services\Bridge; |
| 22 | use MailPoet\Settings\SettingsController; |
| 23 | use MailPoet\Util\Url; |
| 24 | use MailPoet\WP\Functions as WPFunctions; |
| 25 | |
| 26 | class MailerFactory { |
| 27 | /** @var SettingsController */ |
| 28 | private $settings; |
| 29 | |
| 30 | /** @var WPFunctions */ |
| 31 | private $wp; |
| 32 | |
| 33 | /** @var Mailer */ |
| 34 | private $defaultMailer; |
| 35 | |
| 36 | public function __construct( |
| 37 | SettingsController $settings, |
| 38 | WPFunctions $wp |
| 39 | ) { |
| 40 | $this->settings = $settings; |
| 41 | $this->wp = $wp; |
| 42 | } |
| 43 | |
| 44 | public function getDefaultMailer(): Mailer { |
| 45 | if ($this->defaultMailer === null) { |
| 46 | $this->defaultMailer = $this->buildMailer(); |
| 47 | } |
| 48 | return $this->defaultMailer; |
| 49 | } |
| 50 | |
| 51 | public function buildMailer(?array $mailerConfig = null, ?array $sender = null, ?array $replyTo = null, ?string $returnPath = null): Mailer { |
| 52 | $sender = $this->getSenderNameAndAddress($sender); |
| 53 | $replyTo = $this->getReplyToNameAndAddress($sender, $replyTo); |
| 54 | $mailerConfig = $mailerConfig ?? $this->getMailerConfig(); |
| 55 | $returnPath = $returnPath ?? $this->getReturnPathAddress($sender); |
| 56 | switch ($mailerConfig['method']) { |
| 57 | case Mailer::METHOD_AMAZONSES: |
| 58 | $mailerMethod = new AmazonSES( |
| 59 | $mailerConfig['region'], |
| 60 | $mailerConfig['access_key'], |
| 61 | $mailerConfig['secret_key'], |
| 62 | $sender, |
| 63 | $replyTo, |
| 64 | $returnPath, |
| 65 | new AmazonSESMapper(), |
| 66 | $this->wp, |
| 67 | ContainerWrapper::getInstance()->get(Url::class) |
| 68 | ); |
| 69 | break; |
| 70 | case Mailer::METHOD_MAILPOET: |
| 71 | $mailerMethod = new MailPoet( |
| 72 | $mailerConfig['mailpoet_api_key'], |
| 73 | $sender, |
| 74 | $replyTo, |
| 75 | ContainerWrapper::getInstance()->get(MailPoetMapper::class), |
| 76 | ContainerWrapper::getInstance()->get(AuthorizedEmailsController::class), |
| 77 | ContainerWrapper::getInstance()->get(Bridge::class), |
| 78 | ContainerWrapper::getInstance()->get(Url::class) |
| 79 | ); |
| 80 | break; |
| 81 | case Mailer::METHOD_SENDGRID: |
| 82 | $mailerMethod = new SendGrid( |
| 83 | $mailerConfig['api_key'], |
| 84 | $sender, |
| 85 | $replyTo, |
| 86 | new SendGridMapper(), |
| 87 | ContainerWrapper::getInstance()->get(Url::class) |
| 88 | ); |
| 89 | break; |
| 90 | case Mailer::METHOD_PHPMAIL: |
| 91 | $mailerMethod = new PHPMail( |
| 92 | $sender, |
| 93 | $replyTo, |
| 94 | $returnPath, |
| 95 | new PHPMailMapper(), |
| 96 | ContainerWrapper::getInstance()->get(Url::class) |
| 97 | ); |
| 98 | break; |
| 99 | case Mailer::METHOD_SMTP: |
| 100 | $mailerMethod = new SMTP( |
| 101 | $mailerConfig['host'], |
| 102 | $mailerConfig['port'], |
| 103 | (int)$mailerConfig['authentication'], |
| 104 | $mailerConfig['encryption'], |
| 105 | $sender, |
| 106 | $replyTo, |
| 107 | $returnPath, |
| 108 | new SMTPMapper(), |
| 109 | ContainerWrapper::getInstance()->get(Url::class), |
| 110 | $mailerConfig['login'], |
| 111 | $mailerConfig['password'] |
| 112 | ); |
| 113 | break; |
| 114 | default: |
| 115 | throw new InvalidStateException(__('Mailing method does not exist.', 'mailpoet')); |
| 116 | } |
| 117 | return new Mailer($mailerMethod); |
| 118 | } |
| 119 | |
| 120 | private function getMailerConfig(): array { |
| 121 | $config = $this->settings->get(Mailer::MAILER_CONFIG_SETTING_NAME); |
| 122 | if (!$config || !isset($config['method'])) throw new InvalidStateException(__('Mailer is not configured.', 'mailpoet')); |
| 123 | return $config; |
| 124 | } |
| 125 | |
| 126 | private function getSenderNameAndAddress(?array $sender = null): array { |
| 127 | if (empty($sender)) { |
| 128 | $sender = $this->settings->get('sender', []); |
| 129 | if (empty($sender['address'])) throw new InvalidStateException(__('Sender name and email are not configured.', 'mailpoet')); |
| 130 | } |
| 131 | $fromName = $this->encodeAddressNamePart($sender['name'] ?? ''); |
| 132 | return [ |
| 133 | 'from_name' => $fromName, |
| 134 | 'from_email' => $sender['address'], |
| 135 | 'from_name_email' => sprintf('%s <%s>', $fromName, $sender['address']), |
| 136 | ]; |
| 137 | } |
| 138 | |
| 139 | private function getReplyToNameAndAddress(array $sender, ?array $replyTo = null): array { |
| 140 | if (!$replyTo) { |
| 141 | $replyTo = $this->settings->get('reply_to'); |
| 142 | $replyTo['name'] = (!empty($replyTo['name'])) ? |
| 143 | $replyTo['name'] : |
| 144 | $sender['from_name']; |
| 145 | $replyTo['address'] = (!empty($replyTo['address'])) ? |
| 146 | $replyTo['address'] : |
| 147 | $sender['from_email']; |
| 148 | } |
| 149 | if (empty($replyTo['address'])) { |
| 150 | $replyTo['address'] = $sender['from_email']; |
| 151 | } |
| 152 | $replyToName = $this->encodeAddressNamePart($replyTo['name'] ?? ''); |
| 153 | return [ |
| 154 | 'reply_to_name' => $replyToName, |
| 155 | 'reply_to_email' => $replyTo['address'], |
| 156 | 'reply_to_name_email' => sprintf('%s <%s>', $replyToName, $replyTo['address']), |
| 157 | ]; |
| 158 | } |
| 159 | |
| 160 | private function getReturnPathAddress(array $sender): ?string { |
| 161 | $bounceAddress = (string)$this->settings->get('bounce.address'); |
| 162 | return $this->wp->isEmail($bounceAddress) ? $bounceAddress : $sender['from_email']; |
| 163 | } |
| 164 | |
| 165 | private function encodeAddressNamePart($name): string { |
| 166 | if (mb_detect_encoding($name) === 'ASCII') return $name; |
| 167 | // encode non-ASCII string as per RFC 2047 (https://www.ietf.org/rfc/rfc2047.txt) |
| 168 | return sprintf('=?utf-8?B?%s?=', base64_encode($name)); |
| 169 | } |
| 170 | } |
| 171 |