Skip to content
Snippets Groups Projects
Commit 64344002 authored by mattwire's avatar mattwire
Browse files

Fix Bcc mail

parent 5ec39f02
No related branches found
No related tags found
No related merge requests found
......@@ -71,13 +71,55 @@ class CRM_Ses_Mail extends Mail {
}
}
// Sanitize and prepare headers for transmission
if (!is_array($headers)) {
return new PEAR_Error('SES: $headers must be an array');
}
$ses_access_key = Civi::settings()->get('ses_access_key');
$ses_secret_key = Civi::settings()->get('ses_secret_key');
$ses_region = Civi::settings()->get('ses_region');
// Sanitize and prepare headers for transmission
if (!is_array($headers)) {
return new PEAR_Error('SES: $headers must be an array');
// $headers['Bcc'] gets unset before calling this function because it assumes it won't get removed by the mailer.
// But SES needs the full headers and then it removes it when processing.
// So we have to add it back in. We can get it from the last element in the $recipients array:
// $recipients = [
// 0: To (single email)
// 1: Cc (comma separated string)
// 2: Bcc (comma separated string)
// ]
// BUT BE CAREFUL! To or Cc might not be defined so it won't always be element 2. It will always be the last element
// but we don't know if we actually have any Bcc addresses without checking the To, Cc headers first.
// Eg. if you have To+Bcc then $recipients[0] is To and $recipients[1] is Bcc but if you have To+Cc then $recipients[1]
// is Cc and if $recipients[2] is not set you have no Bcc and don't need to add the header... confused yet?
// Work out which $recipients keys represent To, Cc and Bcc
if (array_key_exists('To', $headers)) {
$newRecipients['To'] = array_shift($recipients);
}
if (array_key_exists('Cc', $headers)) {
$newRecipients['Cc'] = array_shift($recipients);
}
$newRecipients['Bcc'] = array_shift($recipients);
if (!empty($newRecipients['Bcc'])) {
// The order of headers might not matter (I didn't check if SES cares or not).
// But we insert the Bcc header in the right place anyway. ie. after Cc or To or From.
if (array_key_exists('Cc', $headers)) {
$insertBccAfterHeader = 'Cc';
}
elseif (array_key_exists('To', $headers)) {
$insertBccAfterHeader = 'To';
}
else {
$insertBccAfterHeader = 'From';
}
foreach ($headers as $headerKey => $headerValue) {
$newHeaders[$headerKey] = $headerValue;
if ($headerKey === $insertBccAfterHeader) {
$newHeaders['Bcc'] = $newRecipients['Bcc'];
}
}
$headers = $newHeaders;
}
$this->_sanitizeHeaders($headers);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment