Enabling PHP mail() function to work on a load balanced droplet

June 22, 2015 1.3k views
Applications PHP Frameworks PHP Configuration Management Load Balancing Email CentOS

Hello all, newb alert!

I was wondering if someone could help me or point me in the right direction...

I have an PHP CodeIgniter application which I am migrating from a managed shared hosting provider and I wanted to test my skills by bringing it into DO. I brought it into DO so that I could implement a HA Proxy load balancer and 3 x Application Server Droplets (1 as backup).

All the app servers are configured identically (CentOS 7) and the load balancer (CentOS 7) is pointing to the app servers in a round robin configuration.

Everything is working as it should apart from e-mails.. The issue I am facing is that when the app now tries to use the PHP Mail() function, it fails because i'm guessing there is no mail server set up? The main MX records to the domain are pointing to Microsoft as Office365 is used for managing that aspect. However I would still like all three app servers to be able to send mail from the PHP application (new user registration confirmation/notifications etc..).

As the domain is not pointed to the app servers and instead to the load balancer, how do I configure the app servers to correctly send email from an address? (eg.

Many Thanks,

1 comment
1 Answer

I was just pondering the same question which is how I found your post. There are two options I'm considering, but first my software already runs phpmailer (free/opensource) which is a very nice interface for sending mail with php and handles a lot of cases and exceptions that you might miss if you were just using PHP's mail functions.

1) Update the mailer to use SMTP, where it would deliver the mail to my main mail server, which would deliver to the recipient.
2) Install just 'mail' and an SMTP server for outgoing mail only on each web node. Doing so would require adding the public IPs to the DNS SPF record so it doesn't get marked as spam.

Something else to consider is where your load is. Having each node send it's own mail would be great if you have to send a lot of emails. But it might not be worth the cost of extra memory use by the SMTP server. So I think in my case I'm going with option number 1.

The only time we send out a lot of emails is when we do our mailing list / promotional items, but that all comes from one node, so I think i'm just going to put the app that sends those emails on the actual SMTP server, which only our site admins can access.

Have another answer? Share your knowledge.