This page looks best with JavaScript enabled

Extra steps to send email with python from gmail

 ·   ·  β˜• 3 min read  ·  ✍️ Ohidur Rahman Bappy

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 “less 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 SMTPAuthenticationError with 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 login method:

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 server object, 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 providers 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!


Ohidur Rahman Bappy
WRITTEN BY
Ohidur Rahman Bappy
πŸ“šLearner 🐍 Developer