Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify to PHP that mail() should be sent using an external mail server?

I have my email hosted at Rackspace Email and would like to use that as my mail server for the contact form on my website.

Looking at the php.ini file, I'm only able to specify the sendmail_path on UNIX systems, from which I've read points to the program that actually sends mail on the server.

I do not want to send mail from my Ubuntu server since I'm not experienced enough to make a secure setup for email... I would like to relay everything to Rackspace's mail.emailsrvr.com.

My question is, how do I specify to the PHP setup on my server that the mail() function should be using an external mail server?

like image 939
Matt Avatar asked Jul 04 '11 15:07

Matt


2 Answers

Since I was researching this issue and stumbled across this post and a third-party php library was not an option for me.

As we know, php uses the sendmail command of the server by default The sendmail_path option in php.ini can be changed to override the setting to your own command with it's own arguments, etc. For example: sendmail_path = /usr/bin/unix2dos | /usr/bin/dos2unix | /usr/sbin/sendmail -t -i

SSMTP will allow you to direct outbound emails to a mailhost from your web/php server. https://wiki.archlinux.org/index.php/SSMTP

apt-get install ssmtp

Then you can use sendmail_path = /usr/sbin/ssmtp -t to tell php to use ssmtp instead of sendmail. Be sure to restart your web server after you have made changes to php.ini

Also ensure you have configured ssmtp and validated your SPF, DKIM, DMARC records before you make the changes to sendmail_path in php.ini

For example gmail Mail server. /etc/ssmtp/ssmtp.conf

# The user that gets all the mails (UID < 1000, usually the admin)
[email protected]

# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also http://mail.google.com/support/bin/answer.py?answer=78799
mailhub=smtp.gmail.com:587

# The address where the mail appears to come from for user authentication.
rewriteDomain=yourdomain.com

# The full hostname
hostname=FQDN.yourdomain.com

# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes

# Username/Password
[email protected]
AuthPass=postmaster-password

# Email 'From header's can override the default domain?
FromLineOverride=yes

For a stack exchange question to the same see https://unix.stackexchange.com/questions/36982/can-i-set-up-system-mail-to-use-an-external-smtp-server

To expand on this.

If using Google, each From: email address must be setup on the sending account as an "Account You Own" setting under accounts. Otherwise google will rewrite the headers with x-google-original-from and specify the From as the sending account instead.

like image 127
Will B. Avatar answered Sep 21 '22 20:09

Will B.


For those who don't want to use a PHP library such as Swiftmailer (and ultimately those who don't want to touch their PHP codebase just to switch SMTP servers), you can do either one of the following:

1.) Windows Servers: Modify your PHP INI file to use an external SMTP relay host. You'll see it in the mailer section labeled "For Windows servers only" - or something similar.

2.) Linux Servers: Install Postfix (e-mail relay service) and configure that to use an external SMTP host. Your PHP installation will attempt to use this to send e-mails by default without any additional configuration.

**This is obviously not intended to give you step by step details on either option above, but rather to point you in the right direction if you're looking for a solution that doesn't require changing instances in your code where PHP's mail() is called.

like image 36
Angelo Ativo Avatar answered Sep 17 '22 20:09

Angelo Ativo