565
Views

I’m working on a project where I will be sending out notifications when something changes in my home as part of an automation and security project with raspberry pi. To get this to be useful, I want to send an email notification for now to see when environmental changes such as a light being turned on or when the camera senses motion.

Installing SSMTP To Send Mail

As with anything Linux related there are a million ways to accomplish this. I want simplicity here so I’m using SSMTP and a gmail account that I’ve created for my Raspberry Pi.

sudo apt-get install ssmtp mailutils mpack

Now let’s add the SMTP credentials. You need to edit ssmtp.conf to add your SMTP credentials.

sudo nano  /etc/ssmtp/ssmtp.conf

Find this line if you are using gmail and update it to your SMTP server and port number. I’m using 587 and will enable STARTTLS in the next section.

mailhub=smtp.gmail.com:587

We need to add the following lines to use a secure SMTP connection. Just add these to the bottom of the file.

AuthUser=emailaddress@gmail.com
AuthPass=PASSWORD 
useSTARTTLS=YES

Allowing Attachments In Mail (optional)

The default mail user agent doesn’t allow you to send mail with attachments so we will be removing it and adding a new one.

Let’s start by installing the new one with the following command.

sudo apt-get install heirloom-mailx

Now we can remove the original one.

sudo rm /etc/alternatives/mail

Finally, we add this line.

sudo ln -s /usr/bin/heirloom-mailx /etc/alternatives/mail

Sending Mail

Use this formatting to send an email with an attachment and subject.

mail -a mylogfile.log -s "Your Subject Here" emailaddress@mailaccount.com

You will be prompted to fill in the body of the message, You can add anything you would like and then press return. Type a “.” on the last line and the message will send.

If you are confused at how this message works, you can review this example. You will just need to give it a try to understand the flow.

pi@sdatic:~ $ mail -a test.log -s "Sending Example Log" example@sdatic.com
Here is the body of my message.
Hitting return just takes me to a new line. 
I have to hit return and press "." and then return to finish the message. 
.
EOT
pi@sdatic:~ $ 

Conclusion

This is one of many ways to add mail functionality from your Raspberry Pi but I think it’s the easiest, it just works. Comment below if you have any questions about the process.

I will be adding a continuation for this project to setup a job that will send this out hourly as well as events that will trigger if the system senses a change. I’m also putting together a Web UI and more functionality so stay tuned.

Article Categories:
Tech
SDATIC

Web developer and former photographer by trade...gamer and all around tech enthusiast in his free time. Christoph started sdatic.com as a way to organize his ideas, research, notes and interests. He also found it enjoyable to connect and share with others who parallel his interests. Everything you see here is a resource that he found useful or interesting and he hopes that you will too.

All Comments

  • Hey, how could you automate this with a bash script? I’m new to this kind of stuff and I want to set up a security cam that automatically sends me a picture if my motion sensor is detected. Im using a raspberry pi 3

    Kenneth C Payne November 13, 2017 7:34 pm Reply
    • Yes you could! Great idea, I’ll have to give this a go and make a post about it!

      Christoph October 15, 2018 10:02 am Reply

Leave a Reply

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