@@ -229,19 +229,14 @@ protected function get_recipients()
229229 $ recipients_header_to [] = $ to [0 ];
230230 }
231231 }
232+ $ recipients_header_to = implode (', ' , $ recipients_header_to );
232233
233234 // include bcc to recipients
234235 // sparkposts recipients list acts as bcc by default
235- $ bcc = $ this ->get_bcc (implode (', ' , $ recipients_header_to ));
236- if (!empty ($ bcc )) {
237- $ recipients = array_merge ($ recipients , $ bcc );
238- }
236+ $ recipients = array_merge ($ recipients , $ this ->get_bcc ($ recipients_header_to ));
239237
240- // include cc to recipients
241- $ cc = $ this ->get_cc ();
242- if (!empty ($ cc )) {
243- $ recipients = array_merge ($ recipients , $ cc );
244- }
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 ));
245240
246241 return $ recipients ;
247242 }
@@ -286,6 +281,11 @@ protected function build_recipient($email, $name = '', $header_to = '') {
286281
287282 if (!empty ($ header_to )) {
288283 $ 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 ' ]);
289289 }
290290
291291 return $ recipient ;
@@ -306,17 +306,32 @@ protected function get_bcc($header_to)
306306
307307 /**
308308 * Returns the list of CC recipients
309+ * @header_to string Optional, shouldn't be used for setting CC in headers
309310 * @return array
310311 */
311- protected function get_cc ()
312+ protected function get_cc ($ header_to = '' )
312313 {
313314 $ cc = array ();
314315 foreach ($ this ->getCcAddresses () as $ ccAddress ) {
315- $ cc [] = $ this ->build_recipient ($ ccAddress [0 ], $ ccAddress [1 ]);
316+ $ cc [] = $ this ->build_recipient ($ ccAddress [0 ], $ ccAddress [1 ], $ header_to );
316317 }
317318 return $ cc ;
318319 }
319320
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+
320335 /**
321336 * Returns a collection that can be sent as headers in body
322337 * @return array
@@ -343,8 +358,9 @@ protected function get_headers()
343358 }
344359
345360 // include cc in header
361+ $ cc = $ this ->get_cc ();
346362 if (!empty ($ cc )) {
347- $ formatted_headers ['CC ' ] = $ cc = $ this ->get_cc ( );
363+ $ formatted_headers ['CC ' ] = $ this ->stringify_recipients ( $ cc );
348364 }
349365
350366 return $ formatted_headers ;
0 commit comments