Monday, January 23, 2012

python send email via gmail

The Code

  1. import smtplib  
  2.   
  3. fromaddr = 'fromuser@gmail.com'  
  4. toaddrs  = 'touser@gmail.com'  
  5. msg = 'There was a terrible error that occured and I wanted you to know!'  
  6.   
  7. # Credentials (if needed)  
  8. username = 'username'  
  9. password = 'password'  
  10.   
  11. # The actual mail send  
  12. server = smtplib.SMTP('smtp.gmail.com:587')  
  13. server.starttls()  
  14. server.login(username,password)  
  15. server.sendmail(fromaddr, toaddrs, msg)  
  16. server.quit()