archieoboyle
Posts: 10
Joined: Sun Apr 06, 2014 2:04 pm

RPI email cam

Mon Apr 14, 2014 7:43 am

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

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RPI email cam

Mon Apr 14, 2014 9:14 am

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")
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

archieoboyle
Posts: 10
Joined: Sun Apr 06, 2014 2:04 pm

Re: RPI email cam

Mon Apr 14, 2014 4:54 pm

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:'

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RPI email cam

Mon Apr 14, 2014 6:52 pm

It's python3 not python2.7
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

archieoboyle
Posts: 10
Joined: Sun Apr 06, 2014 2:04 pm

Re: RPI email cam

Tue Apr 15, 2014 5:56 pm

ok thanks, also do i need to add any information into the brackets? or anywhere else; other then the e-mail address?

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RPI email cam

Tue Apr 15, 2014 9:56 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

archieoboyle
Posts: 10
Joined: Sun Apr 06, 2014 2:04 pm

Re: RPI email cam

Sat Apr 19, 2014 4:12 pm

Ok yes I see, the email address from the pi would just be the raspberrypi@IPADDRESS ?

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RPI email cam

Sat Apr 19, 2014 4:52 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

archieoboyle
Posts: 10
Joined: Sun Apr 06, 2014 2:04 pm

Re: RPI email cam

Sun Apr 20, 2014 6:01 pm

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

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RPI email cam

Sun Apr 20, 2014 6:43 pm

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)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Advanced users”