| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
abstract class Mailer |
| 40 |
{ |
| 41 |
protected $params = null; |
| 42 |
protected $from = null; |
| 43 |
protected $addr = []; |
| 44 |
protected $subj = null; |
| 45 |
protected $body = null; |
| 46 |
|
| 47 |
private function __construct(array $params) |
| 48 |
{ |
| 49 |
$this->params = $params; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static function get(array $params) |
| 62 |
{ |
| 63 |
$library = $params['library'] ?? 'internal'; |
| 64 |
switch ($library) { |
| 65 |
case 'internal': |
| 66 |
return new MailerInternal($params); |
| 67 |
case 'phpmailer': |
| 68 |
return new MailerPhpMailer($params); |
| 69 |
} |
| 70 |
throw new LogicException('Unsupported mailing library: ' . $library); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function addAddress(string $address) |
| 81 |
{ |
| 82 |
if (!in_array($address, $this->addr)) { |
| 83 |
$this->addr[] = $address; |
| 84 |
} |
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function setFrom(string $from) |
| 98 |
{ |
| 99 |
if (!filter_var($from, FILTER_VALIDATE_EMAIL)) { |
| 100 |
throw new LogicException('Invalid email address (From): ' . $from); |
| 101 |
} |
| 102 |
$this->from = $from; |
| 103 |
return $this; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function setSubject(string $subject) |
| 114 |
{ |
| 115 |
$this->subj = $subject; |
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function setBody($body) |
| 127 |
{ |
| 128 |
$this->body = $body; |
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
abstract public function send(): void; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected function checkData(): void |
| 147 |
{ |
| 148 |
if (!$this->from) { |
| 149 |
throw new LogicException('The "From" address is not specified'); |
| 150 |
} |
| 151 |
if (!count($this->addr)) { |
| 152 |
throw new LogicException('The "To" address is not specified'); |
| 153 |
} |
| 154 |
if (empty($this->subj)) { |
| 155 |
throw new LogicException('The subject of the message is not specified'); |
| 156 |
} |
| 157 |
if (!$this->body) { |
| 158 |
throw new LogicException('The body of the message is not specified'); |
| 159 |
} |
| 160 |
} |
| 161 |
} |