How To Send Email With Python (smtplib and MIMEText)

It is actually pretty easy to  send emails with python. The smtplib module provides the necessary "tools" to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

In this script I am using gmail, but it is not limited to Google as long as your provide the correct smtp server + port.

Python 2.7.3 

  1. from email.mime.text import MIMEText  
  2. import smtplib  
  3.   
  4. def sendmail(to,subject,text):  
  5.     user = 'name@gmail.com'  
  6.     pwd = 'password'  
  7.     msg = MIMEText(text)  
  8.     msg['From'] = 'james@gmail.com'  
  9.     msg['To'] = to  
  10.     msg['Subject'] = subject  
  11.     try:  
  12.         smtpServer = smtplib.SMTP('smtp.gmail.com', 587)  
  13.         smtpServer.ehlo()  
  14.         smtpServer.starttls()  
  15.         smtpServer.ehlo()  
  16.         smtpServer.login(userpwd)  
  17.         smtpServer.sendmail(user, to, msg.as_string())  
  18.         smtpServer.close()  
  19.         print "mail sent."  
  20.     except SMTPException:  
  21.         print "failed."  

Let's take it for a test drive... 


How To Send Email With Python (smtplib and MIMEText)

 Seems to work.

How To Send Email With Python



Comments

Popular posts from this blog

How to be anonymous on the internet (99-100%)

Deep Web Link List (.onion)

Automatically Check The Raspberry Pi's Temperature (+Prevent Overheating)