| 1 |
<?php |
| 2 |
|
| 3 |
namespace SendWP; |
| 4 |
|
| 5 |
use SendWP\API\Request; |
| 6 |
|
| 7 |
/** |
| 8 |
* A decorater for sending API based transactional email. |
| 9 |
* |
| 10 |
* @TODO Maybe extract formatting methods. |
| 11 |
*/ |
| 12 |
class Mailer implements MailerInterface |
| 13 |
{ |
| 14 |
protected $phpmailer; |
| 15 |
protected $request; |
| 16 |
|
| 17 |
public static function factory(&$phpmailer) |
| 18 |
{ |
| 19 |
$request = \SendWP\API\Request::create('postmaster'); |
| 20 |
$phpmailer = new self($phpmailer, $request); |
| 21 |
return $phpmailer; |
| 22 |
} |
| 23 |
|
| 24 |
public function __construct($phpmailer, Request $request) |
| 25 |
{ |
| 26 |
$this->phpmailer = $phpmailer; |
| 27 |
$this->request = $request; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Check for property/method in $phpmailer. |
| 32 |
*/ |
| 33 |
public function __get($name) |
| 34 |
{ |
| 35 |
if (property_exists($this->phpmailer, $name)) { |
| 36 |
return $this->phpmailer->$name; |
| 37 |
} |
| 38 |
return ''; |
| 39 |
} |
| 40 |
|
| 41 |
public function __call($name, $arguments) |
| 42 |
{ |
| 43 |
if (method_exists($this->phpmailer, $name)) { |
| 44 |
return call_user_func([ $this->phpmailer, $name ], $arguments); |
| 45 |
} |
| 46 |
return null; |
| 47 |
} |
| 48 |
|
| 49 |
public function getAttachments() |
| 50 |
{ |
| 51 |
$attachments = $this->phpmailer->getAttachments(); |
| 52 |
|
| 53 |
// Format the attachments, per service requirement. |
| 54 |
$attachments = array_map([ $this, 'formatAttachment' ], $attachments); |
| 55 |
|
| 56 |
return $attachments; |
| 57 |
} |
| 58 |
|
| 59 |
public function send() |
| 60 |
{ |
| 61 |
$to_emails = array_map([ $this, 'formatEmails' ], $this->getToAddresses()); |
| 62 |
$cc_emails = array_map([ $this, 'formatEmails' ], $this->getCcAddresses()); |
| 63 |
$bcc_emails = array_map([ $this, 'formatEmails' ], $this->getBccAddresses()); |
| 64 |
|
| 65 |
$args = [ |
| 66 |
'body' => [ |
| 67 |
'to' => json_encode( (array) $to_emails ), |
| 68 |
'from' => $this->From, |
| 69 |
'from_name' => $this->FromName, |
| 70 |
'subject' => $this->Subject, |
| 71 |
], |
| 72 |
]; |
| 73 |
|
| 74 |
if('text/html' == $this->ContentType) { |
| 75 |
$args[ 'body' ][ 'body' ] = $this->Body; |
| 76 |
$args[ 'body' ][ 'altbody' ] = $this->AltBody; |
| 77 |
} else { |
| 78 |
$args[ 'body' ][ 'altbody' ] = $this->Body; |
| 79 |
} |
| 80 |
|
| 81 |
if($reply_to = $this->getReplyToAddresses()){ |
| 82 |
/** |
| 83 |
* $reply_to is an array of arrays. |
| 84 |
* [ [ $address, $name ], [...] ] |
| 85 |
* To get a string value for the address, we need to reset it twice. |
| 86 |
*/ |
| 87 |
$reply_to = reset($reply_to); |
| 88 |
$args['body']['reply_to'] = reset($reply_to); |
| 89 |
} |
| 90 |
|
| 91 |
if( ! empty( $cc_emails ) ) { |
| 92 |
$args[ 'body' ][ 'cc' ] = json_encode( (array) $cc_emails ); |
| 93 |
} |
| 94 |
|
| 95 |
if( ! empty( $bcc_emails ) ) { |
| 96 |
$args[ 'body' ][ 'bcc' ] = json_encode( (array) $bcc_emails ); |
| 97 |
} |
| 98 |
|
| 99 |
if ($attachments = $this->getAttachments()) { |
| 100 |
$args[ 'body' ][ 'attachments' ] = $attachments; |
| 101 |
} |
| 102 |
|
| 103 |
// Response is empty... |
| 104 |
$response = $this->request->post($args); |
| 105 |
|
| 106 |
return true; // Sent by the Service. |
| 107 |
} |
| 108 |
|
| 109 |
protected function formatEmails($emails) |
| 110 |
{ |
| 111 |
return reset($emails); |
| 112 |
} |
| 113 |
|
| 114 |
protected function formatAttachment($attachment) |
| 115 |
{ |
| 116 |
return [ |
| 117 |
'filename' => $attachment[1], // $filename per PHPMailer docs. |
| 118 |
'filedata' => file_get_contents($attachment[0]) // $path per PHPMailer docs. |
| 119 |
]; |
| 120 |
} |
| 121 |
} |
| 122 |
|