Skip to content

Commit 19bdfc3

Browse files
committed
Add cc, header_to for bcc, improved code
1 parent 6cab242 commit 19bdfc3

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

mailer.http.class.php

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -217,24 +217,32 @@ protected function handle_response($response)
217217
protected function get_recipients()
218218
{
219219
$recipients = array();
220+
$recipients_header_to = array();
220221

221222
foreach ($this->to as $to) {
222-
$recipients[] = array(
223-
'address' => array(
224-
'email' => $to[0],
225-
'name' => $to[1]
226-
)
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
}
229232

230-
// add bcc to recipients
233+
// include bcc to recipients
231234
// sparkposts recipients list acts as bcc by default
232-
$bcc = $this->get_bcc();
233-
235+
$bcc = $this->get_bcc(implode(',', $recipients_header_to));
234236
if(!empty($bcc)) {
235237
$recipients = array_merge($recipients, $bcc);
236238
}
237239

240+
// include cc to recipients
241+
$cc = $this->get_cc();
242+
if(!empty($cc)) {
243+
$recipients = array_merge($recipients, $cc);
244+
}
245+
238246
return $recipients;
239247
}
240248

@@ -268,24 +276,47 @@ protected function get_reply_to()
268276
return implode(',', $replyTos);
269277
}
270278

279+
protected function build_recipient($email, $name = '', $header_to = '') {
280+
$recipient = array(
281+
'address' => array(
282+
'email' => $email,
283+
'name' => $name,
284+
)
285+
);
286+
287+
if(!empty($header_to)) {
288+
$recipient['address']['header_to'] = $header_to;
289+
}
290+
291+
return $recipient;
292+
}
293+
271294
/**
272-
* Returns the list of BCC headers
295+
* Returns the list of BCC recipients
273296
* @return array
274297
*/
275-
protected function get_bcc()
298+
protected function get_bcc($header_to)
276299
{
277300
$bcc = array();
278301
foreach($this->getBccAddresses() as $bccAddress) {
279-
$bcc[] = array(
280-
'address' => array(
281-
'email' => $bccAddress[0],
282-
'name' => $bccAddress[1],
283-
)
284-
);
302+
$bcc[] = $this->build_recipient($bccAddress[0], $bccAddress[1], $header_to);
285303
}
286304
return $bcc;
287305
}
288306

307+
/**
308+
* Returns the list of CC recipients
309+
* @return array
310+
*/
311+
protected function get_cc()
312+
{
313+
$cc = array();
314+
foreach($this->getCcAddresses() as $ccAddress) {
315+
$cc[] = $this->build_recipient($ccAddress[0], $ccAddress[1]);
316+
}
317+
return $cc;
318+
}
319+
289320
/**
290321
* Returns a collection that can be sent as headers in body
291322
* @return array
@@ -299,18 +330,23 @@ protected function get_headers()
299330
$headers = $this->createHeader();
300331

301332

302-
$formatted_headers = new StdClass();
333+
$formatted_headers = array();
303334
// split by line separator
304335
foreach (explode($this->LE, $headers) as $line) {
305336

306337
$splitted_line = explode(': ', $line);
307338
$key = trim($splitted_line[0]);
308339

309340
if (!in_array($key, $unsupported_headers) && !empty($key) && !empty($splitted_line[1])) {
310-
$formatted_headers->{$key} = trim($splitted_line[1]);
341+
$formatted_headers[$key] = trim($splitted_line[1]);
311342
}
312343
}
313344

345+
// include cc in header
346+
if(!empty($cc)) {
347+
$formatted_headers['CC'] = $cc = $this->get_cc();
348+
}
349+
314350
return $formatted_headers;
315351
}
316352
}

0 commit comments

Comments
 (0)