From ffc00c17f31aedc885b0c9b2d10ba7f5da9e42b5 Mon Sep 17 00:00:00 2001
From: Melissa Bent <melissa.bent@mediacurrent.com>
Date: Thu, 25 May 2017 19:18:09 -0600
Subject: [PATCH] Add support for CC and BCC email addresses in Mandrill API
 calls.

---
 src/MandrillService.php          | 53 ++++++++++++++++++++++++++++------------
 src/Plugin/Mail/MandrillMail.php | 18 +++++++++++++-
 2 files changed, 54 insertions(+), 17 deletions(-)

diff --git a/src/MandrillService.php b/src/MandrillService.php
index 224e8b1..d3fb368 100644
--- a/src/MandrillService.php
+++ b/src/MandrillService.php
@@ -81,26 +81,47 @@ class MandrillService implements MandrillServiceInterface {
   /**
    * Helper to generate an array of recipients.
    *
-   * @param mixed $receiver
-   *   a comma delimited list of email addresses in 1 of 2 forms:
-   *   user@domain.com
-   *   any number of names <user@domain.com>
+   * This function accepts and array of values keyed in the following way:
+   * $receiver = [
+   *   'to' => 'user@domain.com, any number of names <user@domain.com>',
+   *   'cc' => 'user@domain.com, any number of names <user@domain.com>',
+   *   'bcc' => 'user@domain.com, any number of names <user@domain.com>',
+   * ];
+   * The only required key is 'to'. The other values will automatically be
+   * discovered if present. The strings of email addresses could provide a
+   * single email address or many, depending on the needs of the application.
+   *
+   * This structure is in keeping with the Mandrill API documentation located
+   * here: https://mandrillapp.com/api/docs/messages.html.
+   *
+   * @param mixed $receivers
+   *   An array of comma delimited lists of email addresses.
    *
    * @return array
    *   array of email addresses
    */
-  public function getReceivers($receiver) {
-    $recipients = array();
-    $receiver_array = explode(',', $receiver);
-    foreach ($receiver_array as $email) {
-      if (preg_match(MANDRILL_EMAIL_REGEX, $email, $matches)) {
-        $recipients[] = array(
-          'email' => $matches[2],
-          'name' => $matches[1],
-        );
-      }
-      else {
-        $recipients[] = array('email' => $email);
+  public function getReceivers($receivers) {
+    // Check the input variable type to provide backward compatibility for
+    // when only a string of 'to' recipients are passed.
+    if (is_string($receivers)) {
+      $receivers = [
+        'to' => $receivers,
+      ];
+    }
+    $recipients = [];
+    foreach ($receivers as $type => $receiver) {
+      $receiver_array = explode(',', $receiver);
+      foreach ($receiver_array as $email) {
+        if (preg_match(MANDRILL_EMAIL_REGEX, $email, $matches)) {
+          $recipients[] = array(
+            'email' => $matches[2],
+            'name' => $matches[1],
+            'type' => $type,
+          );
+        }
+        else {
+          $recipients[] = array('email' => $email);
+        }
       }
     }
     return $recipients;
diff --git a/src/Plugin/Mail/MandrillMail.php b/src/Plugin/Mail/MandrillMail.php
index 59f3a57..cfcb57f 100644
--- a/src/Plugin/Mail/MandrillMail.php
+++ b/src/Plugin/Mail/MandrillMail.php
@@ -169,8 +169,24 @@ class MandrillMail implements MailInterface {
       // (This prevents double-attaching in the drupal_alter hook below.)
       unset($message['params']['attachments']);
     }
+    // Setup the list of recipients from the mail message and header data.
+    $receivers = ['to' => $message['to']];
+    if (isset($message['headers']['cc'])) {
+      $receivers['cc'] = $message['headers']['cc'];
+    }
+    if (isset($message['headers']['bcc'])) {
+      $receivers['bcc'] = $message['headers']['bcc'];
+    }
+    // Include the Start case versions of cc and bcc keys since PHP's array keys
+    // are case-sensitive.
+    if (isset($message['headers']['Cc'])) {
+      $receivers['cc'] = $message['headers']['Cc'];
+    }
+    if (isset($message['headers']['Bcc'])) {
+      $receivers['bcc'] = $message['headers']['Bcc'];
+    }
     // Extract an array of recipients.
-    $to = $this->mandrill->getReceivers($message['to']);
+    $to = $this->mandrill->getReceivers($receivers);
     // Account for the plaintext parameter provided by the mimemail module.
     $plain_text = empty($message['params']['plaintext']) ? MailFormatHelper::htmlToText($message['body']) : $message['params']['plaintext'];
     // Get metadata.
-- 
2.9.2

