Description
Preconditions
- Magento 2.2.4
- PhP 7.1
- Apache
Steps to reproduce
- Set general store email addresses
- Set store specific email senders in Sales Emails
- Send order/shipment/creditnota/invoice mail to customer
Expected result
Email is sent from store email address
Actual result
It sends from the mail server default mailadress
Why
In vendor \magento\module-sales\Model\Order\Email\SenderBuilder.php the configureEmailTemplate()
function as been changed.
You can see that
$this->transportBuilder->setFrom($this->identityContainer->getEmailIdentity());
has been changed to
$this->transportBuilderByStore->setFromByStore(
$this->identityContainer->getEmailIdentity(),
$this->identityContainer->getStore()->getId()
);
With further inspection it on first glance it returns the same result .
Eventually the from gets formatted within the _formatAddress
function within Zend/Mail.php and used within the same class at setFrom function which has
$this->_storeHeader('From', $this->_formatAddress($email, $name), true);
The 'From' header can have something like Test Mail test@mail.com or just test@mail.com. But the function itself returns just Test Mail with no email.
And that is because the sprintf
function appearently seems to have issues with <>
signs. Note sure why.
So i changed
$encodedName = $this->_encodeHeader($name);
if ($encodedName === $name && strcspn($name, '()<>[]:;@\\,.') != strlen($name)) {
$format = '"%s" <%s>';
} else {
$format = '%s <%s>';
}
return sprintf($format, $encodedName, $email);
to
$encodedName = $this->_encodeHeader($name);
if ($encodedName === $name && strcspn($name, '()<>[]:;@\\,.') != strlen($name)) {
$format = '"%1$s" %3$s%2$s%4$s';
} else {
$format = '%1$s %3$s%2$s%4$s';
}
return sprintf($format, $encodedName, $email , '<' , '>');
and now it does return Test Mail test@mail.com. So that is fixed;
And now i am stuck.
this->transportBuilder->setFrom($this->identityContainer->getEmailIdentity());
-->sends what i need.
$this->transportBuilderByStore->setFromByStore(
$this->identityContainer->getEmailIdentity(),
$this->identityContainer->getStore()->getId()
);
-->sends from my SMTP servers default emailaddress.
It seems like the headers are reset somewhere.