Main Tutorials

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 = '[email protected]'
gmail_user = '[email protected]'
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 Author

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

Comments

Subscribe
Notify of
32 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
rexw
5 years ago

Great code. No BS. Works immediately!

Ayushi
5 years ago

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

Janith
6 years ago

Thanks, This helps me a lot.

VIKAS RAWAT
5 years ago

hi thanks for the above example:

I am getting below error message

smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server.

faizan
6 years ago

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
8 years ago

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

mounika
8 years ago

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

mounika
8 years ago

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

SUMIT SAIN
8 years ago

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

Sujayyendhiren
8 years ago

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

Alexandre Magno
9 years ago

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

javi
9 years ago

Hi Mikong,

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

smtpserver.ehlo

Regards.

Lars
10 years ago

This is great! Works perfectly. Thanks so much!

Vin
10 years ago

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
10 years ago

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
11 years ago

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

Mandar Parab

Bhavan
11 years ago

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
11 years ago

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
11 years ago
Reply to  sandeep

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

import smtplib

From = '[email protected]'  
Recipient  = 'some1else'[email protected]'  
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() 

Omar Mendoza
11 years ago

Thanks a lot excelent job !

sawan gupta
11 years ago

thanks, this is a great example.

Picus
11 years ago

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
12 years ago

thank you, it’s work ^_^

phone
12 years ago

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

trzn
12 years ago

It’s works. thank’s mkyong 🙂

walter
13 years ago

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

mikefrid
13 years ago

Brilliant! Thanks for sharing

Paddy3118
14 years ago

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

Steven
11 years ago
Reply to  mkyong

Hi I ran the program, but it didnot work, gave the following error:

C:\Python25>Python SendEmail_ByGmail3.py
Traceback (most recent call last):
File “SendEmail_ByGmail3.py”, line 6, in
smtpserver = smtplib.SMTP(“smtp.gmail.com”,587)
File “C:\Python25\lib\smtplib.py”, line 244, in __init__
(code, msg) = self.connect(host, port)
File “C:\Python25\lib\smtplib.py”, line 310, in connect
raise socket.error, msg
socket.error: (10061, ‘Connection refused’)

Jestoy
9 years ago
Reply to  Steven

Or instead of “smtplib.SMTP” use “smtplib.SMTP_SSL” that works for me.

import smtplib

user = ‘[email protected]
password = ‘yourpassword’

recipients = [‘[email protected]’]
sender = ‘[email protected]
message = ‘This is a test.’

session = smtplib.SMTP_SSL(‘smtp.gmail.com’)
session.login(user, password)
session.sendmail(sender, recipient, message)
session.close()

cae2100
11 years ago
Reply to  Steven

the issue that your having is that the python program cannot communicate with the gmail’s email servers, alot of the time, that issue is because certain internet companies block port 25 except for thier servers, all you have to do is use your internet’s mail servers instead and you can send it that way.