ssmtp is a nice and simple solution for sending emails from the command line etc.

Installing


sudo apt-get install ssmtp
sudo apt-get install mailutils

Now edit the SSMTP configuration file


sudo nano /etc/ssmtp/ssmtp.conf

It needs to include this:


root=postmaster
mailhub=smtp.gmail.com:587
hostname=raspberrypi
[email protected]
AuthPass=TheGmailPassword
FromLineOverride=YES
UseSTARTTLS=YES

Save and exit

Sending an email


echo "Hello world email body" | mail -s "Test Subject" [email protected]

 

Sending A File

Install mpack


sudo apt-get install mpack

To send a file


mpack -s "Test" /home/pi/some_folder/somefile.ext [email protected]

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. TomD

    2 years ago

    Scripts that had been working for years sending mail with Gmail stopped working. I had to enable two-step authorization and an “APP password” in the Gmail account.

    1. Andrew

      12 months ago

      I think it’s not possible anymore from May 30, 2022 for keep gmail accounts secure.. i understand well?

  2. John

    2 years ago

    Followed orignal instructions, but result of echo… was “bash: mail: command not found”

    Also I noticed that:
    mailhub=smtp.gmail.com:587

    said smtp, so I changed that to smtp.gmail.com:587

    but got same result, mail command not found.

    1. Andrew

      12 months ago

      you need to install mailutils package

  3. Donald T

    3 years ago

    I’ve been trying to get this to work for the better part of the day. My problem was that I too was getting the “mail: cannot send message: Process exited with a non-zero status” message.

    The key for me turned out to the “SSL connection using ECDHE_RSA_AES_256_GCM_SHA384” entry in the /var/log/mail.log file. I found another website that indicates ssmtp does not work with the Rpi Buster OS; ssmtp has been deprecated for that OS.

    The recommendation was to use msmtp package. I uninstalled ssmtp, installed msmtp, created the required entries in the configuration file ( /etc/msmtprc), and five minutes was sending email.

    The relevant website is: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=244147

  4. JUlian

    3 years ago

    first it did not work.
    I had to allow the server for certain users:

    sudo nano /etc/ssmtp/revaliases

    there added:
    root:[email protected]:smtp.gmail.com:587
    OTTO:[email protected]:smtp.gmail.com:587

    furthermore for me personally the connection was tls and not starttls.
    therfore

    UseTLS=Yes
    and
    ports 465

  5. Matthijs Rademakers

    3 years ago

    got the message: mail: cannot send message: Process exited with a non-zero status
    First of all what is actually going on? I found these:

    root@pi2:/home/pi# ls /var/log/mail.*
    /var/log/mail.err /var/log/mail.info /var/log/mail.log /var/log/mail.warn

    root@pi2:/home/pi# tail /var/log/mail.log
    Apr 2 15:31:46 pi2 sSMTP[7839]: Authorization failed (535 Incorrect authentication data)
    Apr 2 15:41:53 pi2 sSMTP[7855]: Creating SSL connection to host
    Apr 2 15:41:53 pi2 sSMTP[7855]: SSL connection using ECDHE_RSA_AES_256_GCM_SHA384
    Apr 2 15:41:53 pi2 sSMTP[7855]: 550 Sender e-mail address invalid / Afzender e-mail adres bestaat niet
    Apr 2 15:42:04 pi2 sSMTP[7858]: Creating SSL connection to host
    Apr 2 15:42:05 pi2 sSMTP[7858]: SSL connection using ECDHE_RSA_AES_256_GCM_SHA384
    Apr 2 15:42:05 pi2 sSMTP[7858]: 550 Sender e-mail address invalid / Afzender e-mail adres bestaat niet
    Apr 2 16:21:07 pi2 sSMTP[8000]: Creating SSL connection to host
    Apr 2 16:21:07 pi2 sSMTP[8000]: SSL connection using ECDHE_RSA_AES_256_GCM_SHA384
    Apr 2 16:21:07 pi2 sSMTP[8000]: 550 Sender e-mail address invalid / Afzender e-mail adres bestaat niet

    from another post I found out that it is possible to specify the from mail address by specifying it in /etc/ssmtp/revaliases.
    Add the line root:[email protected]:smtp.isp.com in that file and try again.

  6. Daniel Jankins

    3 years ago

    Hi, it did not work for me. I made the changes to ssmtp.comtp.conf. I check them serial time. Checked email passwords. logon to the accounts. sent email between. Could not get it to work.
    I tryed with gmail and outlook accounts
    I also trying python example programs. I get the same thing.

    Thank you

    pi@retrotest:~ $ sudo nano /etc/ssmtp/ssmtp.conf
    pi@retrotest:~ $ echo “Hello world email body” | mail -s “Test subject” [email protected]
    mail: cannot send message: Process exited with a non-zero status
    pi@retrotest:~ $

  7. blintze

    3 years ago

    Thank you! Worked like a charm!

  8. webwiseguys

    3 years ago

    You can send mail even with 2-step verification turned on. First, make sure you’re signed into gmail with the email address, then go here: https://myaccount.google.com/u/2/apppasswords
    Use the same configuration as the original post. Creating an App Password will require a name for the APP ( I used my hostname). When it generates a password copy it, then paste it into your configuration (AuthPass=PASTE_PASSWORD_HERE). Test by sending an email.

  9. Romel Mendoza

    3 years ago

    hi how to setup this email if the internet connection is not so good and the email is not sent out it will send it after the raspberry pi got an internet connection? Like an outbox

  10. Stephen Mann

    3 years ago

    E: Unable to locate package ssmtp

    Is ssmtp depreciated or is there another source?

  11. Jonathan Hull

    7 years ago

    It didn’t work for me until I took out the port number at the end of mailhub=smtp.gmail.com – this could have been a firewall issue on my end though.

  12. Ole Andersen

    7 years ago

    Just what I needed. Nice and simple. Thank You.

  13. Ben

    7 years ago

    when i enter “sudo apt-get install ssmtp” and “sudo apt-get install mailutils” it says
    “dpkg was interrupted you must manually run sudo bkpg –configure -a” to correct the problem
    how can i fix this

  14. Jitendra Kasaudhan

    7 years ago

    Thanks a lot for this post. It perfectly works for me fomr ubuntu 14.10 :)

  15. Kris Pa

    8 years ago

    Hi,

    Thank you for your useful info.
    I’d like to add more info here in case the others ran into same problem as me.

    On my machine, in addition to given instructions, I need to change
    1. Less secure apps –> turn on (follow URL in lemanoman’s comment)
    2. root=postmaster line to [email protected]

    the “[email protected]” is the same as the one used for AuthUser parameter.

    And that does the trick.
    (this info is as of 9-Sep-2016)

  16. Rotten_Ronnie

    8 years ago

    I had no success with port 465, so modified it back to 587 as suggested with gmail’s “Less secure apps” setting turned on.

    Don’t forget you can ‘tail -f /var/log/mail.log’ (without the quotes) to display the message status.

    Thanks for sharing!

    1. Adam

      8 years ago

      Thanks for the tip :-)

  17. lemanoman

    8 years ago

    If you got the error : “mail: cannot send message: Process exited with a non-zero status”, you must to activate the less secure apps on you google account: “https://www.google.com/settings/security/lesssecureapps”

    1. Edward Santillan

      7 years ago

      yes, gmail sent me a notification of the failed attempt and included a hot link for enabling less secure email; then everything worked fine just as you said.

  18. 1234Garrett1234

    8 years ago

    Can I sned an e-mail with attachment and body text?

  19. eamant

    8 years ago

    Can anyone help me by sending an example in python?

    thanks a lot

  20. Feri

    8 years ago

    Hi! Not good in english and not good in Linux – but: How to send emails in this manner directly from programm written in C language? Pls, little code example. Thx.
    Feri

  21. Mohammad Hameed

    9 years ago

    Allowing less secure apps in gmail solves the problem – mail: cannot send message: Process exited with a non-zero status

  22. Mohammad Hameed

    9 years ago

    I had the same issue mail: cannot send message: Process exited with a non-zero status.

  23. Roxysteve

    9 years ago

    One problem that may be hiding under the “returned non-zero status” error is that an existing GMAIL account may refuse the connection as insufficiently secure.

    I made a new account just for the pi and that seemed to clear up the issue for me. I did not have to reset any security settings on the new account and was unwilling to relax the security I had set up on my main GMAIL account.

    Everyone should look at the various Linux ssmtp install guides to understand how to secure your ssmtp.conf file (where your GMAIL password sits in plaintext!).

    1. Mthylynn

      8 years ago

      Create a gmail account for the device ([email protected]) or something like that, then use that as the sending email, get an API key from gmail for the security and that may help.

    2. mrtorrence

      3 years ago

      How do we get an API key from Gmail?

  24. priya

    9 years ago

    Sir
    When we work out all the commands it shows error –
    send-mail:cannot open stpm.gmail.com:587

    1. eamant

      8 years ago

      use smtp.gmail.com:465

  25. lalwanis

    9 years ago

    Appreciate your effort to make using pi very easy…
    Thanks for your efforts

  26. Old Raggare

    9 years ago

    I followed all the steps.
    I’m getting an error:
    mail: cannot send message: Process exited with a non-zero status

    1. Trackest27 Fallade27

      8 years ago

      this may mean you need to turn on less secure apps for your gmail account

    2. Aarushi Slathia

      7 years ago

      i got the same error and I tried doing this but didn’t work.

  27. Hans

    9 years ago

    Works fine on my system, except for messages addressed to recipient ‘root@localhost’.

    System: raspberrypi 2.

    Does anybody knows to fix this issue?

    thans in advance.

    1. Mthylynn

      8 years ago

      Check that your permissions are correct. that looks like a resolution problem. If there is no POP server, it won’t receive any messaging.

  28. Paul

    9 years ago

    doesn’t work for me. something’s missing.

    1. Brent Lessard

      9 years ago

      followed steps exactly, works for me

  29. PpFlo

    9 years ago

    TX! Usefull and simple!

Leave a Reply to Mohammad Hameed Cancel reply

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