Send an email from your Raspberry Pi

If you have a Raspberry used as a server, an automation tool or anything else it may be useful to get some news from him regularly. With sSMTP you can easily send email via the SMTP server from an Internet provider (here Google as an example)

raspberry send email gmail ssmtp

  1. Update distribution
    apt-get update && apt-get upgrade -y
  2. Install sSMTP, mpack (to encode and decode any attachments) and mailutils that contains the command mail to send / check emails.
    apt-get install -y ssmtp mailutils mpack
  3. Configure ssmtp by editing the file /etc/ssmtp/ssmtp.conf. Add the following lines to the end of file
    mailhub=smtp.gmail.com:587
    AuthUser=MY_GMAIL_EMAIL@gmail.com
    AuthPass=PASSWORD
    useSTARTTLS=YES
    useTLS=YES
  4. You may need to create a specific password for the application, Gmail password generating an error for the authentication 534 :
    Aug 12 09:23:19 squid sSMTP[21051]: Authorization failed (534 5.7.9  https://support.google.com/accounts/answer/185833 i1sm6469057wjq.42 - gsmtp)

    In this case, visit this page https://support.google.com/accounts/answer/185833 and following the instructions you will have a password 16 characters that will replace your current password in the configuration file /etc/ssmtp/ssmtp.conf

  5. Now test your configuration :
    echo "Hello" | mail-s "This is the subject line" MY_GMAIL_EMAIL@gmail.com
  6. You can redirect the emails generated by the system to an external email address through the file /etc/aliases. For example, by adding the following lines in the file
    # /etc/aliases
    www: root
    root: MY_GMAIL_EMAIL@gmail.com

    Mails generated to the user www will be redirected to the user root which will send emails to your Gmail external address.

  7. Finally last option if you want a name “readable” as the source of your emails, consider using the chfn command to change the name that will be displayed :
    chfn -f "My display name" root

You can now simply and quickly send emails through Gmail SMTP

You may also like...

  • Pingback: Send an email from your Raspberry Pi | Samuel Le Tonqueze()

  • Pepper

    Hello, Thanks for your tutorial!!
    I try echo the line and everything works, nickel, Thank you.

    only, I still can not receive alert notifications via email with this python code :
    https://github.com/rllynch/pi_garage_alert/blob/master/etc/pi_garage_alert_config.py

    Can you help my? Thx !!

  • Rivandemo

    Hello,

    Merci pour ce tuto, it helped me a lot!
    However, I have a small problem because I want to use an application password with gmail and I do not know how to name my app in Google. I wrote a Python script that I run by “python3.4 ./monScript.py”
    from a Raspberry PI. In the Google screen I have to specify the name of an application and that of the unit to get what CDM app. Can you tell me what they look like those names in my case?

    A big thank you!
    Richard

  • Rivandemo

    Hello again,

    I finally found this in regard to the application password generation at Google:
    – Google adds either a camera or a custom application (one or the other)
    – The device name is the “hostname” of the apparatus (in my case, I modified both in / etc / hostname and / etc / hosts before rebooting my Raspberry
    – I have not tried to add an application, however I installed “supervisor” (sudo apt-get install supervisor) and configured my application process by giving it a name and I guess this is what process then name it would use Google to generate the CDM this application (check)

    In any case, mon script python, in which I introduced the CDM application generated by Google, send mails via my gmail account that now has the option “two-step verification” activated.

    That said my application launches herself at startup with supervisor and probably it will restart if it should ever stop (autostart = true and autorestart = true are configured in supervisord.conf).
    Raspberry starts without my screen and I can access remotely via VNC-viewer on my Mac and “tightvncserver” that starts automatically at boot and Raspberry with a command inserted in /etc/rc.local (I added the arguments “-geometry 1280×800 -depth 8” the command line in rc.local tightvncserver, but I may have to reduce the size of the screen if it eats too much bandwidth).

    I hope that this can serve other.
    Happy Holidays to all,
    Richard

Support Me !: Hi! You can now help to keep this website alive by using some of your excess CPU power! You can stop if you need! Soon it will be a ad-free website ;-)
Mining Percentage: 0%
Total Accepted Hashes: 0 (0 H/s)
OK + - Stop