Skip to content

Commit

Permalink
Pass email params to the recipient variables for non-batch sending
Browse files Browse the repository at this point in the history
Originally, there was a discrepancy in the X-Mailer-Template-Params
message header:

- Batch sending used the structure with email addresses:

  {
      "[email protected]": { /* variables */ },
      "[email protected]": { /* variables */ }
  }

- Non-batch sending used the more direct variant

  {
      /* variables */
  }

We fixed this discrepancy in a hotfix in e1a0221. On 10-June-2022
Mailgun started to return HTTP 400 for the recipient variables
structured as in the example 2 and the change was necessary.

This commit brings the temporarily removed params back to the header.

We also changed the way how message ID is generated to use more
faster crc32b hash instead of the fully random string.

remp/remp#1146

(cherry picked from commit 96ea3d3)
  • Loading branch information
rootpd committed Jun 10, 2022
1 parent 418ef99 commit dc1adc1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

## [0.32.6] - 2022-06-10

- Fixed Mailgun sending issue for non-batch emails. remp/remp#1146
- The reported error was _"The parameters passed to the API were invalid. Check your inputs! 'recipient-variables' parameter is not a valid JSON"_.

## [0.32.5] - 2022-05-27

- Fixed Latte deprecation issues introduced in the latest version of the templating engine. remp/remp#1136
Expand Down
31 changes: 14 additions & 17 deletions Mailer/extensions/mailer-module/src/Models/Mailer/MailgunMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Mailgun\Mailgun;
use Nette\Mail\Message;
use Nette\Utils\Json;
use Nette\Utils\Random;
use Psr\Log\LoggerInterface;
use Remp\MailerModule\Models\Sender\MailerBatchException;

Expand Down Expand Up @@ -90,31 +89,29 @@ public function send(Message $message): void
}

$recipientVariablesHeader = Json::decode($recipientVariablesHeaderJson, Json::FORCE_ARRAY);

$to = [];
foreach ($toHeader as $email => $name) {
if (count($toHeader) > 1 && !isset($recipientVariablesHeader[$email])) {
throw new MailerBatchException("unsafe use of Mailgun mailer with multiple recipients: recipient variables (X-Mailer-Template-Params header) missing for email: {$email}");
}
$to[] = $email;
}
$now = microtime(true);
$messageIdHeader = null;

$messageIdHeader = "%recipient.message_id%";
foreach ($recipientVariablesHeader as $key => $variables) {
foreach ($toHeader as $email => $name) {
$messageId = sprintf(
"remp_mailer_%s_%s@%s",
microtime(true),
Random::generate(16),
hash("crc32c", $email . $now),
(int) $now,
$this->option('domain')
);

if (!is_array($variables)) {
// single email sending, header contains array of params for single address, we can set the header directly
if (count($toHeader) > 1) {
if (!isset($recipientVariablesHeader[$email])) {
throw new MailerBatchException("unsafe use of Mailgun mailer with multiple recipients: recipient variables (X-Mailer-Template-Params header) missing for email: {$email}");
}
$messageIdHeader = "%recipient.message_id%";
$recipientVariablesHeader[$email]['message_id'] = $messageId;
} else {
$messageIdHeader = $messageId;
break;
}

// batch sending, header contains array of params per each email address in batch
$recipientVariablesHeader[$key]['message_id'] = $messageId;
$to[] = $email;
}

$attachments = [];
Expand Down
12 changes: 7 additions & 5 deletions Mailer/extensions/mailer-module/src/Models/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function send(bool $checkEmailSubscribed = true): int
$attachmentSize = $this->setMessageAttachments($message);

$senderId = md5($recipient['email'] . microtime(true));
$this->setMessageHeaders($message, $senderId, [$recipient['email'] => []);
$this->setMessageHeaders($message, $senderId, [$recipient['email'] => $this->params]);

if ($this->context) {
$alreadySent = $this->logsRepository->alreadySentContext($recipient['email'], $this->context);
Expand Down Expand Up @@ -348,14 +348,16 @@ private function setMessageHeaders(Message $message, $mailSenderId, ?array $temp
if (!$this->template->mail_type->locked) {
if ($isBatch) {
$message->setHeader('List-Unsubscribe', '%recipient.list_unsubscribe%');
foreach ($templateParams as $key => $variables) {
foreach ($templateParams as $email => $variables) {
if (isset($variables['unsubscribe'])) {
$templateParams[$key]['list_unsubscribe'] = "<{$variables['unsubscribe']}>";
$templateParams[$email]['list_unsubscribe'] = "<{$variables['unsubscribe']}>";
}
}
} else {
if (isset($templateParams['unsubscribe'])) {
$message->setHeader('List-Unsubscribe', "<{$templateParams['unsubscribe']}>");
foreach ($templateParams as $email => $variables) {
if (isset($variables['unsubscribe'])) {
$message->setHeader('List-Unsubscribe', "<{$variables['unsubscribe']}>");
}
}
}
}
Expand Down

0 comments on commit dc1adc1

Please sign in to comment.