Problem/Motivation

I have code to manually send various emails from our site. We set different From addresses in the mail header depending on the email we send. We have been using Mimemail (and for test, also php mail) senders for sending email and this has always worked fine. We recently had to switch to using SMTP for sending email and just noticed that we are no longer able to set the From address as SMTP (when the smpt config From is not set) always uses the site email address (as opposed to checking first if it is already set).

Steps to reproduce

Proposed resolution

SMTP should not set the From address if it is already set. Possibly if the SMTP From is set then it makes sense; but if it isn't set, it shouldnt be automatically setting it to the site email.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#3 smtp from.jpeg72.84 KBpablonicolas

Comments

liquidcms created an issue. See original summary.

undersound3’s picture

pablonicolas’s picture

StatusFileSize
new72.84 KB

It seems to be working fine on my tests. If I set an email address on the Config Form, it overriddes the Email address from the site settings.

Example

I think using hook_mail_alter is the way to go if you want to change the "From" value depending the email.

liquidcms’s picture

I think you missed the point of this issue. Yes, setting From on SMTP config form, sets From. Not setting From uses Site address. Using mail_alter to set From, is overwritten depending on which of those set on SMTP config form.

In other words, setting nothing for From on SMTP config form should not override s earlier set From address.

liquidcms’s picture

I found the trick. The SMTP module looks for specific message values from_mail and from_name. If those are set, then it uses those to build the From address. This code in a mail alter does the trick:

function my_custom_mail_alter(&$message) {
  // Set special From params to work with SMTP module.
  $from = $message['params']['headers']['From'] ?? $message['headers']['From'];
  $from_name = '';
  $from_email = $from;
  if (preg_match('/^\s*(.*?)\s*<([^>]+)>\s*$/', $from, $matches)) {
    $from_name = trim($matches[1], '" ');
    $from_email = trim($matches[2]);
  }
  $message['params']['from_name'] = $from_name;
  $message['params']['from_mail'] = $from_email;
}