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.
chris says
Great work, got mine working. Thanks!
David Rahrer says
How does the PHP mail setup work in a multi-site environment?
Jesin says
Hi David,
On a multi-site environment the sendmail instance has multiple domain names configured using the virtusertable feature. For more information see http://www.techrepublic.com/article/configure-it-quick-set-up-sendmail-to-host-multiple-domains/
If you’re looking to configure MSMTP on a multi-site environment then the SMTP server must allow “From:” addresses of different domain names. So if you’re using Gmail navigate to Settings > Accounts and add your different domain email addresses in the “Send mail as:” section.
sandeep says
msmtp: /etc/msmtprc: must be owned by you
msmtp: /etc/msmtprc: must be owned by you
Myra says
Mine is not working.Made all the changes mentioned but email to gmail accounts not sent
Jesin A says
What SMTP server are you using? Try the following command:
Check the debugging output for error messages.
Myra says
No command ‘cho’ found, did you mean:
Command ‘cdo’ from package ‘cdo’ (universe)
Command ‘co’ from package ‘rcs’ (universe)
Command ‘echo’ from package ‘coreutils’ (main)
Command ‘cht’ from package ‘chemtool’ (universe)
Command ‘who’ from package ‘coreutils’ (main)
cho: command not found
ignoring system configuration file /etc/msmtprc: No such file or directory
ignoring user configuration file /home/tele/.msmtprc: No such file or directory
falling back to default account
msmtp: account default not found: no configuration file available
Myra says
This is the exact error what im getting:
ignoring system configuration file /etc/msmtprc: No such file or directory
ignoring user configuration file /home/tele/.msmtprc: No such file or directory
falling back to default account
msmtp: account default not found: no configuration file available
Jesin A says
The command I posted wasn’t copied properly. It is “echo” and NOT “cho”.
Where is your msmtprc file containing the the SMTP details and credentials?
roberto says
Super! It works! 😀
andy says
Hello Jesin,
I believe I have used your tutorial before. I am revisiting it in 2019, to see if the gmail portion needs updating. I have followed the example closely, but the message I received indicates:
tls requires either tls_trust_file (highly recommended) or tls_fingerprint or a disabled tls_certcheck
I may need to brush up on the msmtp man pages, but perhaps you can help. Any assistance is surely appreciated Jesin! =)
Jesin A says
Hi Andy,
Using
yum
orapt
install theca-certificates
package. The certificates are usually installed in/etc/ssl/certs/ca-certificates.crt
so you can use that path intls_trust_file
.