| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace Liuch\DmarcSrg\Mail; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Exception\LogicException; |
| 35 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class MailerPhpMailer extends Mailer |
| 41 |
{ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function send(): void |
| 51 |
{ |
| 52 |
$this->checkData(); |
| 53 |
$mailer = new \PHPMailer\PHPMailer\PHPMailer(true); |
| 54 |
try { |
| 55 |
$mailer->setFrom($this->from); |
| 56 |
foreach ($this->addr as $to) { |
| 57 |
$mailer->addAddress($to); |
| 58 |
} |
| 59 |
$mailer->Subject = $this->subj; |
| 60 |
$mailer->XMailer = null; |
| 61 |
if (is_null($this->body->html)) { |
| 62 |
$mailer->Body = implode("\r\n", $this->body->text); |
| 63 |
} else { |
| 64 |
$mailer->isHTML(); |
| 65 |
$mailer->Body = implode("\r\n", $this->body->html); |
| 66 |
if (!is_null($this->body->text)) { |
| 67 |
$mailer->AltBody = implode("\r\n", $this->body->text); |
| 68 |
} |
| 69 |
} |
| 70 |
$method = $this->params['method'] ?? 'mail'; |
| 71 |
if ($method === 'smtp') { |
| 72 |
$mailer->isSMTP(); |
| 73 |
$mailer->Host = $this->params['host']; |
| 74 |
$mailer->Port = $this->params['port']; |
| 75 |
switch ($this->params['encryption'] ?? 'ssl') { |
| 76 |
case 'ssl': |
| 77 |
$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS; |
| 78 |
break; |
| 79 |
case 'starttls': |
| 80 |
$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS; |
| 81 |
break; |
| 82 |
case 'none': |
| 83 |
$mailer->SMTPAutoTLS = false; |
| 84 |
break; |
| 85 |
default: |
| 86 |
$mailer->SMTPAutoTLS = true; |
| 87 |
break; |
| 88 |
} |
| 89 |
$username = $this->params['username'] ?? null; |
| 90 |
if (!empty($username)) { |
| 91 |
$mailer->SMTPAuth = true; |
| 92 |
$mailer->Username = $username; |
| 93 |
$mailer->Password = $this->params['password'] ?? ''; |
| 94 |
} |
| 95 |
|
| 96 |
} elseif ($method === 'mail') { |
| 97 |
$mailer->isMail(); |
| 98 |
} else { |
| 99 |
throw new LogicException('Unsupported sending method'); |
| 100 |
} |
| 101 |
if ($this->params['novalidate-cert'] ?? false) { |
| 102 |
$mailer->SMTPOptions = [ |
| 103 |
'ssl' => [ |
| 104 |
'verify_peer' => false, |
| 105 |
'verify_peer_name' => false, |
| 106 |
'allow_self_signed' => true |
| 107 |
] |
| 108 |
]; |
| 109 |
} |
| 110 |
$mailer->send(); |
| 111 |
} catch (\Exception $e) { |
| 112 |
throw new RuntimeException($e->getMessage()); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected function checkData(): void |
| 124 |
{ |
| 125 |
parent::checkData(); |
| 126 |
if (isset($this->params['method']) && $this->params['method'] === 'smtp') { |
| 127 |
if (empty($this->params['host'])) { |
| 128 |
throw new LogicException('The SMTP server host is not specified'); |
| 129 |
} |
| 130 |
if (!is_int($this->params['port']) || $this->params['port'] <= 0) { |
| 131 |
throw new LogicException('The SMTP server port is not specified'); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |