Email Being Sent From php Mail from address.
I have a website sending mail via php Mail built in craft cms. The mail is coming through fine but the from address is not the address specified by craft i.e. it's not using
Can anyone explain why and how I can rectify it so that it uses the email address for the domain name I'm using rather than localhost.localdomain?
Many thanks
3 Replies
The php mail() function is this:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Usually, you need to do two things:
1) in the $additional_headers, include the From: address
2) in the $additional_parameters, include the "-f address" parameter
For example:
// Required variables
$FROMEMAIL = "from@me.com";
$TOEMAIL = "to@you.com";
$SUBJECT = "my email subject";
$PLAINTEXT = "body of email in plain text";
$RANDOMHASH = "whatever";
// Basic headers
$headers = "From: ".$FROMEMAIL."\n";
$headers .= "Reply-To: ".$FROMEMAIL."\n";
$headers .= "Return-path: ".$FROMEMAIL."\n";
$headers .= "Message-ID: <".$RANDOMHASH."@myserver.tld>\n";
$headers .= "X-Mailer: My Application Name\n";
$headers .= "MIME-Version: 1.0\n";
// Add content type (plain text encoded in quoted printable, in this example)
$headers .= "Content-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: quoted-printable\n";
// Convert plain text body to quoted printable
$message = quoted_printable_encode($PLAINTEXT);
// Create a BASE64 encoded subject
$subject = "=?UTF-8?B?".base64_encode($SUBJECT)."?=";
// Send email
mail($TOEMAIL, $subject, $message, $headers, "-f".$FROMEMAIL);
I don't think this is being caused by the code as I have used this plugin on many occasion and it has always used the settings you create. I think this is more to do with the Linode set-up. It's a while since I set up my Linode but I do recall a setting at some point specifying localhost.localdomain but I can't remember where. So I think it is this setting that is being used in php mail is there somewhere in the Linode setup that you specify a mailing domain perhaps?
hostnamectl set-hostname myhost.whatever.tld
Just make sure you have a local MTA like postfix, check with:
systemctl status postfix
By default, PHP uses the command /usr/sbin/sendmail -t -i to transmit mail, which in turn points to /etc/alternatives/mta, which can be used with postfix, exim or other MTA that you've previously setup.
Also, make sure you haven't placed the hostname of the server in the wrong place, like in /etc/hosts, it should NOT point to 127.0.0.1 or localhost.localdomain.