This article explains how to send email from the Raspberry Pi. It is a generic procedure and also works on other Debian distributions. This is not for receiving mail, only for sending it. (Receiving email on the Pi is a much more involved process, requiring your Pi to perform the role of a full mail server).
The ability to send mail across the Internet is useful. It enables your scripts and applications to send you email about system events or sending data such as pictures from a webcam.
This article was updated in December 2017 and has been successfully tested on Raspbian Stretch/Debian 9.
This procedure uses sendmail, the great grandaddy of all email programmes. Install it now as follows. ssh into your pi, or sit in front of the desktop, as you prefer. Become root:
$ sudo su -
Update software lists and install:
# apt-get update
# apt-get install sendmail
This will install sendmail and a few other packages, create the directory /etc/mail, and perform a few other tasks.
Change to /etc/mail and edit the file /etc/mail/submit.mc, using an editor of your choice. (Do not edit the submit.cf file)
# cd /etc/mail
# vi submit.mc
Add the three lines shown below in bold to the file. Put them just after the “DOMAIN(`debian-msp’)dnl” line, as shown:
VERSIONID(`$Id: submit.mc, v 8.14.4-4 2013-02-11 11:12:33 cowboy Exp $')
FEATURE(`authinfo',`hash -o /etc/mail/authinfo.db')dnl
Here, mail.btinternet.com is the mail server of my provider (BT). My domain name is bloggs.gotdns.org. Email sent from the Pi will appear to come from an email address “<user>@bloggs.gotdns.org“.
Save the submit.mc file after making the above changes.
Next, edit the authinfo file.
# vi authinfo
Make it contain a single line, like this one:
AuthInfo: "U:email@example.com" "P:mypassword" "M:PLAIN"
firstname.lastname@example.org is my email address at the ISP. mypassword is the password to go with it. Enter the appropriate address and password for you. Sendmail will use this information to authenticate with your ISP every time it sends an email. It is a basic spam prevention measure implemented by many service providers.
Save the authinfo file and use makemap to create the binary version. The following command creates a file called “authinfo.db“, using the information you placed into authinfo.
# makemap hash /etc/mail/authinfo < /etc/mail/authinfo
Finally, edit your /etc/hosts file and add your domain to the line containing “raspberrypi”. That is the same domain name you used in submit.mc, above (in my case, bloggs.gotdns.org). On a newly installed Raspbian Jessie, it looks like this:
Change that to:
127.0.1.1 raspberrypi bloggs.gotdns.org
Now restart sendmail. This can take a couple of minutes sometimes, so be patient:
# service sendmail restart
Note:If you have found this page after encountering a message like “WARNING: local host name (raspberrypi) is not qualified; see cf/README: WHO AM I?, then adding the domain name to /etc/hosts as just described, should fix it.
Send a Test Email
Send a test email to a mail account you own, eg gmail, hotmail, outlook or whatever:
# /usr/lib/sendmail -v email@example.com
Test from Pi
This will print a load of stuff as it sends the mail, including hopefully a “235 PLAIN authentication successful” message. Check your mail account to see the mail came through. (Check the “junk” mail folder too). It should not take more than a second or two, or maybe a few minutes at very busy times.
Fix Local Email
If the above test worked, Internet mail is working from your Pi. Great. Now, however, local mail fails. By “local mail”, I mean email sent from one user on the Pi to another user, for example when cron sends a user the output from their scheduled jobs, or when the system sends diagnostic messages to the root user. It may not be important to you, but best make it work anyway. Proceed as follows.
Tell sendmail not to authenticate for local mail. Edit the file /etc/mail/access and add this line:
Save the file and type “make” to regenerate its binary version, access.db:
root@raspberrypi:/etc/mail# vi access
# Optional file...
Updating access_db ...
The following file(s) have changed:
** ** You should issue `/etc/init.d/sendmail reload` ** **
# service sendmail restart
If you see messages complaining about “sasl2-bin” not being installed, and about enabling SASL2 support at a later date, they can be ignored for the purposes of this procedure.
As a test, try sending a local mail. Eg send a mail to the “pi” user.
root@raspberrypi:/etc/mail# /usr/lib/sendmail -v pi
Hello pi user from root
Again, lots of diagnostic stuff is printed, indicating successful delivery, including something like “050 … Sent”.
Check that user pi did indeed receive the mail. We could login as user pi and read the mail with an application (do that by all means), but just tailing the user’s mail file is quicker:
# tail /var/mail/pi
You will see the received mail, ie. lots of header information followed by a blank line and your actual message:
Hello pi user from root
Raspbian Wheezy only: Enable IPV6
At around the end of March 2016, an update was made to Raspbian Wheezy (Debian 7) that will prevent local mail from working unless you have IPv6 enabled. This only affects users of Wheezy. IPv6 is enabled by default on Jessie (Debian 8), so it isn’t a problem. However, Wheezy users might see errors like these after updating their systems with apt-get:
Eg. local mail fails, even after making all of the configurations above:
root@raspberrypi:# sendmail -v pi test pi... Connecting to [127.0.0.1] via relay... pi... Deferred: Connection refused by [127.0.0.1]
The “Connection refused” message indicates that the sendmail daemon is not running. Check it with:
root@raspberrypi:# ps -ef | grep sendmail
If it isn’t running, attempt to start the daemon:
root@raspberrypi:# service sendmail restart
If that falls (check with ps again), look at the end of the file /var/log/mail.log for messages like these:
# tail /var/log/maillog root@raspberrypi:# tail /var/log/mail.log Apr 3 14:52:07 raspberrypi sm-mta: NOQUEUE: SYSERR(root): opendaemonsocket: daemon MTA-v6: can't create server SMTP socket: Address family not supported by protocol Apr 3 14:52:07 raspberrypi sm-mta: daemon MTA-v6: problem creating SMTP socket Apr 3 14:52:07 raspberrypi sm-mta: NOQUEUE: SYSERR(root): opendaemonsocket: daemon MTA-v6: server SMTP socket wedged: exiting
The sendmail daemon is failing to start because it seems to require IPv6 to be running. After much messing about, I found the easiest solution was just to enable IPv6.
Edit the file /etc/modules
root@raspberrypi:# vi /etc/modules
and add a single line to the end of it:
and reboot the Pi:
root@raspberrypi:# shutdown -r 0
After the system reboots, local mail should be working and sendmail should be running correctly:
root@raspberrypi:# sendmail -v pi test <ctrl-d> root@raspberrypi:# ps -ef | grep send root 2381 1 0 00:21 ? 00:00:00 sendmail: MTA: accepting connections
Send a Picture
It is quite easy to send email attachments from the command line or a script. One of the best ways is to use mutt.
# apt-get install mutt
Send an email. You don’t need to be root for this, so type <ctrl-d> to go back to the non-root user. The “echo” bit here just sends an empty email, and the “-a” attaches the jpg file. On my Pi, there is an image called “driveway.jpg” installed by default. Email it as follows. Note the double minus (–) seperating the destination adddress from the other options:
$ echo | mutt -a /usr/share/scratch/Media/Backgrounds/Outdoors/driveway.jpg -s "Photo" -- firstname.lastname@example.org
I use this method to email webcam pictures to an external email account.
Thanks for reading. I hope you found this guide useful.