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