Sending mail with the Raspberry Pi

Raspian, the default linux distribution for the Raspberry Pi does not install a mail server by default, so the raspberry cannot send mails. For raspberries running headless, i.e. without a monitor attached, mailing would be a good feauture, e.g. to report pending package updates, etc. So here is a simple way to enable sending mails from the raspberry without installing a full featured mail server.

You can install a full-featured mail server like Postfix, but this would consume too much resources on the raspberry and for sending update notifications you would not need all its features. A more simple way is to use the mail server of your internet provider or a service like GMX or similar. You only need to tell the Raspberry to forward the outgoing mails to the correct mail server. This can be done by installing the packages ssmtp and heirloom-mailx:

apt-get install ssmtp heirloom-mailx

The ssmtp package is used to transport your mails to your provider and needs to be configured by editing /etc/ssmtp/ssmtp.conf. There you add the name of your mail server, your username and password and enable TSL to encrypt the connection to your provider (if it supports encryption). This is my ssmtp.conf configuration file:

# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=MyMailServerName

# Where will the mail seem to come from?
rewriteDomain=MyMailServerDomain
# The full hostname
hostname=raspberry

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES

# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes

# Username/Password
AuthUser=MyMailUsername
AuthPass=SecretPassword

The heirloom-mailx package is installed to be able to send mails on the command-line. You can test your installation like this:

echo "mail from your raspberry" | \
mail -s "Testmail" YourEmail@YourProvider.com

If this does not work, you can call ssmtp directly and get debug output this way:

echo "Test" | \
sendmail -v YourEmail@YourProvider.com

This will print out the communication with the mail server of your provider.

LinkedIn logo mail logo