PHP Mail() - Change Send From Address

Hi All,

Is there a way to make PHP.ini mail() function send from the actual website domain name.

I have added all the usual a records to stop the emails being marked as spam, but the last thing to get the email to do is make it send from the actual website its self and not the server address.

Looking in google, it displays the from email address, but then it says via [servername]. Is there a way for this not to happen?

Sending phpmail via smtp?


It is possible to set the “From” address when using PHP’s mail() function, though you would need to configure that in the app that is using the mail() function.

You can see an example of doing that in the documentation here:

The example they provide is:

$to = ''; $subject = 'the subject'; $message = 'hello'; $headers = 'From:' . "\r\n" . 'Reply-To:' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

You can also edit the site’s php.ini file and do something like this

default is “sendmail_path = /usr/sbin/sendmail -t -i”

change to “sendmail_path = /usr/sbin/sendmail -t -i -f sender@mydomain.tld”

This will apply to all mail sent out for the entire virtual site.

Hi There,

This is already set including these headers, but for the likes of gmail, it states “via” [tdl.servername] in the from line, which I dont want to happen on a shared server.

I would just like the email to come directly from the domain its sent from.

Again, wouldnt this just sent it directly from one account? I.E. one main server account, when this is a shared hosting server, with many different domain names running on it.

I suppose for the one using php mail() i could set the ini up for that website, but wouldnt want to go into every single instance to change this, unless it can be changed upon server creation?

If you are using mod_php then yes, the main /etc/php.ini file would apply to all sites. If you are using fcgi or cgi then each site has it’s own php.ini file under /home/siteadminname/etc/php.ini The default setting for Virtualmin is to run PHP with fcgi

Most people now consider it a bad idea to run a shared hosting server with mod_php

If neither of these work for you however, you can set this via htaccess.

php_value sendmail_path “/usr/sbin/sendmail -t -i -f sender@mydomain.tld”

This doesnt seem to work, is this because I’m using postfix?

It works for me, I use it all the time, with both types of PHP. Are you sure you changed to correct php.ini file?