RWEL – smtp and IMAP libraries
I used this code to send an email:
import smtplib
fromaddr = 'gabriella.levine@gmail.com'
toaddrs = 'gabriella.levine@gmail.com'
msg = 'There was a terrible error that occured and I wanted you to know!'
# Credentials (if needed)
username = 'gabriella.levine@gmail.com'
password = 'qwewqwe1!'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
I would like to be able to monitor a project I’m doing for Sustainable energy (where I will be monitoring the voltage input from a solar panel), and send myself an email if any of the components (the Arduino, the Xbee, the Real time clock, the sd card / shield) stops functioning properly
I thought using this code and the smtp library would send an email but I KEEP getting an error(see below), and I’m not quite sure why yet:
import smtplib
from email.mime.text import MIMEText
sender = 'gabriella.levine@gmail.com'
recipients = 'gl715@nyu.edu'
msg = MIMEText('hi my name is gabby')
msg['Subject'] = 'test'
msg['gabriella levine'] = sender
msg['you'] = recipients
smtpserver = 'smtp.gmail.com'
smtpuser = 'gabriella.levine' # set SMTP username here
smtppass = 'qwewqwe1!' # set SMTP password here
session = smtplib.SMTP("smtp.gmail.com", 587)
session.ehlo()
session.starttls()
session.ehlo()
session.login(smtpuser, smtppass)
smtpresult = session.sendmail(sender, [recipients], msg.as_string())
if smtpresult:
errstr = ""
for recip in smtpresult.keys():
errstr = """Could not delivery mail to: %s
Server said: %s
%s
%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
raise smtplib.SMTPException, errstr
session.close()
ERROR:
Traceback (most recent call last):
File “1151.py”, line 1, in
import smtplib
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py”, line 46, in
import email.utils
File “/Users/administrator/Desktop/pyEmail/email.py”, line 25, in
conn.login( “putemailhere”, “putpasswordhere” )
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py”, line 501, in login
raise self.error(dat[-1])
imaplib.error: [AUTHENTICATIONFAILED] Invalid credentials (Failure)
I also started trying to sort through email using the IMAP library – I haven’t gotten very far, and have to look at the documentation some more, but this is what I have:
import sys
import imaplib
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login('gabriella.levine', 'qwewqwe1!')
output = conn.select("INBOX")
print "new messages", output
#toaddress = msg['to']
#output1 = conn.select("SENT MAIL")
#print output1
output1=conn.select('Sent')
print output1