How To Use Gmail or Yahoo with PHP mail() Function

Published on August 2, 2013
How To Use Gmail or Yahoo with PHP mail() Function

What the Red Means

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.

About PHP mail()

The PHP mail() function uses the program in sendmail_path configuration directive to send emails. This is set up as sendmail by default.

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.4.31/msmtp-1.4.31.tar.bz2/download
tar -xvf msmtp-1.4.31.tar.bz2
cd msmtp-1.4.31
make install

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

Configuring 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.

vi ~/.msmtprc

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 user1@yahoo.com
password yourYahooPa5sw0rd

For Gmail, use the following settings:

account gmail
tls on
tls_certcheck off
auth on
host smtp.gmail.com
port 587
user user1@gmail.com
from user1@gmail.com
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: alice@example.com \n\
To: bob@domain.com \n\
Subject: Hello World \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 bob@domain.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 bob@domain.com 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

Configuring PHP

Open the php.ini file, its location varies according to the OS and PHP type installed (PHP CGI, mod_php, PHP-FPM etc):

vi /etc/php5/php.ini

Find the following line:

sendmail_path =

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:

if(mail("receipient@domain.com","A Subject Here","Hi there,\nThis email was sent using PHP's mail function."))
print "Email successfully sent";
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.

tail /var/log/msmtp.log

Common errors

If the email was not sent when using the PHP script, troubleshoot as follows:

  • Check if you edited the correct php.ini file. This can be confirmed by creating a phpinfo(); file and checking the "Loaded Configuration File" section.
  • The path to the msmtp configuration file might be wrong or the web server doesn't have permission to read this file.
  • Check if an email is sent by running the script using command-line PHP:
    php /var/www/html/file.php

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Jesin A


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Ok … managed to nail the problem, droplet is in Singapore 1 with IPv6 enabled. It takes 3-5 minutes with IPv6 and after disabling IPv6 within OS (nothing else changed), it takes 2-4 seconds with IPv4.

I do not know if it is Google or DigitalOcean but I really hope that DigitalOcean will look into and check if there is a IPv6 routing or speed issue.

I ended up going down a deep rabbit hole of apparmor permissions, etc.

Here’s my recommandations:

  • Check /var/log for last modified file when you execute the PHP script to see which files are logging errors
  • Run the PHP script in CLI as www-data (or whatever your webserver runs as), since it gives more detials than just “an error occurred”:
sudo -u www-data php mail.php

In my case, these were the changes needed:

sudo nano /etc/apparmor.d/usr.bin.msmtp

Then add:

/etc/.msmtp_php r

Reload apparmor:

sudo apparmor_parser -r /etc/apparmor.d/usr.bin.msmtp
sudo chmod 600 /etc/.msmtp_php

Good luck!

Had the same issue with PHP 8.2 of emails sending in SSH, but not from the PHP file itself. Nothing in the logs.

Ultimately gave up and just decided to send emails like this:

exec('echo -e "To: alice@email.com\nSubject: Your subject \n\nYour message body" | msmtp -a gmail bob@email.com');

Thanks for the awesome tutorial! I have found tons of new and useful information here and it worked.

I’m close to getting this working, but can’t get the last step. This line sends the email just fine:

cat sample_email.txt | msmtp --debug -a gmail bob@domain.com

But when I try to execute it from a php file (either in the command line with “php /var/www/file.php” or in the web browser) it doesn’t send at all. Oddly, the command line returns “Email successfully sent” while the web browser says “An error occurred.” It seems like something is wrong with the php config. Any ideas?

Five years later and this article is still helpful. Incase anyone else is still reading and has any issues like with security warning, follow these steps:

  1. Setup HTTPS
  2. Setup UFW with correct ports
  3. Create an secondary email account (name it something like auto.response@gmail.com).
  4. Set PHP mail headers as follows:
  mail ( $to, $subject, $html_email, getMailHeaders() );
  function getMailHeaders() {
    $headers = "From: No-Reply <example@gmail.com>\r\n";
    $headers .= "Return-Path: Name <example@gmail.com>\r\n";
    $headers .= "Bcc: Name <example@gmail.com>\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "X-Priority: 3\r\n";
    $headers .= "X-Mailer: PHP". phpversion() ."\r\n";
    return $headers;

Following these steps all emails have no security warning and are not marked as spam.


first of all thanks for this and all the other great tutorials!

However, with this one I have one issue I could not solve.

If I do: cat sample_email.txt | msmtp --debug -a gmail bob@domain.com

There is a lot of console output, ending with: reading recipients from the command line

If I wait long enough (approx. 2 minutes) I receive the mail. However, the problem propagates to phps mail function. This means, if a user fills out a contact form he has to wait for approx. 2 minutes until he receives feedback.

(When googling, I usually find that the /etc/hosts file needs to be properly set, however, this is not the issue here).

Any ideas?

Excellent tutorial!!! I followed the steps and worked like a charm. But I want to send emails from different senders from my website. eg. If the user goes to www.mypage.com/A.php I want to send a mail from mailA@gmail.com and If the user goes to www.mypage.com/B.php I want to send a mail from mailB@gmail.com

In /etc/.msmtp_php I defined both accounts

account gmailA
user mailA@gmail.com
from mailA@gmail.com
password XXXXX
account gmailB
user mailB@gmail.com
from mailB@gmail.com
password XXXXX

In sendpath I defined only gmailA

sendmail_path = "/usr/bin/msmtp -C /etc/.msmtp_php --logfile /var/log/msmtp.log -a gmailA -t"

and all the mails sends with FROM: mailA@gmail.com Which is fine but I want that sometimes the sender is mailB@gmail.com

I use this headers in mail()

 $header.= "From: mailB@gmail.com \r\n" .
    "Reply-To: mailB@gmail.com \r\n";  

but sender keep being mailA@gmail.com

is there any way to define another account in sendmail_path so I can “choose” which one to use??

Dear All,

I am a newbie and using Debian 8 and NGINX for my web server, just followed this tutorial step by step and finally I still unable to send an email. For testing, I run this command;

cat sample_email.txt | msmtp --debug -a gmail wahyu.wardana@yahoo.com

then I got this message:

-bash: /usr/bin/msmtp: Permission denied

What’s the problem, can you help me, please?

Thank you.

I’ve tried this method. It sends an email with this terminal command “cat sample_email.txt | msmtp --debug -a gmail bob@domain.com”, but when I implemented the mail() in php it does not send. Also, it does not send an error.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel