The lines that the user needs to enter or customize will be in red in this tutorial!
The rest should mostly be copy-and-pastable.
While most Linux installations have sendmail preinstalled, there is always a hassle of setting up SPF/PTR records, generating DKIM keys and a lot more to ensure that the email sent by your PHP script is not flagged as spam. A SMTP client called MSMTP can be used to send emails using third-party SMTP servers, this can also be used by PHP's mail() in the place of sendmail.
To install MSMTP on Fedora Linux use yum:
yum install msmtp
CentOS repository doesn't have a RPM package for MSMTP so we need to install it from source:
yum install make gcc pkgconfig wget http://sourceforge.net/
projects/msmtp/files/msmtp/1.tar -xvf msmtp-1.4.31.tar.bz2 cd msmtp-1.4.31 ./configure make make install 4.31/msmtp-1.4.31.tar.bz2/ download
The latest version is 1.4.31 at the time of this writing but it may change in future so to get the latest version, visit this sourceforge page.
On Ubuntu/Debian distribution use apt-get:
apt-get install msmtp
Arch Linux users:
sudo pacman -S msmtp
The configuration file of MSMTP is stored in ~/.msmtprc for each user and /etc/msmtprc is the system wide configuration file. Open the configuration file in your directory.
Add the following lines for a Yahoo account:
account yahoo tls on tls_starttls off tls_certcheck off auth on host smtp.mail.yahoo.com user user1 from firstname.lastname@example.org password yourYahooPa5sw0rd
For Gmail, use the following settings:
account gmail tls on tls_certcheck off auth on host smtp.gmail.com port 587 user email@example.com from firstname.lastname@example.org password yourgmailPassw0rd
This file can also have more than one account, just ensure that the "account" value is unique for each section. Save the file and use chmod to make this file readable only by the owner since it contains passwords. This step is mandatory because msmtp won't run if the permissions are more than 600.
chmod 600 ~/.msmtprc
Before implementing this in PHP, check from the command-line to ensure it works properly. To do this, create a plain text file containing a simple email:
echo -e "From: email@example.com \n\ To: firstname.lastname@example.org \n\ Subject: Hello World \n\ \n\ This email was sent using MSMTP via Gmail/Yahoo." >> sample_email.txt
Now send this email:
cat sample_email.txt | msmtp --debug -a gmail email@example.com
Replace the word "gmail" with "yahoo" or whatever you entered for the "account" option. You'll see a lot of messages because of the "--debug" parameter. This is to make troubleshooting easy if things don't work as expected. If firstname.lastname@example.org receives this email, everything is setup correctly so copy this file to the /etc directory:
cp -p ~/.msmtprc /etc/.msmtp_php
Change the ownership to the username under which the web server is running. This can be "apache", "www-data", or "nobody" depending on the Linux distribution on your VPS and web server installed:
chown www-data:www-data /etc/.msmtp_php
Open the php.ini file, its location varies according to the OS and PHP type installed (PHP CGI, mod_php, PHP-FPM etc):
Find the following line:
Modify it by adding the path to the msmtp command:
sendmail_path = "/usr/bin/msmtp -C /etc/.msmtp_php --logfile /var/log/msmtp.log -a gmail -t"
Manually create a log file and change its ownership to the username your web server is running as:
touch /var/log/msmtp.log chown www-data:www-data /var/log/msmtp.log
Restart your web server to apply the changes:
service httpd restart
In Arch Linux, this is done using the systemctl command:
systemctl restart httpd
Depending on your OS and web server, replace "httpd" with the appropriate name. If PHP is running as a separate process (like PHP-FPM), restart it instead:
service php5-fpm restart
Create a PHP script with a simple mail() to test this setup:
","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.
If this email wasn't sent you can check the msmtp log file for errors.
If the email was not sent when using the PHP script, troubleshoot as follows:
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up