Fix reply-to address when sending WP e-mails

This commit makes sure that the reply-to set for WP e-mails is
preserved when MailPoet is used to send them. Before this change,
MailPoet would ignore whatever was set for the reply-to field for WP
e-mails and use the same address as the one used for the sender instead.

I'm not super happy with the solution proposed here. I couldn't find an
easy way to simple set the reply-to address in the Mailer class and that
is why I'm calling Mailer::init() inside WordPressMailer.

[MAILPOET-3707]
This commit is contained in:
Rodrigo Primo
2021-08-18 10:30:42 -03:00
committed by Veljko V
parent a53a680e8c
commit f8388c5497
2 changed files with 125 additions and 0 deletions

View File

@@ -51,7 +51,15 @@ class WordPressMailer extends \PHPMailer {
];
$sendWithMailer = function ($mailer) use ($email, $address, $extraParams) {
// we need to call Mailer::init() for every single WP e-mail to make sure reply-to is set
$replyTo = $this->getReplyToAddress();
$mailer->init(false, false, $replyTo);
$result = $mailer->send($email, $address, $extraParams);
// make sure Mailer::init() is called again to clear the reply-to address that was just set if Mailer is used in another context
$mailer->mailerInstance = null;
if (!$result['response']) {
throw new \Exception($result['error']->getMessage());
}
@@ -103,4 +111,25 @@ class WordPressMailer extends \PHPMailer {
}
return $result;
}
private function getReplyToAddress() {
$replyToAddress = false;
$addresses = $this->getReplyToAddresses();
if (!empty($addresses)) {
// only one reply-to address supported by \MailPoet\Mailer
$address = array_shift($addresses);
$replyToAddress = [];
if ($address[1]) {
$replyToAddress['name'] = $address[1];
}
if ($address[0]) {
$replyToAddress['address'] = $address[0];
}
}
return $replyToAddress;
}
}