|
6 | 6 | use JustCoded\FormHandler\DataObjects\EmailAttachment; |
7 | 7 | use JustCoded\FormHandler\DataObjects\MailMessage; |
8 | 8 | use Mandrill; |
| 9 | +use Mandrill_Error; |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * Class MandrillMailer |
|
15 | 16 | class MandrillMailer extends DataObject implements MailerInterface |
16 | 17 | { |
17 | 18 | /** |
18 | | - * @var string |
| 19 | + * Attachments size limit |
| 20 | + * |
| 21 | + * @var int |
19 | 22 | */ |
20 | | - protected $host; |
| 23 | + protected $attachmentsSizeLimit = 8000000; |
21 | 24 |
|
22 | 25 | /** |
| 26 | + * User from user config |
| 27 | + * |
23 | 28 | * @var string |
24 | 29 | */ |
25 | 30 | protected $user; |
26 | 31 |
|
27 | 32 | /** |
| 33 | + * Password from user config |
| 34 | + * |
28 | 35 | * @var string |
29 | 36 | */ |
30 | 37 | protected $password; |
31 | 38 |
|
32 | 39 | /** |
33 | | - * @var string|bool |
34 | | - */ |
35 | | - protected $protocol = false; |
36 | | - |
37 | | - /** |
38 | | - * @var string |
39 | | - */ |
40 | | - protected $port = 25; |
41 | | - |
42 | | - /** |
| 40 | + * List of errors |
| 41 | + * |
43 | 42 | * @var array |
44 | 43 | */ |
45 | 44 | protected $errors = array(); |
46 | 45 |
|
47 | 46 | /** |
48 | | - * Your api key |
| 47 | + * Sending form |
49 | 48 | * |
50 | | - * @var string |
| 49 | + * @param MailMessage $message Mailer message |
| 50 | + * |
| 51 | + * @return array|bool |
51 | 52 | */ |
52 | | - protected $apiKey; |
53 | | - |
54 | 53 | public function send(MailMessage $message) |
55 | 54 | { |
56 | | - $mail = new Mandrill(true); // Passing `true` enables exceptions. |
57 | 55 | try { |
58 | | - // Enable SMTP if host is set. |
59 | | - if ( ! empty($this->host)) { |
60 | | - $mail->SMTPDebug = 0; |
61 | | - $mail->isSMTP(); |
62 | | - $mail->Host = $this->host; |
63 | | - $mail->Port = $this->port; |
64 | | - if ( ! empty($this->user)) { |
65 | | - $mail->SMTPAuth = true; |
66 | | - $mail->Username = $this->user; |
67 | | - $mail->Password = $this->password; |
68 | | - } |
69 | | - if ( ! empty($this->protocol)) { |
70 | | - $mail->SMTPSecure = $this->protocol; |
71 | | - } |
72 | | - } |
| 56 | + $mandrill = new Mandrill($this->password); |
73 | 57 |
|
74 | | - // Set From. |
75 | | - if ($address = $message->getFrom()) { |
76 | | - $mail->setFrom($address->getEmail(), $address->getName()); |
77 | | - } |
| 58 | + $mandrillMessage = array( |
| 59 | + 'html' => $message->getBody(), |
| 60 | + 'text' => 'Example text content', |
| 61 | + 'subject' => $message->getSubject(), |
| 62 | + 'from_email' => $message->getFrom()->getEmail(), |
| 63 | + 'from_name' => $message->getFrom()->getName(), |
| 64 | + ); |
78 | 65 |
|
79 | 66 | // Recipients. |
80 | 67 | if ($to = $message->getTo()) { |
| 68 | + $toArray = []; |
81 | 69 | foreach ($to as $address) { |
82 | | - $mail->addAddress($address->getEmail(), $address->getName()); |
| 70 | + $toArray[] = [ |
| 71 | + 'email' => $address->getEmail(), |
| 72 | + 'name' => $address->getName(), |
| 73 | + 'type' => 'to' |
| 74 | + ]; |
83 | 75 | } |
84 | 76 |
|
85 | | - } |
| 77 | + if ($cc = $message->getCc()) { |
| 78 | + foreach ($cc as $address) { |
| 79 | + $toArray[] = [ |
| 80 | + 'email' => $address->getEmail(), |
| 81 | + 'name' => $address->getName(), |
| 82 | + 'type' => 'cc' |
| 83 | + ]; |
| 84 | + } |
| 85 | + } |
86 | 86 |
|
87 | | - if ($cc = $message->getCc()) { |
88 | | - foreach ($cc as $address) { |
89 | | - $mail->addCC($address->getEmail(), $address->getName()); |
| 87 | + if ($bcc = $message->getBcc()) { |
| 88 | + foreach ($bcc as $address) { |
| 89 | + $toArray[] = [ |
| 90 | + 'email' => $address->getEmail(), |
| 91 | + 'name' => $address->getName(), |
| 92 | + 'type' => 'bcc' |
| 93 | + ]; |
| 94 | + } |
90 | 95 | } |
91 | 96 |
|
| 97 | + $mandrillMessage['to'] = $toArray; |
92 | 98 | } |
93 | 99 |
|
94 | | - if ($bcc = $message->getBcc()) { |
95 | | - foreach ($bcc as $address) { |
96 | | - $mail->addBCC($address->getEmail(), $address->getName()); |
| 100 | + // Attachments. |
| 101 | + if (0 < $message->getAttachmentsSize() && $message->getAttachmentsSize() < $this->attachmentsSizeLimit |
| 102 | + && $attachments = $message->getAttachments() |
| 103 | + ) { |
| 104 | + $attachmentsArray = []; |
| 105 | + foreach ($attachments as $attachment) { |
| 106 | + $attachmentsArray[] = [ |
| 107 | + 'type' => $attachment->type, |
| 108 | + 'name' => $attachment->name, |
| 109 | + 'content' => $attachment->getBase64() |
| 110 | + ]; |
97 | 111 | } |
98 | 112 |
|
| 113 | + $mandrillMessage['attachments'] = $attachmentsArray; |
99 | 114 | } |
100 | 115 |
|
101 | | - //Attachments |
102 | | - if ($attachments = $message->getFiles()) { |
103 | | - foreach ($attachments as $attachment) { |
104 | | - /** @var EmailAttachment $attachment */ |
105 | | - $mail->addAttachment($attachment->getPath(), $attachment->getName()); // Optional name |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - // Content. |
110 | | - $mail->Subject = $message->getSubject(); |
111 | | - if ($body = $message->getBody()) { |
112 | | - $mail->isHTML(true); |
113 | | - $mail->Body = $message->getBody(); |
114 | | - $mail->AltBody = $message->getAltBody(); |
115 | | - } else { |
116 | | - $mail->Body = $message->getAltBody(); |
117 | | - } |
| 116 | + $async = false; |
| 117 | + $ip_pool = 'Main Pool'; |
| 118 | + $send_at = date('Y-m-d h:i:s'); |
| 119 | + $result = $mandrill->messages->send($mandrillMessage, $async, $ip_pool, $send_at); |
118 | 120 |
|
119 | | - $this->errors = array(); |
120 | | - return $mail->send(); |
121 | | - } catch (PhpMailerException $e) { |
122 | | - $this->errors[] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo; |
| 121 | + return $result; |
| 122 | + } catch (Mandrill_Error $e) { |
| 123 | + $this->errors[] = 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage(); |
123 | 124 | return false; |
124 | 125 | } |
125 | 126 | } |
126 | 127 |
|
| 128 | + /** |
| 129 | + * Getting list of errors |
| 130 | + * |
| 131 | + * @return array |
| 132 | + */ |
127 | 133 | public function getErrors() |
128 | 134 | { |
129 | 135 | return $this->errors; |
|
0 commit comments