How to send email in Python via SMTPLIB

Here is an email example written in Python module “smtplib”. It will connect to the GMail SMTP server and do the authentication with username and password given (hardcoded in program), and use the GMail SMTP server to send email to the recipient.


import smtplib

to = 'mkyong2002@yahoo.com'
gmail_user = 'mkyong2002@gmail.com'
gmail_pwd = 'yourpassword'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + '\n this is test msg from mkyong.com \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()

Reference

http://docs.python.org/library/smtplib.html

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Janith
Guest
Janith

Thanks, This helps me a lot.

faizan
Guest
faizan

HI, when I use this the smtpserver.starttls(), it gives me an error

smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [Errno 10054] An existing connection was forcibly closed by the remote host

any idea what might be causing this. I am using the exact code that your posted above….

AYRTON
Guest
AYRTON

THANK YOU FOR SHARE. DO YOU KNOW A GOOD BOOK ABOUT NETWORKING USING PYTHON?

mounika
Guest
mounika

Hi, could you please post code for how to write unittest for send email function via smtplib

mounika
Guest
mounika

can anyone post code for how to write unittest for send email function.

SUMIT SAIN
Guest
SUMIT SAIN

how to send email with attachment using python and Google App Engine?

Sujayyendhiren
Guest
Sujayyendhiren

Nice example, works as intendend. Many examples in other’s posts dont work without too many changes!!
Thanks for this !!

Alexandre Magno
Guest
Alexandre Magno

smtpserver.ehlo() or smtpserver.ehlo? mistake?

javi
Guest
javi

Hi Mikong,

In the second call to ehlo, parenthesis are missing… then, that line is doing nothin… is that intentionally?

smtpserver.ehlo

Regards.

Lars
Guest
Lars

This is great! Works perfectly. Thanks so much!

Vin
Guest
Vin

Hi

I have tried the gmail snippet, it worked well…thanks
will be great if you can share snippet for outlook 2007 too

Thanks

Vin
Guest
Vin

Hi

I have tried the gmail snippet, it worked well…thanks
If you can share the one for outlook 2007, will be great

Thanks

Mandar
Guest
Mandar

Thanks .. It worked .. Great script !!!…
Let me know if the there are multiple receiptent.

Mandar Parab

trackback
python 2.7 send mail smtp | mytmyt2345
Bhavan
Guest
Bhavan

Hi folks,

I am new to python. Can any one share me the link to download smtplib package. I am working with windows 7 OS. Can any one help me in this concern….

Thanks,
Bhavan

sandeep
Guest
sandeep

Awesome and many thanks as it’s the only working solution i could find.

however i have 2 following questions…

1. Could you please post a sample for email with attachment when the attachments are picked from a list in a loop?
2. After sending a few mails from an account it’s getting marked as spam or it’s showing that daily email sending quota is exceeded. Is there a solutions for this problem?

thanks again ..

King Mak
Guest
King Mak

Here is some thing that works too (only with gmail though):

import smtplib

From = 'urmail@gmail.com'  
Recipient  = 'some1else'smail@gamil.com'  
Messege = """From: ur_name <ur_email>
To: <Recipient_email>
Subject: Testing Testing Testing

This is test messege.
Love python and continue loving it.
"""  
 
# Credentials
username = 'ur_email'  
password = 'password'  
  
# mail is being sent :  
server = smtplib.SMTP('smtp.gmail.com:587')  
server.starttls()  
server.login(username,password)  
server.sendmail(From, Recipient, Messege)  
server.quit() 

trackback
Como enviar mail con Python via SMTPLIB « Francotel
Omar Mendoza
Guest
Omar Mendoza

Thanks a lot excelent job !

sawan gupta
Guest
sawan gupta

thanks, this is a great example.

Picus
Guest
Picus

Thanks this helps, one note, you may want to change how the header variable is concatenated. The immutability of Python strings would be an issue were this code to be used heavily – say in a loop.

Try appending to a list then using .join() before sending.

rudy
Guest
rudy

thank you, it’s work ^_^

phone
Guest
phone

great, this is the only correct one I have found. thanks!

trzn
Guest
trzn

It’s works. thank’s mkyong :)

walter
Guest
walter

thanks this is the only link Iv found so far that has worked

mikefrid
Guest
mikefrid

Brilliant! Thanks for sharing

Paddy3118
Guest
Paddy3118

Try: this for a similar program snippet wraapped as a function.

rexw
Guest
rexw

Great code. No BS. Works immediately!

Ayushi
Guest
Ayushi

This solves the problem for me. Thank you! No ConnectionRefusedError anymore.