Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
},
"require": {
"php": ">=8.0.0",
"php": ">=8.1.0",
"ext-curl": "*",
"ext-openssl": "*",
"phpmailer/phpmailer": "6.9.1",
Expand Down
49 changes: 25 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 19 additions & 11 deletions src/Utopia/Messaging/Adapter/Email/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,32 @@ protected function process(EmailMessage $message): array

$domain = $this->isEU ? $euDomain : $usDomain;

$recipients = $message->getTo();
$toEmails = \array_map(fn ($to) => $to['email'], $recipients);

$body = [
'to' => \implode(',', $message->getTo()),
'from' => "{$message->getFromName()}<{$message->getFromEmail()}>",
'to' => \implode(',', \array_map(
fn ($to) => !empty($to['name'])
? "{$to['name']} <{$to['email']}>"
: $to['email'],
$recipients
)),
'from' => "{$message->getFromName()} <{$message->getFromEmail()}>",
'subject' => $message->getSubject(),
'text' => $message->isHtml() ? null : $message->getContent(),
'html' => $message->isHtml() ? $message->getContent() : null,
'h:Reply-To: '."{$message->getReplyToName()}<{$message->getReplyToEmail()}>",
'h:Reply-To: '."{$message->getReplyToName()} <{$message->getReplyToEmail()}>",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 h:Reply-To entry is a numeric-indexed value, not a keyed header

The array entry on this line is missing the => assignment operator, so the entire string 'h:Reply-To: Name <email>' is stored as a numeric-indexed element (e.g. $body[0]). When form-encoded and sent to Mailgun this becomes 0=h%3AReply-To%3A+... rather than the h:Reply-To custom-header field Mailgun expects. The Reply-To header is therefore silently never forwarded.

This is a pre-existing bug, but this PR already touches the line (adding the RFC 5322 space), making it the natural place to fix it:

Suggested change
'h:Reply-To: '."{$message->getReplyToName()} <{$message->getReplyToEmail()}>",
'h:Reply-To' => "{$message->getReplyToName()} <{$message->getReplyToEmail()}>",

];

if (\count($message->getTo()) > 1) {
$body['recipient-variables'] = json_encode(array_fill_keys($message->getTo(), []));
if (\count($recipients) > 1) {
$body['recipient-variables'] = json_encode(array_fill_keys($toEmails, []));
}

if (!\is_null($message->getCC())) {
foreach ($message->getCC() as $cc) {
if (!empty($cc['email'])) {
$ccString = !empty($cc['name'])
? "{$cc['name']}<{$cc['email']}>"
? "{$cc['name']} <{$cc['email']}>"
: $cc['email'];

$body['cc'] = !empty($body['cc'])
Expand All @@ -82,7 +90,7 @@ protected function process(EmailMessage $message): array
foreach ($message->getBCC() as $bcc) {
if (!empty($bcc['email'])) {
$bccString = !empty($bcc['name'])
? "{$bcc['name']}<{$bcc['email']}>"
? "{$bcc['name']} <{$bcc['email']}>"
: $bcc['email'];

$body['bcc'] = !empty($body['bcc'])
Expand Down Expand Up @@ -140,16 +148,16 @@ protected function process(EmailMessage $message): array
if ($statusCode >= 200 && $statusCode < 300) {
$response->setDeliveredTo(\count($message->getTo()));
foreach ($message->getTo() as $to) {
$response->addResult($to);
$response->addResult($to['email']);
}
} elseif ($statusCode >= 400 && $statusCode < 500) {
foreach ($message->getTo() as $to) {
if (\is_string($result['response'])) {
$response->addResult($to, $result['response']);
$response->addResult($to['email'], $result['response']);
} elseif (isset($result['response']['message'])) {
$response->addResult($to, $result['response']['message']);
$response->addResult($to['email'], $result['response']['message']);
} else {
$response->addResult($to, 'Unknown error');
$response->addResult($to['email'], 'Unknown error');
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Utopia/Messaging/Adapter/Email/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function process(EmailMessage $message): array
$mail->isHTML($message->isHtml());

foreach ($message->getTo() as $to) {
$mail->addAddress($to);
$mail->addAddress($to['email'], $to['name'] ?? '');
}

if (!empty($message->getCC())) {
Expand All @@ -64,12 +64,12 @@ protected function process(EmailMessage $message): array

if (!$mail->send()) {
foreach ($message->getTo() as $to) {
$response->addResult($to, $mail->ErrorInfo);
$response->addResult($to['email'], $mail->ErrorInfo);
}
} else {
$response->setDeliveredTo(\count($message->getTo()));
foreach ($message->getTo() as $to) {
$response->addResult($to);
$response->addResult($to['email']);
}
}

Expand Down
52 changes: 24 additions & 28 deletions src/Utopia/Messaging/Adapter/Email/Resend.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ protected function process(EmailMessage $message): array

$emails = [];
foreach ($message->getTo() as $to) {
$toFormatted = !empty($to['name'])
? "{$to['name']} <{$to['email']}>"
: $to['email'];

$email = [
'from' => $message->getFromName()
? "{$message->getFromName()} <{$message->getFromEmail()}>"
: $message->getFromEmail(),
'to' => [$to],
'to' => [$toFormatted],
'subject' => $message->getSubject(),
];

Expand All @@ -98,35 +102,27 @@ protected function process(EmailMessage $message): array
}

if (! \is_null($message->getCC()) && ! empty($message->getCC())) {
$ccList = [];
foreach ($message->getCC() as $cc) {
if (! empty($cc['email'])) {
$ccList[] = ! empty($cc['name'])
? "{$cc['name']} <{$cc['email']}>"
: $cc['email'];
}
}
if (! empty($ccList)) {
$email['cc'] = $ccList;
}
$ccList = \array_map(
fn ($cc) => ! empty($cc['name'])
? "{$cc['name']} <{$cc['email']}>"
: $cc['email'],
$message->getCC()
);
$email['cc'] = $ccList;
}

if (! empty($attachments)) {
$email['attachments'] = $attachments;
}

if (! \is_null($message->getBCC()) && ! empty($message->getBCC())) {
$bccList = [];
foreach ($message->getBCC() as $bcc) {
if (! empty($bcc['email'])) {
$bccList[] = ! empty($bcc['name'])
? "{$bcc['name']} <{$bcc['email']}>"
: $bcc['email'];
}
}
if (! empty($bccList)) {
$email['bcc'] = $bccList;
}
$bccList = \array_map(
fn ($bcc) => ! empty($bcc['name'])
? "{$bcc['name']} <{$bcc['email']}>"
: $bcc['email'],
$message->getBCC()
);
$email['bcc'] = $bccList;
}

$emails[] = $email;
Expand Down Expand Up @@ -157,9 +153,9 @@ protected function process(EmailMessage $message): array

foreach ($message->getTo() as $index => $to) {
if (isset($failedIndices[$index])) {
$response->addResult($to, $failedIndices[$index]);
$response->addResult($to['email'], $failedIndices[$index]);
} else {
$response->addResult($to);
$response->addResult($to['email']);
}
}

Expand All @@ -168,7 +164,7 @@ protected function process(EmailMessage $message): array
} else {
$response->setDeliveredTo(\count($message->getTo()));
foreach ($message->getTo() as $to) {
$response->addResult($to);
$response->addResult($to['email']);
}
}
} elseif ($statusCode >= 400 && $statusCode < 500) {
Expand All @@ -183,7 +179,7 @@ protected function process(EmailMessage $message): array
}

foreach ($message->getTo() as $to) {
$response->addResult($to, $errorMessage);
$response->addResult($to['email'], $errorMessage);
}
} elseif ($statusCode >= 500) {
$errorMessage = 'Server error';
Expand All @@ -195,7 +191,7 @@ protected function process(EmailMessage $message): array
}

foreach ($message->getTo() as $to) {
$response->addResult($to, $errorMessage);
$response->addResult($to['email'], $errorMessage);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Utopia/Messaging/Adapter/Email/SMTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function process(EmailMessage $message): array
$mail->AltBody = \trim($mail->AltBody);

foreach ($message->getTo() as $to) {
$mail->addAddress($to);
$mail->addAddress($to['email'], $to['name'] ?? '');
}

if (!empty($message->getCC())) {
Expand Down Expand Up @@ -158,18 +158,18 @@ protected function process(EmailMessage $message): array
? 'Unknown error'
: $mail->ErrorInfo;

$response->addResult($to, $sent ? '' : $error);
$response->addResult($to['email'], $sent ? '' : $error);
}

foreach ($message->getCC() as $cc) {
foreach ($message->getCC() ?? [] as $cc) {
$error = empty($mail->ErrorInfo)
? 'Unknown error'
: $mail->ErrorInfo;

$response->addResult($cc['email'], $sent ? '' : $error);
}

foreach ($message->getBCC() as $bcc) {
foreach ($message->getBCC() ?? [] as $bcc) {
$error = empty($mail->ErrorInfo)
? 'Unknown error'
: $mail->ErrorInfo;
Expand Down
Loading
Loading