Skip to content

Commit

Permalink
Merge pull request #46 from SparkPost/bcc
Browse files Browse the repository at this point in the history
Add cc/bcc support
  • Loading branch information
rajumsys authored Aug 11, 2016
2 parents 8ed594b + e25abca commit 9847bb6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
96 changes: 88 additions & 8 deletions mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function sparkpostSend()
'timeout' => 15,
'headers' => $this->get_request_headers(),
'body' => json_encode($this->get_request_body())

);

$http = _wp_http_get_object();
Expand Down Expand Up @@ -218,14 +217,27 @@ protected function handle_response($response)
protected function get_recipients()
{
$recipients = array();
$recipients_header_to = array();

foreach ($this->to as $to) {
$recipients[] = array(
'address' => array(
'email' => $to[0],
'name' => $to[1]
));
$recipients[] = $this->build_recipient($to[0], $to[1]);

// prepare for header_to
if(!empty($to[1])) {
$recipients_header_to[] = sprintf('%s <%s>', $to[1], $to[0]);
} else {
$recipients_header_to[] = $to[0];
}
}
$recipients_header_to = implode(',', $recipients_header_to);

// include bcc to recipients
// sparkposts recipients list acts as bcc by default
$recipients = array_merge($recipients, $this->get_bcc($recipients_header_to));

// include cc to recipients, they need to included in recipients and in headers (refer to get_headers method)
$recipients = array_merge($recipients, $this->get_cc($recipients_header_to));

return $recipients;
}

Expand Down Expand Up @@ -259,6 +271,67 @@ protected function get_reply_to()
return implode(',', $replyTos);
}

protected function build_recipient($email, $name = '', $header_to = '') {
$recipient = array(
'address' => array(
'email' => $email,
'name' => $name,
)
);

if(!empty($header_to)) {
$recipient['address']['header_to'] = $header_to;
/* if header_to is like 'Name <email>', then having name attribute causes
showing weird display of name in the delivered mail. So, let's remove it
when header_to is set.
*/
unset($recipient['address']['name']);
}

return $recipient;
}

/**
* Returns the list of BCC recipients
* @return array
*/
protected function get_bcc($header_to)
{
$bcc = array();
foreach($this->getBccAddresses() as $bccAddress) {
$bcc[] = $this->build_recipient($bccAddress[0], $bccAddress[1], $header_to);
}
return $bcc;
}

/**
* Returns the list of CC recipients
* @header_to string Optional, shouldn't be used for setting CC in headers
* @return array
*/
protected function get_cc($header_to = '')
{
$cc = array();
foreach($this->getCcAddresses() as $ccAddress) {
$cc[] = $this->build_recipient($ccAddress[0], $ccAddress[1], $header_to);
}
return $cc;
}

protected function stringify_recipients($recipients) {
$recipients_list = array();

foreach($recipients as $recipient) {
if(!empty($recipient['address']['name'])){
$recipients_list[] = sprintf('%s <%s>', $recipient['address']['name'], $recipient['address']['email']);
} else {
$recipients_list[] = $recipient['address']['email'];
}
};

return implode(',', $recipients_list);
}

/**
* Returns a collection that can be sent as headers in body
* @return array
Expand All @@ -271,18 +344,25 @@ protected function get_headers()
);
$headers = $this->createHeader();

$formatted_headers = new StdClass();

$formatted_headers = array();
// split by line separator
foreach (explode($this->LE, $headers) as $line) {

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

if (!in_array($key, $unsupported_headers) && !empty($key) && !empty($splitted_line[1])) {
$formatted_headers->{$key} = trim($splitted_line[1]);
$formatted_headers[$key] = trim($splitted_line[1]);
}
}

// include cc in header
$cc = $this->get_cc();
if(!empty($cc)) {
$formatted_headers['CC'] = $this->stringify_recipients($cc);
}

return $formatted_headers;
}
}
2 changes: 0 additions & 2 deletions wordpress-sparkpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@
add_filter('wp_mail', array($sp, 'init_sp_http_mailer'));
}
}


0 comments on commit 9847bb6

Please sign in to comment.