PHP mail() function is used to create and send emails. The function accepts a number of parameters for assigning the actual values to the various fields of email. The sendmail_path configuration directive has programs which will be used to send emails.
Most of the Linux distributions will have sendmail pre-installed. But the set up of SPF/PTR records, generation of DKIM keys and changes to php script for ensuring mails sent are not flagged as spam can be quite overwhelming.
MSMTP, which is an SMTP client is used to send emails using SMTP servers. This can be used by PHP mail() function instead of sendmail.
Installation of MSMTP
Here, in this tutorial we will be installing MSMTP on Fedora Linux using the yum command:
yum install msmtp
Since the repository do not have RPM package for MSMTP, we will have to install it from the source:
yum install make gcc pkgconfig wget http://sourceforge.net/projects/msmtp/files/msmtp/1.4.31/msmtp-1.4.31.tar.bz2/download tar -xvf msmtp-1.4.31.tar.bz2 cd msmtp-1.4.31./ configure make make install
If you want the latest version, check the official page for updates. Here I have used version 1.4.31.
If you are using Ubuntu/Debian distributions, use apt-get to install:
apt-get install msmtp
Arch Linux users can use pacman installer as shown below:
sudo pacman -S msmtp
Configuration of MSMTP
~/.msmtprc will store the configuration file of MSMTP for every user. The system wide configuration file is /etc/msmtprc.
Open up the configuration file in your directory:
Use the following lines of code for required accounts:
For Yahoo account:
account yahoo tls on tls_start tls off tls_certcheck off auth on host smtp.mail.yahoo.com user user1 from [email protected] password yourYahooPassword
For Gmail account:
account gmail tls on tls_certcheck off auth on host smtp.gmail.comport 587 user [email protected] from [email protected] password yourgmailPassword
Since the file may have more than one account, make sure that the account value is unique for each account.
Save the file and make it readable only by the owner. Msmtp will not run if the permissions on it are above 600.
chmod 600 ~/.msmtprc
To verify it, check if it is working properly from command line. For that, create a new file containing a simple email format.
echo -e "From: Name1<[email protected]> \n\ To: name2<[email protected]> \n\ Subject: Hello World \n\ \n\ This email was sent using MSMTP via Gmail/Yahoo." >> sample_email.txt
Send the email:
cat sample_email.txt | msmtp --debug -a gmail [email protected]
Replace the given names with your actual values. You will be able to see a lot of messages since we used the ‘–debug’ parameter.
Copy the file to /etc directory:
cp -p ~/.msmtprc /etc/.msmtp_php
You have to change the ownership to the username under which the server will be running. It can be either ‘apache’ or ‘www-data’ or ‘nobody’ depending on the Linux distribution.
chown www-data:www-data /etc/.msmtp_php
Configuration of PHP
Open up the php.ini file:
Search for the ‘sendmai_path’ and update it by adding msmtp:
sendmail_path = "/usr/bin/msmtp -C /etc/.msmtp_php --logfile /var/log/msmtp.log -a gmail -t"
Create a log file and change the ownership to username under which server is running:
touch /var/log/msmtp.log chown www-data:www-data /var/log/msmtp.log
Restart your web server:
service httpd restart
For Arch Linux, use systemctl command:
systemctl restart httpd
service php5-fpm restart
Create a php script with mail() function to test the set up:
<?php if(mail("[email protected]","A Subject Here","Hi there,\nThis email was sent using PHP's mail function.")) print "Email successfully sent"; else print "An error occured"; ?>
Access this file from the web browser by pointing to:
You can check the msmtp log file if the email was not sent successfully:
If the email was not sent successfully:
- Check the php.ini file to make sure you have done the correct set up. It can be confirmed by creating phpinfo(); file and checking ‘loaded configuration file’ section.
- MSMTP configuration file path may be given wrong.
- Web server may not have proper permission to read the file.
- Check whether email is sent from command line by running the php script.