How to use MSMTP with Gmail, Yahoo and PHP Mail

Written by A.Jesin Monday, 24 October 2011 09:26

This is a three in one tutorial which combines how to use MSMTP to send mails via Gmail and Yahoo servers and how to use MSMTP with PHP Mail() function instead of the default sendmail.

Installing msmtp

To install msmtp on Red Hat/CentOS/Fedora type of distributions

yum install msmtp

To install msmtp on Debian/Ubuntu type of distributions

apt-get install msmtp

Configuring msmtp with Gmail and Yahoo

Create or edit the msmtp configuration file in the user’s home directory. I use VI editor to achieve this

vi ~/.msmtprc

Add the following lines to the file, it configures msmtp for both Gmail and Yahoo

account yahoo
tls on
tls_starttls off
auth on
host smtp.mail.yahoo.com
user user1
from user1@yahoo.com
password ******

account gmail
tls on
auth on
host smtp.gmail.com
port 587
user user1@gmail.com
from user1@gmail.com
password ******

Since the file contains sensitive data like passwords you should assign secure permissions

chmod 600 ~/.msmtprc

Testing msmtp

First create a text file containing an email

vi demo_email

From: Jesin <jesin@example.com>
To: Bob <bob@domain.com>
Subject: Hello World
Email sent using MSMTP

To send this email via gmail

cat demo_email | msmtp -a gmail bob@domain.com

To send this email via yahoo

cat demo_email | msmtp -a yahoo bob@domain.com

Using msmtp with PHP mail()

Here comes the juicy part of the tutorial how to use msmtp to send email via PHP’s mail() function. Place the msmtp configuration file in a common place

cp ~/.msmtprc /etc/msmtprc

Change the ownership of the so that username under which the web server process is running can read the file. You should check this with the documentation of your web server software. I’m specifying the settings for Apache running on Red Hat/CentOS/Fedora

chown apache /etc/msmtprc
chmod 600/etc/msmtprc

For Apache on Debian/Ubuntu

chown www-data /etc/msmtprc
chmod 600 /etc/msmtprc

Edit the php configuration file. The path of the file varies according to the server API (mod_php, fastCGI etc), so view the contents of phpinfo() and look for “Loaded Configuration file” and edit it. The following is the location of php.ini in a FastCGI environment

vi /etc/php5/cgi/php.ini

Search and edit the sendmail_path

sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -a yahoo -t"

You can replace yahoo with gmail. Save the file and reload your web server software

Create a php file demo_mail.php with the following contents to test the configuration inside your web server’s document root

<?php
mail("bob@domain.com","Hello World","Email sent using PHP via msmtp");
?>

Open this file by accessing it via URL and the email should be sent. If the mail is not sent take a look at your web server’s error log.

Sometimes you might encounter the following errors in your web server’s error log

msmtp: /etc/msmtprc: must have no more than user read/write permissions

This is because the msmtp configuration file isn’t chmoded properly and can be read by users other than the owner. So do the following

chmod 600 /etc/msmtprc

If you encounter some other errors post them in the comment form.

Enjoyed reading this article, subscribe to stay up-to-date with more such articles

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>