How I set the php mail function for multiple domains correctly?

October 8, 2014 6.1k views


Sometime ago I install the mail (php function), but when I tried to send the email by any HTML form the response was too slow. I solved the problem editting the /etc/hosts file with a correct FQDN.

I am using multiple domains (Virtual Host), and this week when I was installing another site I checked that the mails is sending slow again. How can i fix it again?

My /etc/hosts are: localhost.localdomain localhost maindomain maindomain mail mail


  • Are you using sendmail, postfix or something else as your MTA on this droplet? The configuration required to support multiple domains will vary depending on the MTA you are using.

  • @ryanpq At the moment I am using sendmail. But I am willing to set up a new machine, What is better?

1 Answer

There are a couple ways to accomplish this in sendmail but as you are not using sendmail as part of a fully fledged email server and only to send emails from your web scripts the following should do the trick:

<VirtualHost *:80>
    Standard stuff goes here

    <Directory /dir/to/your/web/root>
        php_admin_value sendmail_path "/usr/sbin/sendmail -t -i"


Adding this directive to your virtualhosts should set the default sender for the emails sent by your php scripts. In this case it is setting the sender to for this virtualhost.

Be sure to restart apache after updating your configuration so the change can take effect.

  • So I don’t have to configure the /etc/hosts ?

  • This should not be necessary for outgoing mail from php only. The setting in apache should force the correct email address/domain to be used on these messages.

Have another answer? Share your knowledge.