Send Email on Raspberry Pi from Command Line with Msmtp

Msmtp is an SMTP client it itransmits a mail to an SMTP server (for example at a free mail provider) which takes care of further delivery.

To use this program with your mail user agent (MUA), create a configuration file with your mail account(s) and tell your MUA to call msmtp instead of /usr/sbin/sendmail. With Msmtp on Raspberry Pi you can use command to send email from Raspberry Pi or use some script to send an email automatically

Install Msmtp

In order for the Raspberry Pi to send email from command line you need to install packages on the Raspberry. You are going to need the following packages
  • msmtp msmtp is an SMTP client that can be used to send mails from Mutt and probably other MUAs (mail user agents). It forwards mails to an SMTP server (for example at a free mail provider), which takes care of the final delivery. Using profiles, it can be easily configured to use different SMTP servers with different configurations, which makes it ideal for mobile clients.
  • ca-certificates This package includes PEM files of CA certificates to allow SSL-based applications to check for the authenticity of SSL connections.
To install the msmtp and ca-certificates packages run following command:
sudo apt-get update
sudo apt-get install msmtp ca-certificates
If there are no error at installation you can continue to next step

Configure Msmtp

To configure the msmtp, you need create msmtp configuration first on /etc folder and edit the msmtprc file using nano or your favorite text editor
cd /etc
sudo touch msmtprc
sudo nano msmtprc
After you've on nano edit's interface you can add this configuration file and save the configuration
account default
host smtp.gmail.com
port 587
logfile /tmp/msmtp.log
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

auth login
user username@gmail.com
password thepassword
from First Last Name

account account2

Testing

To test if msmtp working properly, run following command:
echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" |msmtp --debug --from=default -t username@gmail.com
Explanation
  •  Subject: is the subject of the mail and after \r\n\r\n is the mail's main messages
  • –debug is the output log of the sending mail
  • –from=default is which account used for sending the mail
  • -t username[at]gmail.com  is the destination address
Then check the destination email Inbox

References

  1. Installing and Configuring mSMTP
  2. Images by Cairo on Flickr, used under CC BY 2.0 / Added more shadow from original

Comments

  1. For anyone ending up here like me and seeing the message:

    msmtp: server message: 535-5.7.8 Username and Password not accepted. Learn more at
    msmtp: server message: 535 5.7.8 https://support.google.com/mail/?p=BadCredentials m3sm2690357lfl.97 - gsmtp
    msmtp: could not send mail (account default from /etc/msmtprc)

    at the end of the test - my recommendation is to create a new Gmail account for the purpose of sending mails and Enable the option "Access for less secure apps".

    All this is due to the fact that Google does think that msmpt is vulnerable and therefore all its usages are considered insecure. So enabling this option (at your own risk) meas that you are OK to use it. But just in case - use a new account for the purpose :) Good luck!

    ReplyDelete
  2. Thanks for the HOWTO. A couple of tips:

    TIP 1: If you want non-root users to be able to send mail, that exposes our password to everyone on the box. We can improve security a little by playing with permissions on the /etc/msmtprc file. Change the group to pi (pi user's default group), and change the permissions on the file to 640:

    chgrp pi /etc/msmtprc
    chmod 640 /etc/msmtprc

    This will allow root, and anyone in the pi group (generally, only the pi user) to read the file, but no one else.

    TIP 2: You can place the log file in /var/log along with all of the other log files. Similar to the rc file, we change the group to the pi group, then add the write permission for anyone in the pi group:

    touch /var/log/msmtp.log
    chgrp pi /var/log/msmtp.log
    chmod g+w /var/log/msmtp.log

    You'll also need to replace the original reference in the msmtprc file from
    /tmp/msmtp.log
    to
    /var/log/msmtp.log

    Thanks again!

    ReplyDelete
  3. Thanks from Skip Hire Near for sharing this blog post. this blog has detailed information, its much more to learn from your blog post...

    ReplyDelete
  4. Thanks for writing for us it is very valuable your post
    has informative
    valuable content, it will be helpful.

    ReplyDelete
  5. thanks for sharing valuable info click to know

    ReplyDelete

Post a Comment