Using gmail through python without smtp

Question:

As of may 30th, smtp is no longer accepted.

https://support.google.com/accounts/answer/6010255?hl=en&ref_topic=7188673

What is the new way to make a simple python emailer rather than a full application with the "login with google" option?

Not sure why I was asked for the code and error, given that I already diagnosed the issue and was asking for alternative methods.
Here it is. Its a handy emailer that texts me to workout when I work at home.

import time
import smtplib
import random
gmail_user = '[email protected]'
gmail_password = 'TheCorrectPassword'

sent_from = gmail_user
to = ['[email protected]']
exercises = ['push ups', 'jumps in place', '20lb curls', 'tricep extensions', 'quarter mile runs']
levels = [1, 2, 3]
level1 = ['10', '15', '16', '20', '1']
level2 = ['15', '30', '30', '40', '2']
level3 = ['20', '50', '48', '70', '4']
while True:
    if int(time.strftime('%H')) > 9:
        if int(time.strftime('%H')) < 23:
            abc = random.uniform(0, 1)
            picker = random.randint(0, 4)
            if abc < 0.3:
                level = level1
            if 0.3 < abc and abc < 0.8:
                level = level2
            if abc > 0.8:
                level = level3
            exersize = exercises[picker]
            amount = level[picker]
            try:
                subject = f'Test'
                body = f'Do {amount} {exersize}'
                server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
                server.ehlo()
                server.login(gmail_user, gmail_password)
                server.sendmail(sent_from, to, body)
                server.close()
                print('Email sent!')
            except Exception as error:
                print(error)
            time.sleep(random.randint(1500, 4800))
    time.sleep(100)

error:

(535, b’5.7.8 Username and Password not accepted. Learn more atn5.7.8 https://support.google.com/mail/?p=BadCredentials jj1-20020a170903048100b00163247b64bfsm7655137plb.115 – gsmtp’)

Solved below: SMTP is still accepted for app passwords.
App passwords creation steps can be found here, but you must enable 2 factor auth first, before app passwords can be created.

https://support.google.com/accounts/answer/185833
https://myaccount.google.com/security

Asked By: AlbinoRhino

||

Answers:

import smtplib

host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "[email protected]"
TO = "[email protected]"
MSG = "Subject: Test email pythonnnBody of your message!"
server.sendmail(FROM, TO, MSG)

server.quit()
print ("Email Send")
Answered By: Majed Jaber

Correction after May 30 2022, sending the users actual password is no longer accepted by googles smtp server

You should configuring an apps password this works. Then replace the password in your code with this new apps password.

An App Password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. App Passwords can only be used with accounts that have 2-Step Verification turned on.

gmail_user = '[email protected]'
gmail_password = 'AppsPassword'

This is normally due to the user having 2fa enabled.

Another option would be to use Xoauth2.

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.