Skip to content
Snippets Groups Projects

Issue #3392183: When multiple recipients use different Personalization object for each of the recipient

Open Issue #3392183: When multiple recipients use different Personalization object for each of the recipient
1 file
+ 50
33
Compare changes
  • Side-by-side
  • Inline
+ 50
33
@@ -167,7 +167,6 @@ class SendGridMail implements MailInterface, ContainerFactoryPluginInterface {
protected function doMail(array $message): bool {
// Begin by creating instances of objects needed.
$sendgrid_message = new Mail();
$personalization0 = $sendgrid_message->getPersonalization();
$sandbox_mode = new SandBoxMode();
$site_config = $this->configFactory->get('system.site');
@@ -269,33 +268,12 @@ class SendGridMail implements MailInterface, ContainerFactoryPluginInterface {
}
$sendgrid_message->addCategories($categories);
$personalization0->setSubject($message['subject']);
$from = new From($data['from'], $data['fromname']);
unset($data);
# Set the from address and add a name if it exists.
$sendgrid_message->setFrom($from);
// If there are multiple recipients we have to explode and walk the values.
if (strpos($message['to'], ',')) {
$sendtosarry = explode(',', $message['to']);
foreach ($sendtosarry as $value) {
// Remove unnecessary spaces.
$value = trim($value);
$sendtoarrayparsed = $this->parseAddress($value);
$personalization0->addTo(new To($sendtoarrayparsed[0], isset($sendtoarrayparsed[1]) ? $sendtoarrayparsed[1] : NULL));
}
}
else {
$toaddrarray = $this->parseAddress($message['to']);
if (isset($toaddrarray[1])) {
$toname = $toaddrarray[1];
}
else {
$toname = NULL;
}
$personalization0->addTo(new To($toaddrarray[0], $toname));
}
// Empty array to process addresses from mail headers.
$address_cc_bcc = [];
@@ -340,7 +318,6 @@ class SendGridMail implements MailInterface, ContainerFactoryPluginInterface {
$sendgrid_message->addContent('text/plain', MailFormatHelper::wrapMail($body_plain));
break;
case 'multipart/alternative':
// Get the boundary ID from the Content-Type header.
$boundary = $this->getSubString($message['body'], 'boundary', '"', '"');
@@ -470,20 +447,67 @@ class SendGridMail implements MailInterface, ContainerFactoryPluginInterface {
}
}
}
// CC and BCC.
if (isset($address_cc_bcc) && array_key_exists('cc', $address_cc_bcc)) {
foreach ($address_cc_bcc['cc'] as $item) {
$personalization0->addCc(new Cc($item['mail'], $item['name']));
$sendgrid_message->addCc(new Cc($item['mail'], $item['name']));
}
}
if (isset($address_cc_bcc) && array_key_exists('bcc', $address_cc_bcc)) {
foreach ($address_cc_bcc['bcc'] as $item) {
$personalization0->addBcc(new Bcc($item['mail'], $item['name']));
$sendgrid_message->addBcc(new Bcc($item['mail'], $item['name']));
}
}
// -----------------------
// END - BCC and CC Address Handling
// -----------------------
// -----------------------
// Personalization Handling
// -----------------------
$sendgrid_message->setSubject($message['subject']);
if (strpos($message['to'], ',')) {
$recipients = explode(",", $message['to']);
}
else {
$recipients = [$message['to']];
}
foreach ($recipients as $recipient_key => $recipient) {
$recipient_send_to_array = $this->parseAddress($recipient);
$personalization = new Personalization();
// Remove unnecessary spaces.
$recipient = trim($recipient);
$recipient_send_to_array = $this->parseAddress($recipient);
$personalization->addTo(new To($recipient_send_to_array[0], isset($recipient_send_to_array[1]) ? $recipient_send_to_array[1] : NULL));
// Add substitutions.
if (isset($message['sendgrid']['substitutions'])) {
foreach ($message['sendgrid']['substitutions'] as $key => $value) {
// For backward compatibility, the v3 version does not allow arrays.
// Each personalization object corresponds to a unique recipient,
// and the substitution values within a personalization object apply
// to that specific recipient.
if (is_array($value)) {
$personalization->addSubstitution($key, $value[$recipient_key]);
}
else {
$personalization->addSubstitution($key, $value);
}
}
}
$sendgrid_message->addPersonalization($personalization);
}
// -----------------------
// END - Personalization Handling
// -----------------------
// Prepare message attachments and params attachments.
if (isset($message['attachments']) && !empty($message['attachments'])) {
foreach ($message['attachments'] as $attachmentitem) {
@@ -563,13 +587,6 @@ class SendGridMail implements MailInterface, ContainerFactoryPluginInterface {
$sendgrid_message->setTemplateId($message['sendgrid']['template_id']);
}
// Add substitutions.
if (isset($message['sendgrid']['substitutions'])) {
foreach ($message['sendgrid']['substitutions'] as $key => $value) {
$personalization0->addSubstitution($key, $value);
}
}
// Tracking settings.
$tracking_settings = new TrackingSettings();
$click_tracking = new ClickTracking();
Loading