'NoneType' object has no attribute 'encode' and SMTP 334 Error

Viewed 563

I am currently try to send an email to myself. I have done this before without any problems, but when I am trying it again today I ran into two separate errors.

import smtplib
import os
   
EMAIL_ADDRESS = os.environ.get('GMAIL_USER')
EMAIL_PASSWORD = os.environ.get('GMAIL_PASS')

with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()

    smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
    subject = 'Subject'
    body = 'hello'
    msg = f'Subject: {subject}\n\n{body}'
    smtp.sendmail(EMAIL_ADDRESS, 'email@gmail.com', msg)

    # smtp.send_message(msg)

This is the message I was returned with

Traceback (most recent call last):
  File "/Users/Desktop/Python_Projects/climbing_notif/Test.py", line 19, in <module>
    smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
  File "/Users/opt/anaconda3/lib/python3.7/smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "/Users/opt/anaconda3/lib/python3.7/smtplib.py", line 638, in auth
    authobject(challenge).encode('ascii'), eol='')
AttributeError: 'NoneType' object has no attribute 'encode'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Desktop/Python_Projects/climbing_notif/Test.py", line 23, in <module>
    smtp.sendmail(EMAIL_ADDRESS, 'email@gmail.com', msg)
  File "/Users/opt/anaconda3/lib/python3.7/smtplib.py", line 280, in __exit__
    raise SMTPResponseException(code, message)
smtplib.SMTPResponseException: (334, b'UGFzc3dvcmQ6')

I would appreciate any help, thank you!

2 Answers

While running your code, I got the same error and I don't have the same environment variables on my device. One possible reason could be that the environment variables that you have entered don't exist, or were somehow deleted.

I had the same problem, I solved it by simply putting my email and password in the code. It's not secure to do it this way though

Related