Page 1 of 1

Hanting error when use email function

Posted: Sun May 21, 2017 10:19 am
by joseplaselva
Hi, I have a script that runs when my raspberry boot, and take pictures with a pi camera, and after send the image by email and after move the file to a pendrive. I would like that if there is some problems with the email the script don't stop and folow the sequence.. ( capture image - send image by mail - move the image to usp pendrive and so and so. ) this is the error that give when wifi is not avaiable :
Traceback (most recent call last):
File "/home/pi/0-kam-automail.py", line 131, in <module>
capture_image()
File "/home/pi/0-kam-automail.py", line 75, in capture_image
sendMail(data)
File "/home/pi/0-kam-automail.py", line 92, in sendMail
server = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known

and stops the script....

this is the part of the email function that I want to handel when error happen :

Code: Select all

def sendMail(data):
    global texte
    print texmed
    
    mail = MIMEMultipart()
    mail['Subject'] = str(texmed) # "Pictures from home"
    mail['From'] = fromaddr
    mail['To'] = toaddr
    mail.attach(MIMEText(texte, 'plain'))

    dat='%s.jpg'%data
    attachment = open(dat, 'rb')
    image=MIMEImage(attachment.read())
    attachment.close()
    mail.attach(image)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "pasword")
    text = mail.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    movepic(data)

def movepic(data):
    pic= '%s.jpg'%data
    src = path+pic
    dst = moveto+pic
    shutil.move(src,dst)

Thanks in advance for some suggestions

Re: Hanting error when use email function

Posted: Sun May 21, 2017 10:41 am
by Davies
you could wrap which ever part of the code that fails in a try: except: statement

Code: Select all

try:
    sendmail()
except:
    pass
you could also ping google servers or many other internet/wifi checks

Code: Select all

ping_hostname = "8.8.8.8"
ping_response = os.system("ping -c 10 " + ping_hostname)
if ping_response == 0: # ping response is 0 if successfull
    print ping_hostname, 'is up!'
else:
    print ping_hostname, 'is down!'

Code: Select all

import urllib2
try:
    url = "http://www.google.com"
    urllib2.urlopen(url, timeout=10)
    status = "Connected"
    print "internet connected"
except:
    status = "Not Connected"
    print "internet not connected"
if status == "Connected":
    do_stuf()

Re: Hanting error when use email function

Posted: Sun May 21, 2017 12:33 pm
by joseplaselva
Many, many thanks for a so fast reply, every sugestion seems good.
But if between this script for internet comprobation and the mail sanding function ( may be is a few seconds interval?) the wifi stops.... how handel the error ( or I must discard this interval because are miliseconds (?). Thanks again.

Re: Hanting error when use email function

Posted: Sun May 21, 2017 1:56 pm
by joseplaselva
I have been testing the folow solution you give me :
try:
sendMail(data)
except:
movepic()
instead of just "sendMail(data)"
AND RUNS PERFECTLY !!!, When I close the Wifi in my router, the script just move the file to my usb pendrive because was not able to send the attached mail.

Thanks again for your reply, it really helps me.
Have a nice Sunday