@@ -44,7 +44,6 @@ function sparkpostSend()
44
44
'timeout ' => 15 ,
45
45
'headers ' => $ this ->get_request_headers (),
46
46
'body ' => json_encode ($ this ->get_request_body ())
47
-
48
47
);
49
48
50
49
$ http = _wp_http_get_object ();
@@ -218,14 +217,27 @@ protected function handle_response($response)
218
217
protected function get_recipients ()
219
218
{
220
219
$ recipients = array ();
220
+ $ recipients_header_to = array ();
221
221
222
222
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
+ }
228
231
}
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
+
229
241
return $ recipients ;
230
242
}
231
243
@@ -259,6 +271,67 @@ protected function get_reply_to()
259
271
return implode (', ' , $ replyTos );
260
272
}
261
273
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
+
262
335
/**
263
336
* Returns a collection that can be sent as headers in body
264
337
* @return array
@@ -271,18 +344,25 @@ protected function get_headers()
271
344
);
272
345
$ headers = $ this ->createHeader ();
273
346
274
- $ formatted_headers = new StdClass ();
347
+
348
+ $ formatted_headers = array ();
275
349
// split by line separator
276
350
foreach (explode ($ this ->LE , $ headers ) as $ line ) {
277
351
278
352
$ splitted_line = explode (': ' , $ line );
279
353
$ key = trim ($ splitted_line [0 ]);
280
354
281
355
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 ]);
283
357
}
284
358
}
285
359
360
+ // include cc in header
361
+ $ cc = $ this ->get_cc ();
362
+ if (!empty ($ cc )) {
363
+ $ formatted_headers ['CC ' ] = $ this ->stringify_recipients ($ cc );
364
+ }
365
+
286
366
return $ formatted_headers ;
287
367
}
288
368
}
0 commit comments