Absolutely Tech

[HowTo] Configure msmtp to work with gmail on linux

By: Deepak Mittal

Msmtp is a small but powerful and highly customizable smtp client. You can access gmail smtp using msmtp, which is exactly what I’ll teach in this tutorial.

Step by step instructions:

  1. Install msmtp and ca-certificates for use with SSL:
    sudo apt-get install msmtp ca-certificates
  2. We’ll create and edit the configuration file using Gedit:

    sudo gedit /etc/msmtprc

    Now copy and paste the following code in the text editor:

    defaults
    tls on
    tls_starttls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt
     
    account default
    host smtp.gmail.com
    port 587
    auth on
    user username@gmail.com
    password mypass
    from username@gmail.com
    logfile /var/log/msmtp.log

    Needless to say, you have to replace username@gmail.com with your email and mypass with your password

  3. It should be readable by anyone. However, we’ll still execute the following command to be sure.

    sudo chmod 0644 /etc/msmtprc
  4. Your msmtp configuration is ready. Lets send a test email to yourself to see if everything’s working properly:
    echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" |msmtp --debug --from=default -t username@gmail.com

    Again replace username@gmail.com with your own email address and execute it. You should receive an email.

These are the exact commands to be used with Ubuntu.
You can replicate the steps for another distibution by using equivalent commands (For eg; yum install in place of apt-get install for centos, redhat, fedora.)
Cheers!

Category: How-To, Ubuntu (Linux)

Tagged: , , , , ,

  • http://twitter.com/xobb xobb

    >> echo “This is a test mail”|msmtp -s “Test mail” –debug –from=default -t username@gmail.com

    This doesn’t work. Fails with the Error:
    >> msmtp: invalid option — ‘s’

    • http://www.absolutelytech.com Deepak Mittal

      I guess something has changed in newer versions. I had tested that command. Anyways, I’ve updated the post with working command.

  • Pingback: Разноє()

  • Nobody

    you’re God! there is a mess around about sending emails from command line with all rocket scientists around using monsters to defeat ants. Thanks

  • Pingback: [HOWTO] Send emails from localhost in PHP using msmtp (using gmail account) on linux | Absolutely Tech()

  • Pingback: Sending email from localhost using MSMTP with gmail | Okada Design Blog()

  • http://profiles.google.com/lamouche Glen Winters

    Hello. I just stumbled on this and got it working. Is there a way to encrypt the password so that it’s not shown in plaintext in the config file?

  • http://www.facebook.com/sitthykun Sitthykun Ly-kh

    I got error
    msmtp: cannot log to /var/log/msmtp.log: cannot open: Permission denied
    msmtp: log info was: host=smtp.gmail.com tls=on auth=on user=myemail@gmail.com from=default recipients=myemail@gmail.com mailsize=40 smtpstatus=250 smtpmsg=’250 2.0.0 OK 1307440481 p5sm25399pbd.12′ exitcode=EX_OK

    • http://www.absolutelytech.com Deepak Mittal

      chmod 0777 /var/log/msmtp.log

      • Hanlon Kitto

        Note that this command leaves a file owned by root world-writable.

      • Влад Красовский

        This is strongly not recomended

  • Rbrunius

    I’m getting the following error to the command line:

    msmtp: /etc/msmtprc: line 1: unknown command defaults

    • http://www.absolutelytech.com Deepak Mittal

      Could you post your msmtprc file. Please remove your username and password before doing so.

      • Yael Demedetskaya

        the same error here

        defaults
        tls on
        tls_starttls on
        tls_trust_file /etc/ssl/certs/ca-certificates.crt

        account default
        host mail.xxx.ru
        port 25
        auth on
        user xxx@xxx.xx
        password xxx
        from sales@transdostavka.ru
        logfile /var/log/msmtp.log

  • Pingback: Getting GMAIL notifications via ZABBIX on CENTOS6 – Charles L'Abri Anderson()

  • manu

    It can not be a good idea to put my gmail password in a file which can be read by everbody!!

  • Guest

    I agree with others that it’s a very bad idea to store the password in plain text. Someone who is competent should provide a tutorial on how to encrypt the password.

  • Pingback: Automated Notification of Dynamic Public IP Change - stevemcandrew.com()

  • Pingback: msmtp für E-Mailversand (auch für PHP) | AZ's Blog()

  • eboak

    There is absolutely no need to have the config file readable by
    everyone and as others have mentioned it is a massive security risk. If
    you are storing your password in plain text you should change the
    permissions to 640 or even 600 and make sure root is the owner:

    chown root:root /etc/msmtprc
    chmod 0640 /etc/msmtprc

    If
    you do not wish to store the password in plain text (which you
    shouldn’t) you can make use of the ‘passwordeval’ configuration command
    which use the output of a command as the password. You could, for
    example, set passwordeval to use gpg to decrypt an encrypted password
    stored in /root/ or your own user’s home directory:

    passwordeval gpg -d /root/.msmtp.password.gpg

    http://msmtp.sourceforge.net/doc/msmtp.html#passwordeval

  • Vittorio F.

    Thanks! Very helpful. This howto worked for me where other pages failed

  • Pingback: Raspberry come datalogger - Pagina 15()

  • Pingback: Setup Linux Environment « Prince Faisal Ahmed()

  • Douglas Richard

    it works properly!
    thank you so much!!

    • Surya

      what is the command to attach a file using msmtp to send in mail

  • Niko Z.

    Works perfectly with Ubuntu 14.04, thanks

  • Pingback: VanRaspi()

  • smbraun

    Problem with CentOs: (I do not have a /etc/ssl/certs/ca-certificates.crt file)
    msmtp: kann Vertrauensdatei /etc/ssl/certs/ca-certificates.crt nicht laden: error:02001002:system library:fopen:Datei oder Verzeichnis nicht ge

    msmtp: konnte Mail nicht verschicken (Account default aus /etc/msmtprc)

  • the_nuts

    msmtp: support for TLS is not compiled in
    How to complie msmstp with TLS? I’m un ubuntu server 14.04

=