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
SMTP.php
98 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\Methods\ErrorMappers\SMTPMapper; |
| 9 | use MailPoet\RuntimeException; |
| 10 | use MailPoet\Util\Url; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | use PHPMailer\PHPMailer\PHPMailer; |
| 13 | |
| 14 | class SMTP extends PHPMailerMethod { |
| 15 | const SMTP_CONNECTION_TIMEOUT = 15; // seconds |
| 16 | |
| 17 | /** @var string */ |
| 18 | public $host; |
| 19 | /** @var int */ |
| 20 | public $port; |
| 21 | /** @var int */ |
| 22 | public $authentication; |
| 23 | /** @var string */ |
| 24 | public $login; |
| 25 | /** @var string */ |
| 26 | public $password; |
| 27 | /** @var string */ |
| 28 | public $encryption; |
| 29 | /** @var PHPMailer */ |
| 30 | public $mailer; |
| 31 | /** @var WPFunctions */ |
| 32 | protected $wp; |
| 33 | |
| 34 | public function __construct( |
| 35 | $host, |
| 36 | $port, |
| 37 | $authentication, |
| 38 | $encryption, |
| 39 | $sender, |
| 40 | $replyTo, |
| 41 | $returnPath, |
| 42 | SMTPMapper $errorMapper, |
| 43 | Url $urlUtils, |
| 44 | $login = null, |
| 45 | $password = null |
| 46 | ) { |
| 47 | $this->wp = new WPFunctions; |
| 48 | $this->host = $host; |
| 49 | $this->port = $port; |
| 50 | $this->authentication = $authentication; |
| 51 | $this->login = $login; |
| 52 | $this->password = $password; |
| 53 | $this->encryption = $encryption; |
| 54 | parent::__construct($sender, $replyTo, $returnPath, $errorMapper, $urlUtils); |
| 55 | } |
| 56 | |
| 57 | public function buildMailer(): PHPMailer { |
| 58 | $mailer = new PHPMailer(true); |
| 59 | $mailer->isSMTP(); |
| 60 | /** @phpstan-ignore-next-line - we cannot annotate the return type from a filter */ |
| 61 | $mailer->Host = $this->wp->applyFilters('mailpoet_mailer_smtp_host', $this->host); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 62 | /** @phpstan-ignore-next-line - we cannot annotate the return type from a filter */ |
| 63 | $mailer->Port = $this->wp->applyFilters('mailpoet_mailer_smtp_port', $this->port); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 64 | /** @phpstan-ignore-next-line - we cannot annotate the return type from a filter */ |
| 65 | $mailer->SMTPSecure = $this->wp->applyFilters('mailpoet_mailer_smtp_encryption', $this->encryption); |
| 66 | if (empty($mailer->SMTPSecure)) { |
| 67 | $mailer->SMTPAutoTLS = false; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 68 | } |
| 69 | /** @phpstan-ignore-next-line - we cannot annotate the return type from a filter */ |
| 70 | $mailer->SMTPOptions = $this->wp->applyFilters('mailpoet_mailer_smtp_options', []); |
| 71 | /** @phpstan-ignore-next-line - we cannot annotate the return type from a filter */ |
| 72 | $mailer->Timeout = $this->wp->applyFilters('mailpoet_mailer_smtp_connection_timeout', self::SMTP_CONNECTION_TIMEOUT); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 73 | |
| 74 | if ($this->authentication === 1) { |
| 75 | $mailer->SMTPAuth = true; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 76 | $mailer->Username = $this->login; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 77 | $mailer->Password = $this->password; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 78 | } |
| 79 | |
| 80 | // values from filters can overwrite username and password |
| 81 | $filterUsername = $this->wp->applyFilters('mailpoet_mailer_smtp_username', null); |
| 82 | $filterPassword = $this->wp->applyFilters('mailpoet_mailer_smtp_password', null); |
| 83 | if ($filterUsername && $filterPassword) { |
| 84 | $mailer->SMTPAuth = true; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 85 | /** @phpstan-ignore-next-line - we cannot annotate the return type from a filter */ |
| 86 | $mailer->Username = $filterUsername; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 87 | /** @phpstan-ignore-next-line - we cannot annotate the return type from a filter */ |
| 88 | $mailer->Password = $filterPassword; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 89 | } |
| 90 | |
| 91 | $mailer = $this->wp->applyFilters('mailpoet_mailer_smtp_instance', $mailer); |
| 92 | if (!$mailer instanceof PHPMailer) { |
| 93 | throw new RuntimeException(__('Filter "mailpoet_mailer_smtp_instance" must return an instance of PHPMailer.', 'mailpoet')); |
| 94 | } |
| 95 | return $mailer; |
| 96 | } |
| 97 | } |
| 98 |