Changing the default “From:” email address for emails sent via PHP on Linux

I’ve had to solve this problem a couple of times at least, and it’s quite a common task, so I thought I’d document it here.

When you send emails to users of your site through using the PHP mail() function, they will sometimes turn up in the mailbox of customers of your site with the following from address:

From: Root <root@apache.ecommercecompany.com>

This makes absolutely no sense to your customers, and often they will think it is spam and delete it. Often, the decision will be made for them by their web mail host, such as hotmail.com or googlemail.com, and they will never even see the email. You don’t want this to happen.

Writing email templates that appear “trustworthy” and have a low chance of being mislabled as spam by the webmail companies, is quite a difficult task, and there’s quite a bit to know about it. However it is quite easy to change the default “From:” email address that PHP sends your emails on as, and that will definitely help.

Assuming you’re running a linux server using sendmail, all you have to do is this.

First create an email address that you would want the customers to see, through editing the /etc/aliases files and running the command newaliases. I created an email address called customer-emails@ecommercecompany.com.

Then change the following sendmail_path line in your php.ini file to something like this:

/etc/php.ini
...
sendmail_path = /usr/sbin/sendmail -t -i -F 'customer-emails' -f 'Customer Emails <customer-emails@ecommercecompany.com>'
...

Broken down, those extra options are:
-F 'customer-emails' # the name of the sender
-f 'Customer Emails <customer-emails@ecommercecompany.com>' # the email From header, which should have the name matching the email address, and it should be the same email address as above

Then restart apache, and it should load the php.ini file changes. Test it by sending a couple of emails to your email address, and you should see emails sent out like this:

From: Customer Emails <customer-emails@ecommercecompany.com>

2 thoughts on “Changing the default “From:” email address for emails sent via PHP on Linux”

  1. This looks like a good page, *except* you don’t document how to use the newaliases command, or what/how i’m editing the /etc/aliases page!

    Now its just kinda useful, and i have to hunt around further! sigh sigh

    Like

  2. There is no need for me to document the newaliases command: it has no arguments. Just run it as root and it will load in aliases from /etc/aliases.

    The /etc/aliases file is also easy to figure out, the format is one alias per line, of the format “[email alias]: [account]”. Any email sent to [email alias] will be sent to the mailbox of [account]. So “postmaster: root” on mymachine.net would direct all email from postmaster@mymachine.net to the mailbox of user root.

    Like

Comments are closed.