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()
Great code. No BS. Works immediately!
This solves the problem for me. Thank you! No ConnectionRefusedError anymore.
Thanks, This helps me a lot.
hi thanks for the above example:
I am getting below error message
smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server.
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….
THANK YOU FOR SHARE. DO YOU KNOW A GOOD BOOK ABOUT NETWORKING USING PYTHON?
Hi, could you please post code for how to write unittest for send email function via smtplib
can anyone post code for how to write unittest for send email function.
how to send email with attachment using python and Google App Engine?
Nice example, works as intendend. Many examples in other’s posts dont work without too many changes!!
Thanks for this !!
smtpserver.ehlo() or smtpserver.ehlo? mistake?
Hi Mikong,
In the second call to ehlo, parenthesis are missing… then, that line is doing nothin… is that intentionally?
”
smtpserver.ehlo
”
Regards.
This is great! Works perfectly. Thanks so much!
Hi
I have tried the gmail snippet, it worked well…thanks
will be great if you can share snippet for outlook 2007 too
Thanks
Hi
I have tried the gmail snippet, it worked well…thanks
If you can share the one for outlook 2007, will be great
Thanks
Thanks .. It worked .. Great script !!!…
Let me know if the there are multiple receiptent.
Mandar Parab
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
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 ..
Here is some thing that works too (only with gmail though):
Thanks a lot excelent job !
thanks, this is a great example.
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.
thank you, it’s work ^_^
great, this is the only correct one I have found. thanks!
It’s works. thank’s mkyong 🙂
thanks this is the only link Iv found so far that has worked
Brilliant! Thanks for sharing
Try: this for a similar program snippet wraapped as a function.
wow, that link is great ! thanks for sharing
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’)
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()
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.