@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | * @param string $address email address for the recipient |
| 80 | 80 | * @param string $title name of the recipient (optional) |
| 81 | 81 | |
| 82 | - * @return object instantiated $this |
|
| 82 | + * @return Archangel instantiated $this |
|
| 83 | 83 | */ |
| 84 | 84 | public function addTo($address, $title = '') |
| 85 | 85 | { |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | * @param string $address email address for the cc recipient |
| 98 | 98 | * @param string $title name of the cc recipient (optional) |
| 99 | 99 | * |
| 100 | - * @return object instantiated $this |
|
| 100 | + * @return Archangel instantiated $this |
|
| 101 | 101 | */ |
| 102 | 102 | public function addCC($address, $title = '') |
| 103 | 103 | { |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @param string $address email address for the bcc recipient |
| 120 | 120 | * @param string $title name of the bcc recipient (optional) |
| 121 | 121 | * |
| 122 | - * @return object instantiated $this |
|
| 122 | + * @return Archangel instantiated $this |
|
| 123 | 123 | */ |
| 124 | 124 | public function addBCC($address, $title = '') |
| 125 | 125 | { |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | * @param string $address email address for the sender |
| 142 | 142 | * @param string $title name of the sender (optional) |
| 143 | 143 | * |
| 144 | - * @return object instantiated $this |
|
| 144 | + * @return Archangel instantiated $this |
|
| 145 | 145 | */ |
| 146 | 146 | public function setFrom($address, $title = '') |
| 147 | 147 | { |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | * @param string $address email address for the reply-to |
| 157 | 157 | * @param string $title name of the reply-to (optional) |
| 158 | 158 | * |
| 159 | - * @return object instantiated $this |
|
| 159 | + * @return Archangel instantiated $this |
|
| 160 | 160 | */ |
| 161 | 161 | public function setReplyTo($address, $title = '') |
| 162 | 162 | { |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | * |
| 185 | 185 | * @param string $subject subject for the email |
| 186 | 186 | * |
| 187 | - * @return object instantiated $this |
|
| 187 | + * @return Archangel instantiated $this |
|
| 188 | 188 | */ |
| 189 | 189 | public function setSubject($subject) |
| 190 | 190 | { |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | * |
| 199 | 199 | * @param string $message the plain-text message |
| 200 | 200 | * |
| 201 | - * @return object instantiated $this |
|
| 201 | + * @return Archangel instantiated $this |
|
| 202 | 202 | */ |
| 203 | 203 | public function setPlainMessage($message) |
| 204 | 204 | { |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @param string $message the html message |
| 214 | 214 | * |
| 215 | - * @return object instantiated $this |
|
| 215 | + * @return Archangel instantiated $this |
|
| 216 | 216 | */ |
| 217 | 217 | public function setHTMLMessage($message) |
| 218 | 218 | { |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | * @param string $type mime type of the file |
| 229 | 229 | * @param string $title the title of the attachment (optional) |
| 230 | 230 | * |
| 231 | - * @return object instantiated $this |
|
| 231 | + * @return Archangel instantiated $this |
|
| 232 | 232 | */ |
| 233 | 233 | public function addAttachment($path, $type, $title = '') |
| 234 | 234 | { |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | /** |
| 375 | 375 | * Shared holder for the plain message header |
| 376 | 376 | * |
| 377 | - * @return array |
|
| 377 | + * @return string[] |
|
| 378 | 378 | */ |
| 379 | 379 | protected function getPlainMessageHeader() |
| 380 | 380 | { |
@@ -388,7 +388,7 @@ discard block |
||
| 388 | 388 | /** |
| 389 | 389 | * Shared holder for the html message header |
| 390 | 390 | * |
| 391 | - * @return array |
|
| 391 | + * @return string[] |
|
| 392 | 392 | */ |
| 393 | 393 | protected function getHtmlMessageHeader() |
| 394 | 394 | { |
@@ -16,444 +16,443 @@ |
||
| 16 | 16 | class Archangel implements LoggerAwareInterface |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** @var string $subject */ |
|
| 20 | - protected $subject; |
|
| 21 | - |
|
| 22 | - /** @var array $toAddresses */ |
|
| 23 | - protected $toAddresses = array(); |
|
| 24 | - |
|
| 25 | - /** @var array $headers */ |
|
| 26 | - protected $headers = array(); |
|
| 27 | - |
|
| 28 | - /** @var string $plainMessage */ |
|
| 29 | - protected $plainMessage; |
|
| 30 | - |
|
| 31 | - /** @var string $htmlMessage */ |
|
| 32 | - protected $htmlMessage; |
|
| 33 | - |
|
| 34 | - /** @var array $attachments */ |
|
| 35 | - protected $attachments = array(); |
|
| 36 | - |
|
| 37 | - /** @var string $boundaryMixed */ |
|
| 38 | - protected $boundaryMixed; |
|
| 39 | - |
|
| 40 | - /** @var string $boundaryAlternative */ |
|
| 41 | - protected $boundaryAlternative; |
|
| 42 | - |
|
| 43 | - /** @var LoggerInterface */ |
|
| 44 | - protected $logger; |
|
| 45 | - |
|
| 46 | - /** @var string LINE_BREAK */ |
|
| 47 | - const LINE_BREAK = "\r\n"; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param string $mailer |
|
| 51 | - */ |
|
| 52 | - public function __construct($mailer = null) |
|
| 53 | - { |
|
| 54 | - if (is_null($mailer)) { |
|
| 55 | - $mailer = sprintf('PHP/%s', phpversion()); |
|
| 56 | - } |
|
| 57 | - $this->headers['X-Mailer'] = $mailer; |
|
| 58 | - |
|
| 59 | - $this->logger = new NullLogger(); |
|
| 60 | - $this->boundaryMixed = sprintf('PHP-mixed-%s', uniqid()); |
|
| 61 | - $this->boundaryAlternative = sprintf('PHP-alternative-%s', uniqid()); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param LoggerInterface $logger |
|
| 66 | - * |
|
| 67 | - * @return $this; |
|
| 68 | - */ |
|
| 69 | - public function setLogger(LoggerInterface $logger) |
|
| 70 | - { |
|
| 71 | - $this->logger = $logger; |
|
| 72 | - |
|
| 73 | - return $this; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Setter method for adding recipients |
|
| 78 | - * |
|
| 79 | - * @param string $address email address for the recipient |
|
| 80 | - * @param string $title name of the recipient (optional) |
|
| 81 | - |
|
| 82 | - * @return object instantiated $this |
|
| 83 | - */ |
|
| 84 | - public function addTo($address, $title = '') |
|
| 85 | - { |
|
| 86 | - array_push( |
|
| 87 | - $this->toAddresses, |
|
| 88 | - $this->formatEmailAddress($address, $title) |
|
| 89 | - ); |
|
| 90 | - |
|
| 91 | - return $this; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Setter method for adding cc recipients |
|
| 96 | - * |
|
| 97 | - * @param string $address email address for the cc recipient |
|
| 98 | - * @param string $title name of the cc recipient (optional) |
|
| 99 | - * |
|
| 100 | - * @return object instantiated $this |
|
| 101 | - */ |
|
| 102 | - public function addCC($address, $title = '') |
|
| 103 | - { |
|
| 104 | - if (!isset($this->headers['CC'])) { |
|
| 105 | - $this->headers['CC'] = array(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - array_push( |
|
| 109 | - $this->headers['CC'], |
|
| 110 | - $this->formatEmailAddress($address, $title) |
|
| 111 | - ); |
|
| 112 | - |
|
| 113 | - return $this; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Setter method for adding bcc recipients |
|
| 118 | - * |
|
| 119 | - * @param string $address email address for the bcc recipient |
|
| 120 | - * @param string $title name of the bcc recipient (optional) |
|
| 121 | - * |
|
| 122 | - * @return object instantiated $this |
|
| 123 | - */ |
|
| 124 | - public function addBCC($address, $title = '') |
|
| 125 | - { |
|
| 126 | - if (!isset($this->headers['BCC'])) { |
|
| 127 | - $this->headers['BCC'] = array(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - array_push( |
|
| 131 | - $this->headers['BCC'], |
|
| 132 | - $this->formatEmailAddress($address, $title) |
|
| 133 | - ); |
|
| 134 | - |
|
| 135 | - return $this; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Setter method for setting the single 'from' field |
|
| 140 | - * |
|
| 141 | - * @param string $address email address for the sender |
|
| 142 | - * @param string $title name of the sender (optional) |
|
| 143 | - * |
|
| 144 | - * @return object instantiated $this |
|
| 145 | - */ |
|
| 146 | - public function setFrom($address, $title = '') |
|
| 147 | - { |
|
| 148 | - $this->headers['From'] = $this->formatEmailAddress($address, $title); |
|
| 149 | - |
|
| 150 | - return $this; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Setter method for setting the single 'reply-to' field |
|
| 155 | - * |
|
| 156 | - * @param string $address email address for the reply-to |
|
| 157 | - * @param string $title name of the reply-to (optional) |
|
| 158 | - * |
|
| 159 | - * @return object instantiated $this |
|
| 160 | - */ |
|
| 161 | - public function setReplyTo($address, $title = '') |
|
| 162 | - { |
|
| 163 | - $this->headers['Reply-To'] = $this->formatEmailAddress($address, $title); |
|
| 164 | - |
|
| 165 | - return $this; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @param string $address |
|
| 170 | - * @param string $title |
|
| 171 | - * |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - protected function formatEmailAddress($address, $title) |
|
| 175 | - { |
|
| 176 | - if (!empty($title)) { |
|
| 177 | - $address = sprintf('"%s" <%s>', $title, $address); |
|
| 178 | - } |
|
| 179 | - return $address; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Setter method for setting a subject |
|
| 184 | - * |
|
| 185 | - * @param string $subject subject for the email |
|
| 186 | - * |
|
| 187 | - * @return object instantiated $this |
|
| 188 | - */ |
|
| 189 | - public function setSubject($subject) |
|
| 190 | - { |
|
| 191 | - $this->subject = $subject; |
|
| 192 | - |
|
| 193 | - return $this; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Setter method for the plain text message |
|
| 198 | - * |
|
| 199 | - * @param string $message the plain-text message |
|
| 200 | - * |
|
| 201 | - * @return object instantiated $this |
|
| 202 | - */ |
|
| 203 | - public function setPlainMessage($message) |
|
| 204 | - { |
|
| 205 | - $this->plainMessage = $message; |
|
| 206 | - |
|
| 207 | - return $this; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Setter method for the html message |
|
| 212 | - * |
|
| 213 | - * @param string $message the html message |
|
| 214 | - * |
|
| 215 | - * @return object instantiated $this |
|
| 216 | - */ |
|
| 217 | - public function setHTMLMessage($message) |
|
| 218 | - { |
|
| 219 | - $this->htmlMessage = $message; |
|
| 220 | - |
|
| 221 | - return $this; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Setter method for adding attachments |
|
| 226 | - * |
|
| 227 | - * @param string $path the full path of the attachment |
|
| 228 | - * @param string $type mime type of the file |
|
| 229 | - * @param string $title the title of the attachment (optional) |
|
| 230 | - * |
|
| 231 | - * @return object instantiated $this |
|
| 232 | - */ |
|
| 233 | - public function addAttachment($path, $type, $title = '') |
|
| 234 | - { |
|
| 235 | - array_push($this->attachments, array( |
|
| 236 | - 'path' => $path, |
|
| 237 | - 'type' => $type, |
|
| 238 | - 'title' => $title, |
|
| 239 | - )); |
|
| 240 | - |
|
| 241 | - return $this; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * The executing step, the actual sending of the email |
|
| 246 | - * First checks to make sure the minimum fields are set (returns false if they are not) |
|
| 247 | - * Second it attempts to send the mail with php's mail() (returns false if it fails) |
|
| 248 | - * |
|
| 249 | - * return boolean whether or not the email was valid & sent |
|
| 250 | - */ |
|
| 251 | - public function send() |
|
| 252 | - { |
|
| 253 | - if (!$this->checkRequiredFields()) { |
|
| 254 | - return false; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - $recipients = $this->buildTo(); |
|
| 258 | - $subject = $this->subject; |
|
| 259 | - $message = (empty($this->attachments)) ? $this->buildMessage() : $this->buildHtmlMessage(); |
|
| 260 | - $headers = $this->buildHeaders(); |
|
| 261 | - |
|
| 262 | - return mail($recipients, $subject, $message, $headers); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Call to check the minimum required fields |
|
| 267 | - * |
|
| 268 | - * @return boolean whether or not the email meets the minimum required fields |
|
| 269 | - */ |
|
| 270 | - protected function checkRequiredFields() |
|
| 271 | - { |
|
| 272 | - if (empty($this->toAddresses)) { |
|
| 273 | - return false; |
|
| 274 | - } |
|
| 275 | - if (empty($this->subject)) { |
|
| 276 | - return false; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - if ( |
|
| 280 | - empty($this->plainMessage) && |
|
| 281 | - empty($this->htmlMessage) && |
|
| 282 | - empty($this->attachments) |
|
| 283 | - ) { |
|
| 284 | - return false; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return true; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Build the recipients from 'to' |
|
| 292 | - * |
|
| 293 | - * @return string comma-separated lit of recipients |
|
| 294 | - */ |
|
| 295 | - protected function buildTo() |
|
| 296 | - { |
|
| 297 | - return implode(', ', $this->toAddresses); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Returns a simple email message without attachments |
|
| 302 | - * |
|
| 303 | - * @return string email message |
|
| 304 | - */ |
|
| 305 | - protected function buildMessage() |
|
| 306 | - { |
|
| 307 | - if (empty($this->plainMessage) && empty($this->htmlMessage)) { |
|
| 308 | - return ''; |
|
| 309 | - } |
|
| 310 | - if (!empty($this->plainMessage) && empty($this->htmlMessage)) { |
|
| 311 | - return $this->plainMessage; |
|
| 312 | - } |
|
| 313 | - if (empty($this->plainMessage) && !empty($this->htmlMessage)) { |
|
| 314 | - return $this->htmlMessage; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - $message = array(); |
|
| 318 | - array_push($message, "--{$this->boundaryAlternative}"); |
|
| 319 | - $message += $this->buildPlainMessageHeaders(); |
|
| 320 | - array_push($message, $this->plainMessage); |
|
| 321 | - array_push($message, "--{$this->boundaryAlternative}"); |
|
| 322 | - $message += $this->buildHtmlMessageHeaders(); |
|
| 323 | - array_push($message, $this->htmlMessage); |
|
| 324 | - array_push($message, "--{$this->boundaryAlternative}--"); |
|
| 325 | - |
|
| 326 | - return implode(self::LINE_BREAK, $message); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Build multi-part message with attachments |
|
| 331 | - * |
|
| 332 | - * @return string email message |
|
| 333 | - */ |
|
| 334 | - protected function buildMessageWithAttachments() |
|
| 335 | - { |
|
| 336 | - $message = array(); |
|
| 337 | - |
|
| 338 | - if (!empty($this->plainMessage) || !empty($this->htmlMessage)) { |
|
| 339 | - array_push($message, "--{$this->boundaryMixed}"); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - if (!empty($this->plainMessage) && !empty($this->htmlMessage)) { |
|
| 343 | - array_push($message, "Content-Type: multipart/alternative; boundary={$this->boundaryAlternative}"); |
|
| 344 | - array_push($message, ''); |
|
| 345 | - array_push($message, "--{$this->boundaryAlternative}"); |
|
| 346 | - $message += $this->buildPlainMessageHeaders(); |
|
| 347 | - array_push($message, $this->plainMessage); |
|
| 348 | - array_push($message, "--{$this->boundaryAlternative}"); |
|
| 349 | - $message += $this->buildHtmlMessageHeaders(); |
|
| 350 | - array_push($message, $this->htmlMessage); |
|
| 351 | - array_push($message, "--{$this->boundaryAlternative}--"); |
|
| 352 | - array_push($message, ''); |
|
| 353 | - } elseif (!empty($this->plainMessage)) { |
|
| 354 | - $message += $this->buildPlainMessageHeaders(); |
|
| 355 | - array_push($message, $this->plainMessage); |
|
| 356 | - } elseif (!empty($this->htmlMessage)) { |
|
| 357 | - $message += $this->buildHtmlMessageHeaders(); |
|
| 358 | - array_push($message, $this->htmlMessage); |
|
| 359 | - } |
|
| 360 | - foreach ($this->attachments as $attachment) { |
|
| 361 | - array_push($message, "--{$this->boundaryMixed}"); |
|
| 362 | - array_push($message, "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\""); |
|
| 363 | - array_push($message, 'Content-Transfer-Encoding: base64'); |
|
| 364 | - array_push($message, 'Content-Disposition: attachment'); |
|
| 365 | - array_push($message, ''); |
|
| 366 | - array_push($message, $this->buildAttachmentContent($attachment['path'])); |
|
| 367 | - } |
|
| 368 | - array_push($message, "--{$this->boundaryMixed}--"); |
|
| 369 | - |
|
| 370 | - return implode(self::LINE_BREAK, $message); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Shared holder for the plain message header |
|
| 376 | - * |
|
| 377 | - * @return array |
|
| 378 | - */ |
|
| 379 | - protected function getPlainMessageHeader() |
|
| 380 | - { |
|
| 381 | - return array( |
|
| 382 | - 'Content-Type: text/plain; charset="iso-8859"', |
|
| 383 | - 'Content-Transfer-Encoding: 7bit', |
|
| 384 | - '', |
|
| 385 | - ); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Shared holder for the html message header |
|
| 390 | - * |
|
| 391 | - * @return array |
|
| 392 | - */ |
|
| 393 | - protected function getHtmlMessageHeader() |
|
| 394 | - { |
|
| 395 | - return array( |
|
| 396 | - 'Content-Type: text/html; charset="iso-8859-1"', |
|
| 397 | - 'Content-Transfer-Encoding: 7bit', |
|
| 398 | - '', |
|
| 399 | - ); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Builder for the additional headers needed for multipart emails |
|
| 404 | - * |
|
| 405 | - * @return string headers needed for multipart |
|
| 406 | - */ |
|
| 407 | - protected function buildHeaders() |
|
| 408 | - { |
|
| 409 | - $headers = array(); |
|
| 410 | - foreach ($this->headers as $key => $value) { |
|
| 411 | - if ($key == 'CC' || $key == 'BCC') { |
|
| 412 | - $value = implode(', ', $value); |
|
| 413 | - } |
|
| 414 | - array_push($headers, sprintf('%s: %s', $key, $value)); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - if (!empty($this->attachments)) { |
|
| 418 | - array_push( |
|
| 419 | - $headers, |
|
| 420 | - "Content-Type: multipart/mixed; boundary=\"{$this->boundaryMixed}\"" |
|
| 421 | - ); |
|
| 422 | - } elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) { |
|
| 423 | - array_push( |
|
| 424 | - $headers, |
|
| 425 | - "Content-Type: multipart/alternative; boundary=\"{$this->boundaryAlternative}\"" |
|
| 426 | - ); |
|
| 427 | - } elseif (!empty($this->htmlMessage)) { |
|
| 428 | - array_push( |
|
| 429 | - $headers, |
|
| 430 | - 'Content-type: text/html; charset="iso-8859-1"' |
|
| 431 | - ); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - return implode(self::LINE_BREAK, $headers); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * File reader for attachments |
|
| 439 | - * |
|
| 440 | - * @param string $path filepath of the attachment |
|
| 441 | - * |
|
| 442 | - * @return string binary representation of file, base64'd |
|
| 443 | - */ |
|
| 444 | - protected function buildAttachmentContent($path) |
|
| 445 | - { |
|
| 446 | - if (!file_exists($path)) { |
|
| 447 | - $this->logger->error("Could not find file {$path} for attaching to Archangel mail."); |
|
| 448 | - return ''; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - $handle = fopen($path, 'r'); |
|
| 452 | - $contents = fread($handle, filesize($path)); |
|
| 453 | - fclose($handle); |
|
| 454 | - |
|
| 455 | - $contents = base64_encode($contents); |
|
| 456 | - $contents = chunk_split($contents); |
|
| 457 | - return $contents; |
|
| 458 | - } |
|
| 19 | + /** @var string $subject */ |
|
| 20 | + protected $subject; |
|
| 21 | + |
|
| 22 | + /** @var array $toAddresses */ |
|
| 23 | + protected $toAddresses = array(); |
|
| 24 | + |
|
| 25 | + /** @var array $headers */ |
|
| 26 | + protected $headers = array(); |
|
| 27 | + |
|
| 28 | + /** @var string $plainMessage */ |
|
| 29 | + protected $plainMessage; |
|
| 30 | + |
|
| 31 | + /** @var string $htmlMessage */ |
|
| 32 | + protected $htmlMessage; |
|
| 33 | + |
|
| 34 | + /** @var array $attachments */ |
|
| 35 | + protected $attachments = array(); |
|
| 36 | + |
|
| 37 | + /** @var string $boundaryMixed */ |
|
| 38 | + protected $boundaryMixed; |
|
| 39 | + |
|
| 40 | + /** @var string $boundaryAlternative */ |
|
| 41 | + protected $boundaryAlternative; |
|
| 42 | + |
|
| 43 | + /** @var LoggerInterface */ |
|
| 44 | + protected $logger; |
|
| 45 | + |
|
| 46 | + /** @var string LINE_BREAK */ |
|
| 47 | + const LINE_BREAK = "\r\n"; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param string $mailer |
|
| 51 | + */ |
|
| 52 | + public function __construct($mailer = null) |
|
| 53 | + { |
|
| 54 | + if (is_null($mailer)) { |
|
| 55 | + $mailer = sprintf('PHP/%s', phpversion()); |
|
| 56 | + } |
|
| 57 | + $this->headers['X-Mailer'] = $mailer; |
|
| 58 | + |
|
| 59 | + $this->logger = new NullLogger(); |
|
| 60 | + $this->boundaryMixed = sprintf('PHP-mixed-%s', uniqid()); |
|
| 61 | + $this->boundaryAlternative = sprintf('PHP-alternative-%s', uniqid()); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param LoggerInterface $logger |
|
| 66 | + * |
|
| 67 | + * @return $this; |
|
| 68 | + */ |
|
| 69 | + public function setLogger(LoggerInterface $logger) |
|
| 70 | + { |
|
| 71 | + $this->logger = $logger; |
|
| 72 | + |
|
| 73 | + return $this; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Setter method for adding recipients |
|
| 78 | + * |
|
| 79 | + * @param string $address email address for the recipient |
|
| 80 | + * @param string $title name of the recipient (optional) |
|
| 81 | + * @return object instantiated $this |
|
| 82 | + */ |
|
| 83 | + public function addTo($address, $title = '') |
|
| 84 | + { |
|
| 85 | + array_push( |
|
| 86 | + $this->toAddresses, |
|
| 87 | + $this->formatEmailAddress($address, $title) |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + return $this; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Setter method for adding cc recipients |
|
| 95 | + * |
|
| 96 | + * @param string $address email address for the cc recipient |
|
| 97 | + * @param string $title name of the cc recipient (optional) |
|
| 98 | + * |
|
| 99 | + * @return object instantiated $this |
|
| 100 | + */ |
|
| 101 | + public function addCC($address, $title = '') |
|
| 102 | + { |
|
| 103 | + if (!isset($this->headers['CC'])) { |
|
| 104 | + $this->headers['CC'] = array(); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + array_push( |
|
| 108 | + $this->headers['CC'], |
|
| 109 | + $this->formatEmailAddress($address, $title) |
|
| 110 | + ); |
|
| 111 | + |
|
| 112 | + return $this; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Setter method for adding bcc recipients |
|
| 117 | + * |
|
| 118 | + * @param string $address email address for the bcc recipient |
|
| 119 | + * @param string $title name of the bcc recipient (optional) |
|
| 120 | + * |
|
| 121 | + * @return object instantiated $this |
|
| 122 | + */ |
|
| 123 | + public function addBCC($address, $title = '') |
|
| 124 | + { |
|
| 125 | + if (!isset($this->headers['BCC'])) { |
|
| 126 | + $this->headers['BCC'] = array(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + array_push( |
|
| 130 | + $this->headers['BCC'], |
|
| 131 | + $this->formatEmailAddress($address, $title) |
|
| 132 | + ); |
|
| 133 | + |
|
| 134 | + return $this; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Setter method for setting the single 'from' field |
|
| 139 | + * |
|
| 140 | + * @param string $address email address for the sender |
|
| 141 | + * @param string $title name of the sender (optional) |
|
| 142 | + * |
|
| 143 | + * @return object instantiated $this |
|
| 144 | + */ |
|
| 145 | + public function setFrom($address, $title = '') |
|
| 146 | + { |
|
| 147 | + $this->headers['From'] = $this->formatEmailAddress($address, $title); |
|
| 148 | + |
|
| 149 | + return $this; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Setter method for setting the single 'reply-to' field |
|
| 154 | + * |
|
| 155 | + * @param string $address email address for the reply-to |
|
| 156 | + * @param string $title name of the reply-to (optional) |
|
| 157 | + * |
|
| 158 | + * @return object instantiated $this |
|
| 159 | + */ |
|
| 160 | + public function setReplyTo($address, $title = '') |
|
| 161 | + { |
|
| 162 | + $this->headers['Reply-To'] = $this->formatEmailAddress($address, $title); |
|
| 163 | + |
|
| 164 | + return $this; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @param string $address |
|
| 169 | + * @param string $title |
|
| 170 | + * |
|
| 171 | + * @return string |
|
| 172 | + */ |
|
| 173 | + protected function formatEmailAddress($address, $title) |
|
| 174 | + { |
|
| 175 | + if (!empty($title)) { |
|
| 176 | + $address = sprintf('"%s" <%s>', $title, $address); |
|
| 177 | + } |
|
| 178 | + return $address; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Setter method for setting a subject |
|
| 183 | + * |
|
| 184 | + * @param string $subject subject for the email |
|
| 185 | + * |
|
| 186 | + * @return object instantiated $this |
|
| 187 | + */ |
|
| 188 | + public function setSubject($subject) |
|
| 189 | + { |
|
| 190 | + $this->subject = $subject; |
|
| 191 | + |
|
| 192 | + return $this; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Setter method for the plain text message |
|
| 197 | + * |
|
| 198 | + * @param string $message the plain-text message |
|
| 199 | + * |
|
| 200 | + * @return object instantiated $this |
|
| 201 | + */ |
|
| 202 | + public function setPlainMessage($message) |
|
| 203 | + { |
|
| 204 | + $this->plainMessage = $message; |
|
| 205 | + |
|
| 206 | + return $this; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Setter method for the html message |
|
| 211 | + * |
|
| 212 | + * @param string $message the html message |
|
| 213 | + * |
|
| 214 | + * @return object instantiated $this |
|
| 215 | + */ |
|
| 216 | + public function setHTMLMessage($message) |
|
| 217 | + { |
|
| 218 | + $this->htmlMessage = $message; |
|
| 219 | + |
|
| 220 | + return $this; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Setter method for adding attachments |
|
| 225 | + * |
|
| 226 | + * @param string $path the full path of the attachment |
|
| 227 | + * @param string $type mime type of the file |
|
| 228 | + * @param string $title the title of the attachment (optional) |
|
| 229 | + * |
|
| 230 | + * @return object instantiated $this |
|
| 231 | + */ |
|
| 232 | + public function addAttachment($path, $type, $title = '') |
|
| 233 | + { |
|
| 234 | + array_push($this->attachments, array( |
|
| 235 | + 'path' => $path, |
|
| 236 | + 'type' => $type, |
|
| 237 | + 'title' => $title, |
|
| 238 | + )); |
|
| 239 | + |
|
| 240 | + return $this; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * The executing step, the actual sending of the email |
|
| 245 | + * First checks to make sure the minimum fields are set (returns false if they are not) |
|
| 246 | + * Second it attempts to send the mail with php's mail() (returns false if it fails) |
|
| 247 | + * |
|
| 248 | + * return boolean whether or not the email was valid & sent |
|
| 249 | + */ |
|
| 250 | + public function send() |
|
| 251 | + { |
|
| 252 | + if (!$this->checkRequiredFields()) { |
|
| 253 | + return false; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + $recipients = $this->buildTo(); |
|
| 257 | + $subject = $this->subject; |
|
| 258 | + $message = (empty($this->attachments)) ? $this->buildMessage() : $this->buildHtmlMessage(); |
|
| 259 | + $headers = $this->buildHeaders(); |
|
| 260 | + |
|
| 261 | + return mail($recipients, $subject, $message, $headers); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Call to check the minimum required fields |
|
| 266 | + * |
|
| 267 | + * @return boolean whether or not the email meets the minimum required fields |
|
| 268 | + */ |
|
| 269 | + protected function checkRequiredFields() |
|
| 270 | + { |
|
| 271 | + if (empty($this->toAddresses)) { |
|
| 272 | + return false; |
|
| 273 | + } |
|
| 274 | + if (empty($this->subject)) { |
|
| 275 | + return false; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + if ( |
|
| 279 | + empty($this->plainMessage) && |
|
| 280 | + empty($this->htmlMessage) && |
|
| 281 | + empty($this->attachments) |
|
| 282 | + ) { |
|
| 283 | + return false; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + return true; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Build the recipients from 'to' |
|
| 291 | + * |
|
| 292 | + * @return string comma-separated lit of recipients |
|
| 293 | + */ |
|
| 294 | + protected function buildTo() |
|
| 295 | + { |
|
| 296 | + return implode(', ', $this->toAddresses); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Returns a simple email message without attachments |
|
| 301 | + * |
|
| 302 | + * @return string email message |
|
| 303 | + */ |
|
| 304 | + protected function buildMessage() |
|
| 305 | + { |
|
| 306 | + if (empty($this->plainMessage) && empty($this->htmlMessage)) { |
|
| 307 | + return ''; |
|
| 308 | + } |
|
| 309 | + if (!empty($this->plainMessage) && empty($this->htmlMessage)) { |
|
| 310 | + return $this->plainMessage; |
|
| 311 | + } |
|
| 312 | + if (empty($this->plainMessage) && !empty($this->htmlMessage)) { |
|
| 313 | + return $this->htmlMessage; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + $message = array(); |
|
| 317 | + array_push($message, "--{$this->boundaryAlternative}"); |
|
| 318 | + $message += $this->buildPlainMessageHeaders(); |
|
| 319 | + array_push($message, $this->plainMessage); |
|
| 320 | + array_push($message, "--{$this->boundaryAlternative}"); |
|
| 321 | + $message += $this->buildHtmlMessageHeaders(); |
|
| 322 | + array_push($message, $this->htmlMessage); |
|
| 323 | + array_push($message, "--{$this->boundaryAlternative}--"); |
|
| 324 | + |
|
| 325 | + return implode(self::LINE_BREAK, $message); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Build multi-part message with attachments |
|
| 330 | + * |
|
| 331 | + * @return string email message |
|
| 332 | + */ |
|
| 333 | + protected function buildMessageWithAttachments() |
|
| 334 | + { |
|
| 335 | + $message = array(); |
|
| 336 | + |
|
| 337 | + if (!empty($this->plainMessage) || !empty($this->htmlMessage)) { |
|
| 338 | + array_push($message, "--{$this->boundaryMixed}"); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + if (!empty($this->plainMessage) && !empty($this->htmlMessage)) { |
|
| 342 | + array_push($message, "Content-Type: multipart/alternative; boundary={$this->boundaryAlternative}"); |
|
| 343 | + array_push($message, ''); |
|
| 344 | + array_push($message, "--{$this->boundaryAlternative}"); |
|
| 345 | + $message += $this->buildPlainMessageHeaders(); |
|
| 346 | + array_push($message, $this->plainMessage); |
|
| 347 | + array_push($message, "--{$this->boundaryAlternative}"); |
|
| 348 | + $message += $this->buildHtmlMessageHeaders(); |
|
| 349 | + array_push($message, $this->htmlMessage); |
|
| 350 | + array_push($message, "--{$this->boundaryAlternative}--"); |
|
| 351 | + array_push($message, ''); |
|
| 352 | + } elseif (!empty($this->plainMessage)) { |
|
| 353 | + $message += $this->buildPlainMessageHeaders(); |
|
| 354 | + array_push($message, $this->plainMessage); |
|
| 355 | + } elseif (!empty($this->htmlMessage)) { |
|
| 356 | + $message += $this->buildHtmlMessageHeaders(); |
|
| 357 | + array_push($message, $this->htmlMessage); |
|
| 358 | + } |
|
| 359 | + foreach ($this->attachments as $attachment) { |
|
| 360 | + array_push($message, "--{$this->boundaryMixed}"); |
|
| 361 | + array_push($message, "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\""); |
|
| 362 | + array_push($message, 'Content-Transfer-Encoding: base64'); |
|
| 363 | + array_push($message, 'Content-Disposition: attachment'); |
|
| 364 | + array_push($message, ''); |
|
| 365 | + array_push($message, $this->buildAttachmentContent($attachment['path'])); |
|
| 366 | + } |
|
| 367 | + array_push($message, "--{$this->boundaryMixed}--"); |
|
| 368 | + |
|
| 369 | + return implode(self::LINE_BREAK, $message); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Shared holder for the plain message header |
|
| 375 | + * |
|
| 376 | + * @return array |
|
| 377 | + */ |
|
| 378 | + protected function getPlainMessageHeader() |
|
| 379 | + { |
|
| 380 | + return array( |
|
| 381 | + 'Content-Type: text/plain; charset="iso-8859"', |
|
| 382 | + 'Content-Transfer-Encoding: 7bit', |
|
| 383 | + '', |
|
| 384 | + ); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Shared holder for the html message header |
|
| 389 | + * |
|
| 390 | + * @return array |
|
| 391 | + */ |
|
| 392 | + protected function getHtmlMessageHeader() |
|
| 393 | + { |
|
| 394 | + return array( |
|
| 395 | + 'Content-Type: text/html; charset="iso-8859-1"', |
|
| 396 | + 'Content-Transfer-Encoding: 7bit', |
|
| 397 | + '', |
|
| 398 | + ); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Builder for the additional headers needed for multipart emails |
|
| 403 | + * |
|
| 404 | + * @return string headers needed for multipart |
|
| 405 | + */ |
|
| 406 | + protected function buildHeaders() |
|
| 407 | + { |
|
| 408 | + $headers = array(); |
|
| 409 | + foreach ($this->headers as $key => $value) { |
|
| 410 | + if ($key == 'CC' || $key == 'BCC') { |
|
| 411 | + $value = implode(', ', $value); |
|
| 412 | + } |
|
| 413 | + array_push($headers, sprintf('%s: %s', $key, $value)); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + if (!empty($this->attachments)) { |
|
| 417 | + array_push( |
|
| 418 | + $headers, |
|
| 419 | + "Content-Type: multipart/mixed; boundary=\"{$this->boundaryMixed}\"" |
|
| 420 | + ); |
|
| 421 | + } elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) { |
|
| 422 | + array_push( |
|
| 423 | + $headers, |
|
| 424 | + "Content-Type: multipart/alternative; boundary=\"{$this->boundaryAlternative}\"" |
|
| 425 | + ); |
|
| 426 | + } elseif (!empty($this->htmlMessage)) { |
|
| 427 | + array_push( |
|
| 428 | + $headers, |
|
| 429 | + 'Content-type: text/html; charset="iso-8859-1"' |
|
| 430 | + ); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + return implode(self::LINE_BREAK, $headers); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * File reader for attachments |
|
| 438 | + * |
|
| 439 | + * @param string $path filepath of the attachment |
|
| 440 | + * |
|
| 441 | + * @return string binary representation of file, base64'd |
|
| 442 | + */ |
|
| 443 | + protected function buildAttachmentContent($path) |
|
| 444 | + { |
|
| 445 | + if (!file_exists($path)) { |
|
| 446 | + $this->logger->error("Could not find file {$path} for attaching to Archangel mail."); |
|
| 447 | + return ''; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + $handle = fopen($path, 'r'); |
|
| 451 | + $contents = fread($handle, filesize($path)); |
|
| 452 | + fclose($handle); |
|
| 453 | + |
|
| 454 | + $contents = base64_encode($contents); |
|
| 455 | + $contents = chunk_split($contents); |
|
| 456 | + return $contents; |
|
| 457 | + } |
|
| 459 | 458 | } |