Put your mind into it!
There are many cases when it can be very useful to be able to send emails from the Raspberry Pi to arbitrary recipients. This is not the same as having a real MTA running on the Pi (like Sendmail, Postfix, Exim, QMail, etc.), which can also receive and store emails. In the following we are only going to cover the possibility of sending emails, not receiving. In most cases this is enough, as people tend to use GMail, Yahoo! Mail and other major email service providers and they store their emails on the servers of these providers. Still, sending out emails from the Raspberry Pi can come in handy in many situations. For example, you could have some sensors connected to the GPIO pins of the Pi and you could program the Pi to send you an email when the temperature in the room rises above or drops below certain threshold values, when a gas sensor registers unwanted gas leaks or when the measured voltage of a monitored battery becomes too low. You could also have your Pi send you daily or weekly emails with summarized system data. Or maybe you could connect a webcam to the Raspberry Pi and set up some motion detection software, which would send you an email as soon as it detects motion in a given area of your house. Maybe we don’t even need to go this far. Maybe you are hosting a WordPress website on your Raspberry Pi and you would like to provide your readers with the possibility to subscribe to the posts. This all means that the Pi needs to be able to send out emails, which, unfortunately, can be complicated to accomplish.
In order to achieve this we are going to install a piece of software called SSMTP, which is a simple tool for sending emails. We are also going to configure PHP in a way which is going to make it possible to send emails from inside PHP scripts. This way it’s going to be easy for web applications (like WordPress plugins) to send mails to chosen recipients.
Many email servers today have very strict rules for accepting emails. For example if the email is not coming from a machine with a static IP address, they might classify the email as SPAM. We don’t want that to happen with the emails sent from the Raspberry Pi, so we are going to send the emails to a Goggle server, which will send them forward to the real recipients. In order to be able to accomplish this, you must have a GMail account.
Be sure to specify the correct GMail user name and password here, otherwise you will get authentication errors.
If the host name of your Raspberry Pi is different from “raspberrypi”, specify your actual host name here instead.
Specify this if you would like the outgoing emails to appear to be sent from your.domain (instead of from gmail.com).
Specify this if you would like SSMTP to leave the From field of the emails untouched. Otherwise it will overwrite the from field with the name of the Linux user which sends the email. The overwriting name is taken from the 5th value in the line that corresponds to the sending user, from the /etc/passwd file. If you plan to send emails from a website (for example from a WordPress plugin) and wish to have nice sender names like “John Doe”, I recommend commenting this line (which is equal to setting the value to NO), otherwise your website will only be able to send emails with less nice sender names, like [email protected] In other words, you probably want SSMTP to overwrite the sender field with a nice name taken from the /etc/passwd file.
In case you wish to send out emails from a WordPress plugin, you must make sure that you have a line for the user www-data, which is the user under which WordPress runs.
The permissions of the file /etc/ssmtp/ssmtp.conf determine who will be able to send emails from the Raspberry Pi. By default this file is owned by the user root and the group of the file is also root. So if you want other users, like www-data to be able to send emails (which you definitely want if you’re using a WordPress plugin for example to send out emails), then you need to give read rights to the users who are not the owner of the file and not in the group of the file. The above permissions (774) mean that the owner (root) will be able to read/write/execute the file (7), the other users in the root group will be able to do the same (7) and the users which are not in the group will only have the right to read the file (4). For more details type chmod –help.
Be careful though. Adding the www-data user to the root group might sometimes not be very safe, as it will allow your website to do many things on your system.
Find the line corresponding to www-data and set the fifth value in it to a nice name like “Your super website”.
The text “Test text” is sent to the email address [email protected] (you may also specify multiple addresses separated by spaces) with the subject “Test Mail”. For more options type mail –help.
If you would also like to be able to send out emails from PHP scrips (which is the case if you plan to send emails from your website, perhaps from a WordPress plugin like Subscribe2), then you need to configure PHP to find the mail sending application.
The most probable cause of this is that you have specified a wrong username or password for your GMail account in the /etc/ssmtp/ssmtp.conf file. Double check the values of the AuthUser and AuthPass fields.
Chances are you specified the wrong port for the Google smtp somewhere in /etc/ssmtp/ssmtp.conf or in /etc/ssmtp/revaliases.
How do I set up SSMTP so that it uses an smtp server that does not require authentication?
Good question… I’m afraid I’ve never tried that, so I don’t know the answer. If you find it, please leave a comment here and let me know.
Clear guide, that was coincidently tailored exactly for my needs. Had email up and running within 5mins.
thanx great and easy instructions, set it up in 6 min.
I have a really stupid problem….the command line “mail” command is not found. I installed wheezy some time ago (~ 6 months). Just trying to get e-mail to work for the first time. The ssmtp package installed okay. Used the find command to try to find “mail” – came up empty. What package am I missing ?
Thanks for the great write up. I thing to note is if you have 2-step verification enabled for your Gmail account you need to generate and use an application specific password to make this work.
idea regarding from this piece of writing.
share views. Great blog, continue the good work!
The guide here http://frenchtouch.pro/tutorial/configure-your-debian-server-to-send-mails/47 says to use sendmail_path = /usr/sbin/ssmtp -t. Which one is correct?
Is step 4 necessary? Do I have to have a website to do this? I just want to send emails from the command line. So I’ve nothing to put in for [email protected] in step 4.
hi, i have got the same issues as you . where u able to resolve it? if yes , how?
I have the same problem as well.
Works fine. Except with mailtest.php it has a little flaw for me.
it seems Fromto: is overrides when using gmail server. I changed it to my mail-relay server of my local-isp and the fromto: works as it should be.
I did this on a clean install and even after doing update and upgrade, there was no mail program. I had to do the apt-get install mailutils. On another machine, I had installed LAMP before doing ssmtp and it worked as described above. I suspect one of those might have installed mailutils (probably mysql).
Good point! I also had a LAMP on the PI, so that might have done it for me.
Many thanks for this very helpful note. Your instructions worked “out of the box”.
The communal router here won’t allow addressing by name and frequently reboots, assigning new IP addresses. As the Pi is headless, this causes trouble. Using ifconfig | SSMTP in a boot script gets round the problem easily.
by accident (stumbleupon). I’ve book-marked it for later!
plse, i’m a new user of the rasberry pi and i need serious help.
i want to use the raspi to send students examination results through e-mail and sms.
it’s kind of a project, someone plse help me..!!!!!
I am working on a Java library for home automation for the Pi, and am using the JavaMail Library/API to send notifications, and received commands via email. It is pretty neat, and easy to use !!!
That is awesome, thanks for this guide. It just worked!
Thanks for the guide. I did need to make a change to get it to work.
Looks like the settings are specific for GMail.
In fact, you do not need to install anything.
Exim is already installed on Raspbian, but it is configured by default to stay local.
Having an issue editing my ssmtp.conf file. When I try and save my .conf file it says there is no such file or directory. I’ve tried in both regular user and super user as well. Any ideas? Very new to email set up.
I am also running xbian but having some trouble sending even a simple echo test. What did define exactly in revaliases and ssmtp.conf ?
Thanks. Got it working with my own domain. Although failed with Google. But I’d prefer that.
I get the following warnings and errors trying to run the php test file.
Appreciate if you could help.
Instead of adding users (like www-data) to the roog group, wouldn’t it be safer to set the group of /etc/ssmtp/ssmtp.conf to ‘mail’?
The Arch Linux wiki also has an excellent article on SSMTP.
Don’t for get to remove the angle brackets above. They are just placeholders.
Thanks! Every example for setting SSMTP up with Godaddy missed the revaliases lines. That was the secret to get it working. Thanks for the tip!
Hello I’m trying to setup ossn (open source social network) but I need something to send verification mail, then will this method work for me??? Or I need change something???
Hi Mato! I don’t know ossn. I cannot tell if it will work or not.
Hi! Configure email to work, then write a shell script which calls the python program and sends the value in email. Schedule you shell script with cron to run every 2 hours.
Hello Praveen, i would like to do similar to your question. have you by any chance worked out how to do that ?
Hi, I am somewhat confused by the naming and revaliases…. so I am hoping you could provide some feedback.
1. Thank you very much for this guide – it worked really well to it going. I used this with fial2ban and the email process worked perfectly.
I want default names like pi and root to be able to use this as well. I use Gmail so I though the following would work in the revalaises file but it did not.
what is the the differences? which one is best – I would be happy to get one approch to work :-).
2. I would like a friend pi name – not by app but by individual pi itself.
Is sudo chfn -f “NAME” pi a better way to do this?
Thanks in advance for any/all help.
hi, i have a script scheduled which send me email for backup.. i can’t understand very well what happen, because i receive a correct mail with attachment, but also a second one that have an incorrect CCn, so gmail send me an “Mail delivery failure”..
the second mail seem to be a system mail, because the body is: “sudo: no tty present and no askpass program specified”. is not important for now what the messagge means, but ccn is “[email protected]”.
Hello Andrea! I’m afraid I don’t know the answer to your question. Let’s see if somebody else who reads this can answer it.
Thanks for the tutorial it has worked well – almost!
also, and more importantly, it doesnt seem to work with php.
Very helpful, thank you! You might want to consider changing the smart quotes in the code samples to inch marks so that the sample code will execute properly from the shell.
Agree – replace article quotes using the ” on your keyboard.
Great tutorial, as simple as following the steps 1,2,3. Was looking for a solution to a sendmail problem and this was it. Awesome.
I am getting the following message when sending a test message. Any help would be appreciated.
Thanks a lot!!. Helped for a friend’s web.
Thanks for the article. I was able to send e-mail from the command line. However, if i try sending it from a Python script it doesnt seem to like calling the smtp library and throwing error.
Has anybody sent e-mail via Python please ?
I have the same problem as the one above me, would be thankful if someone could answer in a short amount of time.
If you have any authorization error, go to “My Account” in Gmail and check that “Allow less secure apps” is ON. You can find it at the end of the “Sign-in & security” section.
If you are copy and pasting commands from this article into the raspberry pi then you will get errors where the article shows ” quote marks. Replace the quote marks in the aricle with the standard ” ones on your keyboard. Looks like the articles publishing software is messing with them and changing the characters.
I do have some other, special characters in my password – and I do see the same authentication error (I’m sure the data is correct; I checked it with copy-and-paste out of ssmtp.conf into a webmailer). Is there a way how to enter those kind of passwords into the conf-file?
Try to encode them properly. They are sent with curl.
Thank you so much for this comment. I had a # in my password and your comment made me check and change it, now I have emails arriving.
How can I disabled this resend thing to this address? Or rather, how do I get rid of these 2 extra mails?
This worked perfectly for me, thanks a bunch! Very clear and easy to follow.
Actually, I found I had to make a modification. These instructions worked well for me when I wanted to run the .php file from the CLI (or using cron).
However, if I wanted to view the .php file in a browser and have it send an email, I had to comment out the ‘sendmail_path’ line in /etc/php5/apache2/php.ini. Leaving it as instructed in the tutorial prevents the mail from being accepted (mail() returns false).
I found this after some googling and finding that this line is still commented out in /etc/php5/cli/php.ini , which must be the file that is read when running from the CLI. It seems that /etc/php5/apache2/php.ini is not involved when running from the CLI, only when running from a browser, and additionally that the sendmail_path line you specified somehow breaks the functionality.
Thanks for the tutorial, you’ve made this much easier for lots of us!
Will ISP port blocking rules cause issues with this?
I’m getting: “192.168.1.35 is currently unable to handle this request.
How can I fix this pleblem? Please help.
Thank you so much for this awesome tutorial.
I can’t send email via PHP script as you show on this instruction so the webpage show “http error 500”. But I can send email via Echo command sucessfully. What wrong with it? How to solve this problem.
Good article. One minor correction. The command PHP to send an email through php should be lowercase and not upper case.
All of my research indicates that this is due to an invalid email address syntax. I followed the instructions, so I am not exactly sure where the syntax issue is.
It appears to be an issue with the RCPT TO field. When I run the test, it shows up as: RCPT TO:. My guess is that the Mail” is the problem, but I am not sure where it is coming from.
I think the Mail” should actually be the email address I used on the cli when I ran the test.
I give up, I can’t enter the actual syntax as the posting system keeps stripping it out. Hopefully I described the issue well enough.
As of Jan. 7, 2017, I managed to get this to work on my Pi 2B. All of the commands at the command line had to be preceded with “sudo” to work. I got his working within a shell script that is activated when my PIR sensor is triggered. Thank you for sharing your code.
No matter what I do for some reason I can’t get my FROM header to be what I want it to be, instead it keeps coming up with the gmail email address. I have rewriteDomain set to my website domian name and the FromLineOverride is set to YES. Could someone please give me any suggestions on how I might fix this?
I keep getting this error code?
Thanks! Great Post for easy set-up..
I’ve found if all you want to do is sent quick emails from the command line, it’s easier to install postfix. apt install postfix. Select that it’s an internet site and that’s really all you need. As long as you have mailutils installed as well you can now easily use mail from the command line and it works.
Obviously if you’re setting up a full email site you would want to be more thorough, but if it’s just to send occasional emails from cmdline this is definitely an easier option.
If you love the stuff on IQJar.com, you can support the website by donating a symbolic amount.
Don't want to donate? No problem. Everything on IQJar.com is 100% free, so just enjoy!