Hi! Hope you're enjoying this blog. I have a new home at www.goldsborough.me. Be sure to also check by there for new posts <3

Wednesday, August 14, 2013

Tutorial: How to send an email in Python


Yes, that's basically what we're doing today. What? You have absolutely no clue what that means up there?
Well ok, I'll just stick to words then. Hello, today I will teach you how to send an email in python.

Sending an email in python means using SMTP (Simple Mail Transfer Protocol). Fortunately, Python already has a library for that: smtplib. So you should be able to import smtplib without any problem.

If you want to try it out right away:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

server = smtplib.SMTP('',) #smtp,port number
server.ehlo()
server.starttls()
server.ehlo()
server.login("user@service.com","password")

fromaddr = ""
toaddr = ""
subject = "From Python"

msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject

body = "Sent from Python"
msg.attach(MIMEText(body, 'plain'))

text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)


All you need to do is get your email client's smtp settings and insert them in line 5. For Windows Mail here, for Google Mail here and for Yahoo Mail here etc.

If you want to learn about that code up there, read on.

To begin, we need to import a couple modules, namely smtplib as well as MIMEMultipart and MIMEText from email.mime.xxxx. I'll explain what the latter do later.

Next, we need to set up our smtp server.

server = smtplib.SMTP('',) #smtp,port number
server.ehlo()
server.starttls()
server.ehlo()
server.login("user@service.com","password")

In the first line, we need to assign the variable server to smtplib's SMTP attribute, which takes the smtp settings of your provider and it's port number as it's arguments, which you can get from the links I provided above, or from Google, if you use a different service. Next, we send an ehlo request, start TTLS (Security), and then send another ehlo request. Finally, we log in to the the connection we just built by using our email address and password.

Next, we actually write the message. For this we use MIME (Multipurpose Internet Mail Extensions), which is the standard protocol for sending emails, and specifically for the format of sending emails (From, To, Subject etc.). Python's email library which we  imported before, has special modules for MIME. Actually, if you want to, attaching sound, video or image to the email is very easily done. Check out the official documentation for that.

fromaddr = ""
toaddr = ""
subject = "From Python"

msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject

body = "Sent from Python"
msg.attach(MIMEText(body, 'plain'))

So, first, fill in for fromaddr and toaddr. Then, we use these variables for creating the message, which is done in the above format. Finally, we attach the body of the message to the header of the message, which we created before. The second argument, here 'plain', is the text subtype (html, plain, xml).

Finally, we send the message using server.sendmail().

The person you sent the email to should get the email right away. Next, you could send an email to every person you know or have ever known telling them you can write emails from Python. Could.

EDIT

Just to help you with attaching an image, here the code you would need to attach an image:

fp = open("image.jpg", 'rb')                                                    
img = MIMEImage(fp.read(),_subtype="image")
fp.close()
img.add_header('Content-Disposition', 'attachment', filename="image.jpg")
msg.attach(img) 

1 comment :

  1. I like your blog, I read this blog please update more content on python, further check it once at python online course

    ReplyDelete