Page 1 of 1

RPI email cam

Posted: Mon Apr 14, 2014 7:43 am
by archieoboyle
Hello!

I need some help! Does any one know how i can get my raspberry pi, with pi cam; to take a picture automatically every minute and then email it to an email address automatically?? Any help with this would be great! :D

Re: RPI email cam

Posted: Mon Apr 14, 2014 9:14 am
by DougieLawson

Code: Select all

import time
from datetime import datetime
import picamera
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

with picamera.PiCamera() as camera:
   camera.resolution = (1024, 768)
   camera.start_preview()
   # Camera warm-up time
   time.sleep(2)
   camera.capture('photo.jpg')

f_time = datetime.now().strftime('%a %d %b @ %H:%M')

toaddr = 'xxxxxx@gmail.com'    # redacted
me = 'picamera@nnn.nnn.nnn.nnn.ifb.co.uk'    # redacted
subject = 'Photo ' + f_time

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = toaddr
msg.preamble = "Photo @ " + f_time

fp = open('photo.jpg', 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)

try:
   s = smtplib.SMTP('localhost')
   s.send_message(msg)
   s.quit()
except:
   print ("Error: unable to send email")

Re: RPI email cam

Posted: Mon Apr 14, 2014 4:54 pm
by archieoboyle
Thanks very much, for the help. However got these issues, any idea what i could be doing wrong?
from: can't read /var/mail/datetime
from: can't read /var/mail/email.mime.image
from: can't read /var/mail/email.mime.multipart
picam.sh: line 8: syntax error near unexpected token `('
picam.sh: line 8: `with picamera.PiCamera() as camera:'

Re: RPI email cam

Posted: Mon Apr 14, 2014 6:52 pm
by DougieLawson
It's python3 not python2.7

Re: RPI email cam

Posted: Tue Apr 15, 2014 5:56 pm
by archieoboyle
ok thanks, also do i need to add any information into the brackets? or anywhere else; other then the e-mail address?

Re: RPI email cam

Posted: Tue Apr 15, 2014 9:56 pm
by DougieLawson
The only changes needed are to replace the to and from email addresses with ones that work on your system with your ISP and your mail server/recipient.

I found with Google's GMail that they wouldn't accept email that came from a bogus/non-existent internet domain.

Re: RPI email cam

Posted: Sat Apr 19, 2014 4:12 pm
by archieoboyle
Ok yes I see, the email address from the pi would just be the raspberrypi@IPADDRESS ?

Re: RPI email cam

Posted: Sat Apr 19, 2014 4:52 pm
by DougieLawson
archieoboyle wrote:Ok yes I see, the email address from the pi would just be the raspberrypi@IPADDRESS ?
I found I had to use pi@internet-resolvable-address.my-isp.co.uk because otherwise GMail rejected it as potential spam.

Re: RPI email cam

Posted: Sun Apr 20, 2014 6:01 pm
by archieoboyle
ok thanks,

my knowledge of this is abit thin, do you have an example of what an address like that would look like?

kind regards

Re: RPI email cam

Posted: Sun Apr 20, 2014 6:43 pm
by DougieLawson
Sorry I don't.

You have to do a reverse nslookup of YOUR public IP address.

You can find that with http://Google.com?q=ip+address

Then
nslookup 86.87.88.89 (using what ever you get from that Google query)

Beware your ip address WILL change from on day to the next.

Code: Select all

#!/usr/bin/python3

from urllib.request import urlopen
ip = urlopen('http://ipaddr.me/').read()
ip = ip.decode('utf-8')
print (ip)

import socket
name, alias, addresslist = socket.gethostbyaddr(ip)
print (name)