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 install ssmtp
-Configure SMTP
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.
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
To send a file with an attachment;
[wp_ad_camp_1]
excellent!
finally a simple to follow and working example!
Yes, well done!
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?
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.
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.
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
Try to use the 587 TLS port instead of the 465 SSL
Thanks . That helped
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
@yashwanth chakka
take a look at the movement/pir project from camjam http://camjam.me/?page_id=623 (worksheet 5) insert a function for taking a picture and sending it by mail, and then around line 35 insert a call for that function.
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.
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.
This site also has simple info: http://debug.ga/sending-emails-from-the-raspberry-pi
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?
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
Really helpful
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’)
very usefull!! thanks
what about multiple mail destination?
very easy and much quicker. Thanks a bunch
Nice information about send email from raspberry pi. before i don’t know mail sending with raspberry.thanks for sharing informative content.
Is there any way to send multiple image attachments e.g. two jpg files to the same recipient?
Thanks