@@ -27,6 +27,13 @@ class ElasticTransport extends Transport
2727 */
2828 protected $ account ;
2929
30+
31+ /**
32+ * If the email is transactional.
33+ * @var string
34+ */
35+ protected $ transactional ;
36+
3037 /**
3138 * THe Elastic Email API end-point.
3239 * @var string
@@ -42,11 +49,12 @@ class ElasticTransport extends Transport
4249 *
4350 * @return void
4451 */
45- public function __construct (ClientInterface $ client , $ key , $ account )
52+ public function __construct (ClientInterface $ client , $ config )
4653 {
4754 $ this ->client = $ client ;
48- $ this ->key = $ key ;
49- $ this ->account = $ account ;
55+ $ this ->key = $ config ['key ' ];
56+ $ this ->account = $ config ['account ' ];
57+ $ this ->transactional = $ config ['transactional ' ];
5058 }
5159
5260 /**
@@ -56,6 +64,11 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = NUL
5664 {
5765 $ this ->beforeSendPerformed ($ message );
5866
67+ $ isTransactional = $ this ->transactional ;
68+ if (!is_null ($ message ->getHeaders ()->get ('X-Transactional ' ))){
69+ $ isTransactional = $ message ->getHeaders ()->get ('X-Transactional ' )->getFieldBody ();
70+ }
71+
5972 $ data = [
6073 'api_key ' => $ this ->key ,
6174 'account ' => $ this ->account ,
@@ -66,13 +79,31 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = NUL
6679 'msgFromName ' => $ this ->getFromAddress ($ message )['name ' ],
6780 'from ' => $ this ->getFromAddress ($ message )['email ' ],
6881 'fromName ' => $ this ->getFromAddress ($ message )['name ' ],
82+ 'replyTo ' => $ this ->getReplyToAddress ($ message )['email ' ],
83+ 'replyToName ' => $ this ->getReplyToAddress ($ message )['name ' ],
6984 'to ' => $ this ->getEmailAddresses ($ message ),
7085 'subject ' => $ message ->getSubject (),
7186 'body_html ' => $ message ->getBody (),
72- 'body_text ' => $ this ->getText ($ message )
87+ 'body_text ' => $ this ->getText ($ message ),
88+ 'isTransactional ' => $ isTransactional
7389 ];
7490
7591 $ attachments = $ message ->getChildren ();
92+
93+ $ response = $ this ->sendEmail ($ data , $ attachments );
94+
95+ // Inject the response data into the message headers.
96+ if ($ response ) {
97+ $ message ->getHeaders ()->addTextHeader ('X-Message-ID ' , $ response ->messageid );
98+ $ message ->getHeaders ()->addTextHeader ('X-Transaction-ID ' , $ response ->transactionid );
99+ }
100+
101+ $ this ->sendPerformed ($ message );
102+
103+ return $ response ;
104+ }
105+
106+ protected function sendEmail (array $ data , array $ attachments = []){
76107 if (!empty ($ attachments )) {
77108 $ options ['multipart ' ] = $ this ->parseMultipart ($ attachments , $ data );
78109 } else {
@@ -87,20 +118,25 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = NUL
87118 $ url = str_replace ('https: ' , 'http: ' , $ url );
88119 }
89120
90- $ result = $ this ->client ->request ('POST ' , $ url , $ options );
121+ try {
122+ $ result = $ this ->client ->request ('POST ' , $ url , $ options );
123+ } catch (\Exception $ e ) {
124+ throw $ e ;
125+ }
91126
92- // Inject the response data into the message instance.
93127 if ($ result ->getStatusCode () == 200 ) {
94128 $ response = $ result ->getBody ()->getContents ();
95129 $ response = json_decode ($ response );
130+
96131 if ($ response ->success ) {
97- $ message ->elasticemail = $ response ->data ;
132+ return $ response ->data ;
133+ }
134+ else {
135+ throw new \Exception ($ response ->error );
98136 }
99137 }
100138
101- $ this ->sendPerformed ($ message );
102-
103- return $ result ;
139+ return false ;
104140 }
105141
106142 /**
@@ -153,6 +189,23 @@ protected function getEmailAddresses(Swift_Mime_SimpleMessage $message, $method
153189 return '' ;
154190 }
155191
192+ /**
193+ * @param \Swift_Mime_Message $message
194+ *
195+ * @return array
196+ */
197+ protected function getReplyToAddress (Swift_Mime_SimpleMessage $ message )
198+ {
199+ if (!$ message ->getReplyTo ()) {
200+ return $ this ->getFromAddress ($ message );
201+ }
202+
203+ return [
204+ 'email ' => array_keys ($ message ->getReplyTo ())[0 ],
205+ 'name ' => array_values ($ message ->getReplyTo ())[0 ],
206+ ];
207+ }
208+
156209 /**
157210 * Parse the attachments and add them to the multipart array.
158211 *
0 commit comments