Send emails with attachements from the Linux command line.

If you are like me and you are running your Raspberry Pi headless, and don't want to start up X windows to send a simple email. You are in luck, as this can easily be done from the command line. And it is very easy to setup.

Note; This is only to send email, and not receive.


- Install SSMTP

pi@raspberrypi ~ $sudo apt-get update
pi@raspberrypi ~ $sudo apt-get install ssmtp


-Configure SMTP

pi@raspberrypi ~ $ sudo nano /etc/ssmtp/ssmtp.conf

Add add these lines;


root=username@gmail.com
mailhub=smtp.gmail.com:465
rewriteDomain=gmail.com
AuthUser=username
AuthPass=password
FromLineOverride=YES
UseTLS=YES


Use your email address for root, and your gmail username and password for AuthUser and AuthPass.



To send an email
To send an email, run SSMTP with the recipients name.
SSMTP will then wait for you to type your email. It has to be formatted as shown below. And once done, press Ctrl-D to send.

pi@raspberrypi ~ $ ssmtp mail.address@example.com
subject: this is a test
hello world!
pi@raspberrypi ~ $



Send an attachment
It is also possible to send attachments. This can be done using mpack


Install mpack

pi@raspberrypi ~ $sudo apt-get install mpack


To send a file with an attachment;

pi@raspberrypi ~ $ mpack -s subject picture.png mail.address@example.com




[wp_ad_camp_1]

25 thoughts on “Send emails with attachements from the Linux command line.”

  1. Very helpful – got me sending image attachments from a RPi-powered photobooth very quickly. The only quibble I have is that the from name is ‘root’ as I am sending from the RPi root. Is there any way to change this so the recipient sees that it is from ‘Photobooth’ or the like?

    1. I think there is a dependency on Gmail allowing this. Anyway, try this.
      -Add ‘FromLineOverride=YES’ to ssmpt.conf.
      -Add ‘root:username@gmail.com:smtp.gmail.com:465’ to /etc/ssmtp/revaliases With the username you want to be the sender.

      Please let me know if it works and I’ll update the blog.

    2. type this from a the prompt

      sudo nano /etc/passwd

      Find this:
      root:x:0:0:SPIBOX:/root:/bin/bash

      Change to this:
      root:x:0:0:PhotoBooth:/root:/bin/bash

      save and exit.
      Your Done.

  2. hi, i have followed this tutorial and several other tutorials but i am not able to send emails from my raspberry pi.
    the log file says: “unable to connect to smtp.gmail.com:465”
    please help

  3. Hey that was a nice one….I have a doubt.. my project is to send a photo taken by pi camera to a specified gmail address when ever a motion is detected in front of a infrared sensor or in short words home surveillance…the problem is with code…if possible can you help me how to write a code for this

  4. Hi. I’ve used mpack with my surveillance raspberry pi for almost a year and no problem with that.
    Now, i’m trying to use mpack in a python script that i call from a php page and it doesn’t work.
    If I execute the python from the command line it works but from the php it skips the mpack command.

    The python is:

    #!/usr/bin/python
    #
    # timelapseOnDemand.py
    # Creates a timelapse from this day
    #
    # Autor : Carlos Silva
    # Data : 29/03/2015

    # Import required Python libraries
    import time
    import datetime
    import os

    i = datetime.datetime.now()
    name = “%s-%s-%s_%s-%s-%s” % (i.hour, i.minute, i.second, i.day, i.month,i.year)

    timelapse = str(os.popen(“sudo jpeg2swf -o %s.swf /home/pi/webpage/files/*.jpg” % (nome)).read())
    print “Timelapse ok”
    email = str(os.popen(“sudo mpack -s ‘Timelapse on demand’ %s.swf my_email@internet.com” % (name)).read())
    print “Email ok”
    delete = str(os.popen(“sudo rm %s.swf” % (name)).read())
    print “Ficheiro deleted”

    Any advice is welcome because i’m on it for days now.

    1. Resolved…

      When using php to execute a file, the user that executes that file is www-data and not pi so…

      sudo visudo

      and added:

      www-data ALL=(ALL) NOPASSWD: ALL

      This resolves the question. Now is just a matter of adding some security to the question and replacing the ALL for something else.

  5. Great write up. Do you know if there any support to send multiple attachments in the one mail? Or to send text and an attachment in the one mail? And if so, how?

    1. With mpack you can use the -d switch to specify a description file that will give body text. You first need to create a text file with the body text

      For example (using the above example and a body text file you created called info.txt):

      mpack -s subject -d info.txt picture.png mail.address@example.com

  6. Very simple instructions to follow and it worked great in the first try.
    I want my python script to send mail. Is there a way to send mail in one line, without having to enter Ctrl+D?
    Something like :
    os.system(‘ssmtp mail adrs@ex.com subject:hello update passed’)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.