Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Linux ›
How to use MSMTP with Gmail, Yahoo and PHP Mail

How to use MSMTP with Gmail, Yahoo and PHP Mail

October 24, 2011 Linux Jesin A 12 Comments

linux category thumbnail

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.

Related posts:

How to Configure MSMTP for Cron windows category thumbnailUsing Sendmail on Windows linux category thumbnailSetup Linux DNS Server for Windows Active Directory linux category thumbnailSSH verify host public key linux category thumbnailHow to configure a Linux PPTP VPN client

Tags: linux, php, php.ini

Comments

  1. chris says

    July 10, 2013 at 4:27 am

    Great work, got mine working. Thanks!

    Reply
  2. David Rahrer says

    July 13, 2013 at 7:25 am

    How does the PHP mail setup work in a multi-site environment?

    Reply
    • Jesin says

      July 20, 2013 at 2:03 pm

      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.

      Reply
  3. sandeep says

    January 22, 2014 at 4:03 pm

    msmtp: /etc/msmtprc: must be owned by you
    msmtp: /etc/msmtprc: must be owned by you

    Reply
  4. Myra says

    September 5, 2014 at 11:57 am

    Mine is not working.Made all the changes mentioned but email to gmail accounts not sent

    Reply
    • Jesin A says

      September 5, 2014 at 2:04 pm

      What SMTP server are you using? Try the following command:

      echo "Hello World" | msmtp -d user@gmail.com

      Check the debugging output for error messages.

      Reply
      • Myra says

        September 6, 2014 at 3:59 pm

        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

        Reply
  5. Myra says

    September 6, 2014 at 4:01 pm

    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

    Reply
    • Jesin A says

      September 6, 2014 at 4:59 pm

      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?

      Reply
  6. roberto says

    October 31, 2016 at 7:57 am

    Super! It works! 😀

    Reply
  7. andy says

    December 19, 2019 at 8:55 am

    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! =)

    Reply
    • Jesin A says

      December 19, 2019 at 7:45 pm

      Hi Andy,

      Using yum or apt install the ca-certificates package. The certificates are usually installed in /etc/ssl/certs/ca-certificates.crt so you can use that path in tls_trust_file.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.