Feb 182013
 

I recently setup motion on my Raspberry Pi so I could stream a Logitech c110 to my Android phone. That was successfully setup in this post.

Next step was to start sending the snapshots to my email address when motion was detected. That way I could see if anything was being detected without having to log into the Raspberry Pi itself.

As I started off with RSE, there is no mail daemon installed, so that’s the first thing I need to do.
sudo apt-get install postfix

Configuration for postfix will depend on where you want email to be routed to. I will be sending it to GMail, and my ISP needs the mail to go through their mail server, so I selected Internet with smarthost
Next thing to setup is the local mail client. There are a few options, but in this post we will use heirloom-mailx.
sudo apt-get install heirloom-mailx

We can test out the Postfix install by attempting to send a test email with our newly installed mail client now !
echo 'hi' | mail -s Test <Your Email Address>
This will send an email to your email address with a body text of “hi” and the subject of “Test”.
If you receive it, congratulations ! Your Pi is now emailing out.
If not then you may need to check your junk mail to see if it got junked.
If there are any issues getting the mail out, running mail on the Pi will launch the mail client in which you will see any failed attempts to send an email.

Once Postfix and the local email client is setup, it’s time to configure motion.
If you’re only running one camera, in /etc/motion/motion.conf at approximately line 521, you should see this
; on_picture_save value
This is the line that configures what motion will do to a file once it is saved.

What we will do, is setup motion to attach the file to an email, and send it to your email address when motion is detected and a file is saved.
To do that, change line 521 to the following –
on_picture_save echo 'webcam alert' | mail -a %f -s "Webcam Alert" <Your Email Address>
Make sure you remove the ; at the beginning as that denotes a commented out line.
The ‘webcam alert’ text can be changed to whatever you want in your version.
Running mail with the -a command line argument will attach the file specified, in this case it is %f, which is motion’s variable for the saved file.

Once that is done, restart motion with /etc/init.d/motion restart and whenever you get motion detected, it will email you the picture as an attachment !

Share

  53 Responses to “How To : Automatically Emailing Motion Snapshots”

  1. I have exchanged on_picture_save value with on_picture_save echo ‘webcam alert’ | mail -a %f -s “Webcam Alert” (w/o brackets) but it is not sending an email. I´m using SSMTP and it is sending emails via console fine. When I start motion it is saving the files. What could I have missed out? Thanks.

    • SSMTP ?
      I’m assuming that’s Secure smtp ?
      Can you pipe the output of the mail command to a log file so that we can see what it outputs ?

  2. sSMTP may stay for “simple”, I´m not sure. See http://wiki.debian.org/sSMTP

    This is the Mail Log, hope it´s what you were looking for, when I manually send an email:
    May 27 16:56:42 raspberrypi sSMTP[2262]: Creating SSL connection to host
    May 27 16:56:45 raspberrypi sSMTP[2262]: SSL connection using RSA_AES_128_CBC_SHA1
    May 27 16:56:47 raspberrypi sSMTP[2262]: Sent mail for pi@raspberrypi (221 2.0.0 SMTP closing connection) uid=1000 username=pi outbytes=411

    • Not quite, sorry I didnt’ explain myself well enough.
      We want to change this line
      on_picture_save echo 'webcam alert' | mail -a %f -s "Webcam Alert"
      to this
      on_picture_save echo 'webcam alert' | mail -a %f -s "Webcam Alert" > /var/log/motionmail.log

      Then we can have a look at motionmail.log and see what it’s doing.

      • hmm… thanks for your explanation. Unfortunately it is not creating motionmail.log. I have also tried to set daemon=no and manually start motion in order to see if it is creating pictures, and it does: [1] File of type 1 saved to: /tmp/motion/01-20130528194049-00.jpg

        The only new log I can find is mail.log.

        I don´t wanna spam your really nice blog, if you want, feel free to contact me via email (I receive the address I mentioned) and we later work on a nice appendix for your kb? Thanks.

        • This isn’t spam 🙂 Knowledge for all 🙂

          I’ve just realised, you haven’t put a destination email address…

          The whole line should have your email address at the end.

          on_picture_save echo ‘webcam alert’ | mail -a %f -s “Webcam Alert” test@test.com

          • Yes, that is how I did it:

            ; on_picture_save echo ‘webcam alert’ | mail -a -s “Webcam Alert” address@mydomain.com > /var/log/motionmail.log

            I´m running my own MTA so I can see if someone tries to connect to it but nothning happens. No motionmail.log file is being created in /var/log/

          • Ah right.

            which mail client are you using ?
            Is there a verbosity argument ?

          • I´m using sSMTP (Simple SMTP) and configured as following:

            AuthUser=name@mydomain.com
            AuthPass=all_mine
            FromLineOverride=YES
            mailhub=m.mydomain.com:587
            UseSTARTTLS=YES

            Works if I send an email via terminal.

            I have tried the command via terminal and created motionmail.log manually because if not, it was saying the file was not found. That´s the output:

            “root@raspberrypi:/home/pi# echo ‘webcam alert’ | mail -a %f -s “Webcam Alert” name@mydomain.com < /var/log/motionmail.log
            mail: Invalid header: %f
            mail: Null message body; hope that's ok
            root@raspberrypi:/home/pi#"

            Mail was successfully sent, nothing was written into motionmail.log

          • Needs to be > not <

          • Arghhh, but OK I found something out: When I run echo ‘webcam alert’ | mail -a %f -s “Webcam Alert” name@mydomain.com > /var/log/motionmail.log via Terminal I´ve been told I have no appropriate rights, when I do this command as root it creates motionmail.log but it´s empty.

            When I start motion via motion -c /etc/motion/motion.conf everything is fine, it´s taking and saving pictures but not sending them. motionmail.log does not change, also the timestamp remains the same to the time I started motion via Terminal.

            Set motion daemon to yes – restart motion. Taking and saving pictures yes, sending mail no 🙁

          • Oops, forgot that you wouldn’t have write access to /var/log
            Try logging to /tmp instead when you run it manually.

          • ok, when I start this command manually it creates the log in /tmp but it´s empty (probably because there´s no error?)

            I started motion manually. Pictures are saved into /tmp but motionlog is not being created (I made a reboot before). Motion Daemon is disabled.

            If I enable Motion Daemon it´s still saving pictures in /tmp but not creating motionmail.log

            This is the command in motion.conf:

            # Command to be executed when a picture (.ppm|.jpg) is saved (default: none)
            # To give the filename as an argument to a command append it with %f
            ; on_picture_save echo ‘webcam alert’ | mail -a %f -s “Webcam Alert” name@mydomain.com > /tmp/motionmail.log

          • I just noticed that there is a ; in front of the line…that comments the line out so that it doesn’t actually do anything.
            remove the ; and also remove > /tmp/motionmail.log

          • I thought # or // comments out. Ok, we´re getting closer 🙂
            Can you help with this?

            [1] File of type 8 saved to: /tmp/motion/01-20130603192125.swf
            [1] File of type 1 saved to: /tmp/motion/01-20130603192125-00.jpg
            mail: Invalid header: /tmp/motion/01-20130603192125-00.jpg
            [1] File of type 1 saved to: /tmp/motion/01-20130603192125-01.jpg
            mail: Invalid header: /tmp/motion/01-20130603192125-01.jpg
            [1] File of type 1 saved to: /tmp/motion/01-20130603192126-00.jpg
            mail: Invalid header: /tmp/motion/01-20130603192126-00.jpg

            Should I try another mail client? Also is this only a configuration that PI can send several pictures in only 1 email? In my test I received 4 emails, I assume every email with 1 picture (in case it works 😉 )

          • Hey Mate,

            Sorry about the late reply, been swamped with life.
            Unfortunately without some scripting, motion will send an email per capture.

            And I would try another client, I don’t know why yours would say invalid header…
            Unless you check the options of your mail client, perhaps it has different arguments to attach files.

  3. Hi,

    Yea, similar over here 🙂
    So I have tried Postfix but did not find a way to enter SMTP login details. I googled for some hints but didn´t find any.

    I tried:

    root@raspberrypi:/home/pi# echo ‘hi’ | mail -s Test name@domain.com
    root@raspberrypi:/home/pi# send-mail: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol
    postdrop: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol
    root@raspberrypi:/home/pi#

    I liked sSMTP more…

  4. or do I need to look somewhere into the depths of Postfix in order to set it up as smarthost with SMTP details?

  5. thanks for the how to , works like a charm , any way of limiting the number of pictures sent when there is an event , im getting way to many emails of the same event .
    thanks
    derek k

    • Hi Dere
      You can modify Motions settings to define what it sees as motion and raise the threshold.

      Otherwise you would need to create a script that would get the last snapshot to send after it waits for a set period of time after motion has been detected

  6. Finally I got it working!!! As SMTP server I used sSMTP and mailutils as client. When I removed alsmost everything, set up sSMTP again and meirloom this time, it´s now sending emails including attached file. I guess mailutils was the error.

  7. HI thank you for this great tutorial everything work like a charm, i receive the mail too, but without the attachment so there isnt the picture

  8. Thanks for the guide, but unfortunately it’s not working for me. I’m running wheezy and I’ve set up mailx that runs the

    on_picture_save echo ‘webcam alert’ | mail -a %f -s “Webcam Alert”

    command just fine on its own but motion won’t execute it. I’ve tried to put the code as a shell script and let motion run that, but no luck. Interestingly motion can run other shell scripts, ones that simply create files or such.

    I’m very new to unix, is this some sort of permissions issue?

    • Have you tried emailing yourself with the mail command separately ?

      • Yes, I can email myself just fine, add attachments and everything. But for some reason, motion can’t execute anything that has to do with mail.

        • Tried getting motion to just email without attaching the file ?

          • Absolutely. Nothing happens, still. Other things execute just fine, like copying the output file to another folder.

          • I think the issue is how I’ve configured mailx. I’m not using postfix since I’ve configured mailx to use a gmail account. the configuration sits in home/pi/.mailrc, am I right to believe that motion or whatever user is using it won’t look there? in that case, where should I place the gmail account settings?

          • I’ve fixed it! I added my .mailrc settings to /etc/nail.rc

            thanks for the guide

          • Awesome, great to hear 🙂

  9. HELP!!!!!

    I am new to the Pi I have had it for 6 months (and counting…) and understand all of what this is asking yet when i try to send a email it says
    “send-mail: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol
    postdrop: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol”

    what shall I do can someone plzz help I’m trying to do this for my granddad so he can watch the birds wash themselves in the bird bath….

    I’v tried the “mail” thing and it just says “No mail for pi”…

    help please can some one help me!!!!!!!!!!!!!!!!!!!!!!!

  10. Hello,

    I followed your instructions but I am (and two more people above) the following error after I send a test email:

    “send-mail: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol
    postdrop: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol”

    How can we fix this one?

    Thank you.

  11. I’m curious if you know how or if I could email an image every hour whether there’s motion detected or not.

  12. I keep getting:

    [1] File of type 8 saved to: /root/images/01-20140611002445.avi
    [1] File of type 1 saved to: /root/images/01-20140611002445-01.jpg
    &: 1: &: webcam alert: not found
    [1] File of type 1 saved to: /root/images/01-20140611002446-00.jpg
    &: 1: &: webcam alert: not found
    [1] File of type 8 saved to: /root/images/02-20140611002616.avi
    [1] File of type 1 saved to: /root/images/02-20140611002616-01.jpg
    &: 1: &: webcam alert: not found
    [1] File of type 1 saved to: /root/images/02-20140611002618-00.jpg
    &: 1: &: webcam alert: not found
    [1] File of type 1 saved to: /root/images/02-20140611002619-00.jpg
    &: 1: &: webcam alert: not found
    [1] File of type 1 saved to: /root/images/02-20140611002621-00.jpg
    &: 1: &: webcam alert: not found
    [1] File of type 1 saved to: /root/images/02-20140611002623-00.jpg
    &: 1: &: webcam alert: not found
    [1] File of type 1 saved to: /root/images/02-20140611002625-00.jpg
    &: 1: &: webcam alert: not found
    [1] File of type 1 saved to: /root/images/02-20140611002626-00.jpg
    &: 1: &: webcam alert: not found

    Here is what my motion conf is set to:
    on_picture_save = “webcam alert” | mail -a %f -s “Webcam Alert” myemail@gmail.com

    I can send test emails all day long with no problem.

    Any ideas?

  13. Hi,

    Looks like my motion doesn’t fire any action on save or on detect events. I use postfix and have this in motion.conf:

    on_picture_save echo ‘Motion detected’ | mail -s “Motion detected” mymail@gmail.com
    on_motion_detected echo ‘Motion detected’ | mail -s “Motion detected” mymail@gmail.com

    However this command works fine from command line.
    I also tried this:

    on_picture_save echo ‘Motion detected’ | mail -s “Motion detected” mymail@gmail.com > /tmp/test.log

    But looks like no log file is created at all.
    Any idea what could be wrong?

    Thanks!

    • Rather than using mail first, try echoing out something to a text file e.g. on_picture_save echo ‘test’ > /tmp/test.log and see if that outputs something

  14. Thanks for the tutorial! Super helpful.

    I do receive the emails, but there are no attachments.

    Any thoughts?

    • Make sure the mail client you’re using supports file attachments with the -a switch. The guide is written with the heirloom-mailx mail client in mind.

  15. Hi, I am just getting started with using the pi, and I thank you for posting this tutorial. That said, I followed your instructions and when I attempt to send the test email I get this error;
    pi@raspberrypi ~ $ send-mail: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol
    postdrop: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol

    I assume I need to change the the address family, if that is the case how do I do that?

    • Warnings can normally be ignored. That’s just telling you that IPv6 isn’t enabled, which normally won’t stop things working.

  16. I know it’s a quite old post but I’ve just tried this funny thing during these days and found 2 things:
    it’s “-A %f” to attach file
    and I also need to specify a sender to avoid the smtp server to reject my message (maybe because I don’t have configured postfix to specify one for me) with
    ‘-a “From: some@email.address“‘

  17. Hi and thanks for the tutorial, i can send mail with postfix but i can’t get the picture when motion detect a movement i don’t know why ? Someone can help ?

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)