how-to-send-emails-with-gmail-using-python
2018-01-16 20:10
302 查看
http://stackabuse.com/how-to-send-emails-with-gmail-using-python/
There are quite a few ways to send email with Python, whether it be through a 3rd party library like with
boto and SES, or through an email protocol like SMTP. While the subject of using Python to send emails may seem like it's been done to death, there are just so many different ways to do it and so many issues that can come up. I thought it would be helpful
to write up a tutorial on how to send emails with Gmail as the provider using Python.
SMTP server, like the one Gmail uses. This library is called, predictably,smtplib and comes included with Python.
SMTP (Simple Mail Transfer Protocol) is an application-level protocol (on top of TCP) used to communicate with mail servers from external services, like an email client on your phone. SMTP is a delivery protocol only, so you can't actually retrieve email
with it, you can only send email, which is what we'll be focusing on in this article. If you want to retrieve email instead, then you'll want to check out the
IMAP (Internet Message Access Protocol) protocol.
I should note that many email services, like
Gmail, don't usually use SMTP on their internal mail servers. SMTP is usually just provided as an outward-facing interface to their service via the
Using this library, there are a few different ways you can create a connection to your mail server. In this section, we'll focus on creating a plain, insecure connection (which should rarely, if ever, be used). This connection is unencrypted and defaults
to port 25. However, the protocol for mail submission actually uses 587, which is what we'll use.
These connections are really simple to create with
And that's it. There really isn't much more to it than passing the server address, port, and calling
Note: You might not be ready to send emails quite yet. Gmail imposes some restrictions on SMTP connections like this. See the section "Authenticating with Gmail" below for more info.
always use a secured connection.
There are a few different ways you can secure your SMTP connections in the
Notice that while this is very similar to the previous insecure connection we created, all that's different is that we're using the
Your other option is to create an SSL connection right from the start. In this case, you'll want to use the
Among other slight differences, we use the SMTPS port (465) right away, although you could just leave the port parameter out of this and
Now that we have our connection, let's create an email.
As you can see, each line contains a new field with its data. No binary protocol, no XML, no JSON, just line-separated strings.
A simple way to parameterize these fields is to use string formatting in Python:
Now all you have to do is pass the
secure" method.
You can't really blame Google for setting it up this way since your application (or some other 3rd party app) will need to have your plaint-text password for this to work, which is definitely not ideal. It's not like the OAuth protocol where a revocable
token is issued, so they have to find another way to ensure no unauthorized parties access your data.
For many other email providers you won't need to do any of the extra steps I describe here.
First, you'll want to allow less secure apps to access your account. For detailed instructions on how to do this, you should check out this page:
Allowing less secure apps to access your account
If you have 2-step verification enabled on your account, then you'll need to create an app-specific password for less secure apps like this. In that case, you'll need to follow the instructions here:
Sign in using App Passwords
And finally, if you're still getting an
Display Unlock Captcha
I haven't had to do this last step for my own accounts, but I've read that it doesn't actually work right away. Apparently after you enable less secure apps, you may need to wait a few minutes before trying the 'Display Unlock Captcha' link. If you run in
to this problem and find a good way around it, please let us know in the comments!
As for the actual Python code, all you need to do is call the
Using the email string we constructed above, and the connected/authenticated
put special restrictions on SMTP access like Google does, let us know! We'd like to include as much info as possible here.
Do you programmatically send emails with SMTP? What kind of applications do you use it for? Let us know in the comments!
Resources
https://docs.python.org/3.5/library/smtplib.html
http://stackoverflow.com/a/27515833/2684304
Edit: Thanks to cropr (in the comments) for pointing out that Gmail imposes a number of restrictions on people sending email with SMTP. Free accounts are limited to 500 emails per day and are rate-limited to about 20 emails per
second.
If you think you'll exceed these limits, then you may want to either set up your own SMTP server or use services like AWS SES or Sendgrid.
There are quite a few ways to send email with Python, whether it be through a 3rd party library like with
boto and SES, or through an email protocol like SMTP. While the subject of using Python to send emails may seem like it's been done to death, there are just so many different ways to do it and so many issues that can come up. I thought it would be helpful
to write up a tutorial on how to send emails with Gmail as the provider using Python.
The SMTP Protocol
This may not come as a surprise, but of course Python already has a library that lets you connect to anSMTP server, like the one Gmail uses. This library is called, predictably,smtplib and comes included with Python.
SMTP (Simple Mail Transfer Protocol) is an application-level protocol (on top of TCP) used to communicate with mail servers from external services, like an email client on your phone. SMTP is a delivery protocol only, so you can't actually retrieve email
with it, you can only send email, which is what we'll be focusing on in this article. If you want to retrieve email instead, then you'll want to check out the
IMAP (Internet Message Access Protocol) protocol.
I should note that many email services, like
Gmail, don't usually use SMTP on their internal mail servers. SMTP is usually just provided as an outward-facing interface to their service via the
smtp.gmail.comserver. This is mostly meant to be used by email clients on your phone or computer (like Outlook, Thunderbird, etc).
Opening the Connection
As already mentioned, Python conveniently comes with thesmtplib, which handles all of the different parts of the protocol, like connecting, authenticating, validation, and of course, sending emails.
Using this library, there are a few different ways you can create a connection to your mail server. In this section, we'll focus on creating a plain, insecure connection (which should rarely, if ever, be used). This connection is unencrypted and defaults
to port 25. However, the protocol for mail submission actually uses 587, which is what we'll use.
These connections are really simple to create with
smtplib. The unencrypted version can be created with:
import smtplib try: server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() except: print 'Something went wrong...'
And that's it. There really isn't much more to it than passing the server address, port, and calling
.helo(), which identifies you to the SMTP server. Using this
serverobject you can now send email over an insecure connection.
Note: You might not be ready to send emails quite yet. Gmail imposes some restrictions on SMTP connections like this. See the section "Authenticating with Gmail" below for more info.
Using a Secure Connection
When an SMTP connection is secured via TLS/SSL, it is done over port 465 and is typically called SMTPS. Needless to say, you shouldalways use a secured connection.
There are a few different ways you can secure your SMTP connections in the
smtpliblibrary. The first way is to first create an insecure connection and then upgrading to TLS. This is done using the
.starttls()method.
import smtplib try: server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() # ...send emails except: print 'Something went wrong...'
Notice that while this is very similar to the previous insecure connection we created, all that's different is that we're using the
.starttls()method to upgrade the connection to secure.
Your other option is to create an SSL connection right from the start. In this case, you'll want to use the
.SMTP_SSL()method instead:
import smtplib try: server_ssl = smtplib.SMTP_SSL('smtp.gmail.com', 465) server_ssl.ehlo() # optional # ...send emails except: print 'Something went wrong...'
Among other slight differences, we use the SMTPS port (465) right away, although you could just leave the port parameter out of this and
smtplibwill default to 465 anyway.
Now that we have our connection, let's create an email.
Creating the Email
Emails, at the very core, are just strings of text connected by newline characters. Most emails will at least have the "From", "To", "Subject" and a body fields. Here is a simple example:From: you@gmail.com To: me@gmail.com, bill@gmail.com Subject: OMG Super Important Message Hey, what's up? - You
As you can see, each line contains a new field with its data. No binary protocol, no XML, no JSON, just line-separated strings.
A simple way to parameterize these fields is to use string formatting in Python:
sent_from = 'you@gmail.com' to = ['me@gmail.com', 'bill@gmail.com'] subject = 'OMG Super Important Message' body = 'Hey, what's up?\n\n- You' email_text = """\ From: %s To: %s Subject: %s %s """ % (sent_from, ", ".join(to), subject, body)
Now all you have to do is pass the
email_textstring to
smtplib, which we'll show in the next section, and you're good to go.
Authenticating with Gmail
There are a few steps you need to take before you can send emails through Gmail with SMTP, and it has to do with authentication. If you're using Gmail as the provider, you'll need to tell Google to allow you to connect via SMTP, which is considered a "lesssecure" method.
You can't really blame Google for setting it up this way since your application (or some other 3rd party app) will need to have your plaint-text password for this to work, which is definitely not ideal. It's not like the OAuth protocol where a revocable
token is issued, so they have to find another way to ensure no unauthorized parties access your data.
For many other email providers you won't need to do any of the extra steps I describe here.
First, you'll want to allow less secure apps to access your account. For detailed instructions on how to do this, you should check out this page:
Allowing less secure apps to access your account
If you have 2-step verification enabled on your account, then you'll need to create an app-specific password for less secure apps like this. In that case, you'll need to follow the instructions here:
Sign in using App Passwords
And finally, if you're still getting an
SMTPAuthenticationErrorwith an error code of 534, then you'll need to do yet another step for this to work.
Display Unlock Captcha
I haven't had to do this last step for my own accounts, but I've read that it doesn't actually work right away. Apparently after you enable less secure apps, you may need to wait a few minutes before trying the 'Display Unlock Captcha' link. If you run in
to this problem and find a good way around it, please let us know in the comments!
As for the actual Python code, all you need to do is call the
loginmethod:
import smtplib gmail_user = 'you@gmail.com' gmail_password = 'P@ssword!' try: server = smtplib.SMTP_SSL('smtp.gmail.com', 465) server.ehlo() server.login(gmail_user, gmail_password) except: print 'Something went wrong...'
Sending the Email
Now that you have your SMTP connection set up and authorized your app with Google, you can finally use Python to send email with Gmail.Using the email string we constructed above, and the connected/authenticated
serverobject, you need to call the
.sendmail()method. Here is the full code, including the methods for closing the connection:
import smtplib
gmail_user = 'you@gmail.com'
gmail_password = 'P@ssword!'
sent_from = gmail_user
to = ['me@gmail.com', 'bill@gmail.com']
subject = 'OMG Super Important Message'
body = 'Hey, what's up?\n\n- You'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:server = smtplib.SMTP_SSL('smtp.gmail.com', 465)server.ehlo()server.login(gmail_user, gmail_password)server.sendmail(sent_from, to, email_text)server.close()
print 'Email sent!'
except:
print 'Something went wrong...'
Conclusion
Aside from the Gmail-specific authorization steps (involving less secure apps, etc), this code should work for just about any other email provider that supports SMTP access, assuming you have the correct server address and port. If you find that other providersput special restrictions on SMTP access like Google does, let us know! We'd like to include as much info as possible here.
Do you programmatically send emails with SMTP? What kind of applications do you use it for? Let us know in the comments!
Resources
https://docs.python.org/3.5/library/smtplib.html
http://stackoverflow.com/a/27515833/2684304
Edit: Thanks to cropr (in the comments) for pointing out that Gmail imposes a number of restrictions on people sending email with SMTP. Free accounts are limited to 500 emails per day and are rate-limited to about 20 emails per
second.
If you think you'll exceed these limits, then you may want to either set up your own SMTP server or use services like AWS SES or Sendgrid.
相关文章推荐
- Using VBScript to Send Emails with Gmail
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server. (文档 ID 885522.1)
- How to send custom http_headers for RPC calls using python xmlrpclib?
- Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?
- How to Access MySQL with Python Version 3.4
- Python for Everybody-Using Python to Access Web DatExtracting Data With Regular Expressions
- How to get the handle in windows using python
- HOW-TO:Debug Python Scripts with Eclipse
- How to configure XDB for using ftp and http protocols with ASM [ID 357714.1]
- How to Send an HTTP Header With Every Request With Spring RestTemplate
- Openstack: python API “how to download image from glance using the python api”
- TensorFlow: How to freeze a model and serve it with a python API
- python网络编程经典好文,如何用urllib获取internet资源,HOWTO Fetch Internet Resources Using The urllib Package
- How to download dynamically loaded content using python
- How To Use Linux epoll with Python
- [Cexpert-001] How to implement atoi with least codes using C language?