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. PpFlo

    6 years ago

    TX! Usefull and simple!

  2. Paul

    6 years ago

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

    1. Brent Lessard

      6 years ago

      followed steps exactly, works for me

  3. Hans

    6 years ago

    Works fine on my system, except for messages addressed to recipient ‘[email protected]’.

    System: raspberrypi 2.

    Does anybody knows to fix this issue?

    thans in advance.

    1. Mthylynn

      5 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.

  4. Old Raggare

    6 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

      5 years ago

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

    2. Aarushi Slathia

      4 years ago

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

  5. lalwanis

    6 years ago

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

  6. priya

    6 years ago

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

    1. eamant

      6 years ago

      use smtp.gmail.com:465

  7. Roxysteve

    6 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

      5 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

      6 months ago

      How do we get an API key from Gmail?

  8. Mohammad Hameed

    6 years ago

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

  9. Mohammad Hameed

    6 years ago

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

  10. Feri

    6 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

  11. eamant

    6 years ago

    Can anyone help me by sending an example in python?

    thanks a lot

  12. 1234Garrett1234

    6 years ago

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

  13. lemanoman

    5 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

      4 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.

  14. Rotten_Ronnie

    5 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

      5 years ago

      Thanks for the tip :-)

  15. Kris Pa

    5 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. Jitendra Kasaudhan

    4 years ago

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

  17. Ben

    4 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

  18. Ole Andersen

    4 years ago

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

  19. Jonathan Hull

    4 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.

  20. Stephen Mann

    6 months ago

    E: Unable to locate package ssmtp

    Is ssmtp depreciated or is there another source?

  21. Romel Mendoza

    6 months 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

  22. webwiseguys

    6 months 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.

  23. blintze

    6 months ago

    Thank you! Worked like a charm!

  24. Daniel Jankins

    6 months 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

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

  25. Matthijs Rademakers

    6 months 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:

    [email protected]:/home/pi# ls /var/log/mail.*
    /var/log/mail.err /var/log/mail.info /var/log/mail.log /var/log/mail.warn

    [email protected]:/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.

  26. JUlian

    6 months 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

Leave a Reply to priya Cancel reply

Your email address will not be published.