@@ -44,7 +44,6 @@ function sparkpostSend()
4444 'timeout ' => 15 ,
4545 'headers ' => $ this ->get_request_headers (),
4646 'body ' => json_encode ($ this ->get_request_body ())
47-
4847 );
4948
5049 $ http = _wp_http_get_object ();
@@ -218,14 +217,27 @@ protected function handle_response($response)
218217 protected function get_recipients ()
219218 {
220219 $ recipients = array ();
220+ $ recipients_header_to = array ();
221221
222222 foreach ($ this ->to as $ to ) {
223- $ recipients [] = array (
224- 'address ' => array (
225- 'email ' => $ to [0 ],
226- 'name ' => $ to [1 ]
227- ));
223+ $ recipients [] = $ this ->build_recipient ($ to [0 ], $ to [1 ]);
224+
225+ // prepare for header_to
226+ if (!empty ($ to [1 ])) {
227+ $ recipients_header_to [] = sprintf ('%s <%s> ' , $ to [1 ], $ to [0 ]);
228+ } else {
229+ $ recipients_header_to [] = $ to [0 ];
230+ }
228231 }
232+ $ recipients_header_to = implode (', ' , $ recipients_header_to );
233+
234+ // include bcc to recipients
235+ // sparkposts recipients list acts as bcc by default
236+ $ recipients = array_merge ($ recipients , $ this ->get_bcc ($ recipients_header_to ));
237+
238+ // include cc to recipients, they need to included in recipients and in headers (refer to get_headers method)
239+ $ recipients = array_merge ($ recipients , $ this ->get_cc ($ recipients_header_to ));
240+
229241 return $ recipients ;
230242 }
231243
@@ -259,6 +271,67 @@ protected function get_reply_to()
259271 return implode (', ' , $ replyTos );
260272 }
261273
274+ protected function build_recipient ($ email , $ name = '' , $ header_to = '' ) {
275+ $ recipient = array (
276+ 'address ' => array (
277+ 'email ' => $ email ,
278+ 'name ' => $ name ,
279+ )
280+ );
281+
282+ if (!empty ($ header_to )) {
283+ $ recipient ['address ' ]['header_to ' ] = $ header_to ;
284+ /* if header_to is like 'Name <email>', then having name attribute causes
285+ showing weird display of name in the delivered mail. So, let's remove it
286+ when header_to is set.
287+ */
288+ unset($ recipient ['address ' ]['name ' ]);
289+ }
290+
291+ return $ recipient ;
292+ }
293+
294+ /**
295+ * Returns the list of BCC recipients
296+ * @return array
297+ */
298+ protected function get_bcc ($ header_to )
299+ {
300+ $ bcc = array ();
301+ foreach ($ this ->getBccAddresses () as $ bccAddress ) {
302+ $ bcc [] = $ this ->build_recipient ($ bccAddress [0 ], $ bccAddress [1 ], $ header_to );
303+ }
304+ return $ bcc ;
305+ }
306+
307+ /**
308+ * Returns the list of CC recipients
309+ * @header_to string Optional, shouldn't be used for setting CC in headers
310+ * @return array
311+ */
312+ protected function get_cc ($ header_to = '' )
313+ {
314+ $ cc = array ();
315+ foreach ($ this ->getCcAddresses () as $ ccAddress ) {
316+ $ cc [] = $ this ->build_recipient ($ ccAddress [0 ], $ ccAddress [1 ], $ header_to );
317+ }
318+ return $ cc ;
319+ }
320+
321+ protected function stringify_recipients ($ recipients ) {
322+ $ recipients_list = array ();
323+
324+ foreach ($ recipients as $ recipient ) {
325+ if (!empty ($ recipient ['address ' ]['name ' ])){
326+ $ recipients_list [] = sprintf ('%s <%s> ' , $ recipient ['address ' ]['name ' ], $ recipient ['address ' ]['email ' ]);
327+ } else {
328+ $ recipients_list [] = $ recipient ['address ' ]['email ' ];
329+ }
330+ };
331+
332+ return implode (', ' , $ recipients_list );
333+ }
334+
262335 /**
263336 * Returns a collection that can be sent as headers in body
264337 * @return array
@@ -271,18 +344,25 @@ protected function get_headers()
271344 );
272345 $ headers = $ this ->createHeader ();
273346
274- $ formatted_headers = new StdClass ();
347+
348+ $ formatted_headers = array ();
275349 // split by line separator
276350 foreach (explode ($ this ->LE , $ headers ) as $ line ) {
277351
278352 $ splitted_line = explode (': ' , $ line );
279353 $ key = trim ($ splitted_line [0 ]);
280354
281355 if (!in_array ($ key , $ unsupported_headers ) && !empty ($ key ) && !empty ($ splitted_line [1 ])) {
282- $ formatted_headers->{ $ key} = trim ($ splitted_line [1 ]);
356+ $ formatted_headers[ $ key] = trim ($ splitted_line [1 ]);
283357 }
284358 }
285359
360+ // include cc in header
361+ $ cc = $ this ->get_cc ();
362+ if (!empty ($ cc )) {
363+ $ formatted_headers ['CC ' ] = $ this ->stringify_recipients ($ cc );
364+ }
365+
286366 return $ formatted_headers ;
287367 }
288368}
0 commit comments