How to send emails with gmail using python

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:


google is not allowing you to log in via smtplib because it has flagged this sort of login as “less secure”, so what you have to do is go to this link while you’re logged in to your google account, and allow the access:

allowgmailpython

Once that is set (see my screenshot below), it should work.

mailsendbypython