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.