[Solved] How to fix AttributeError: “NoneType” object has no attribute “encode” using smtplib
I tried to make a script to allow a user to send the contents of a file via gmail.
This is is my code:
if input1 == 'mail':
path = input("open -- ")
with open(path, "r") as file:
addr = os.environ.get("email")
pw = os.environ.get("pass")
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(addr, pw)
reading = file.read()
subject = path
body = reading
msg = f'Subject {subject}nn{body}'
input3 = ('recipient -- ')
server.sendmail(email, input3, msg)'
and it’s giving me this error:
File "C:/Users/Whit/PycharmProjects/rps/venv/Scripts/maxnum.py", line 36, in <module>
server.login(addr, pw)
File "C:UsersWhitAppDataLocalProgramsPythonPython37-32libsmtplib.py", line 721, in login
initial_response_ok=initial_response_ok)
File "C:UsersWhitAppDataLocalProgramsPythonPython37-32libsmtplib.py", line 638, in auth
authobject(challenge).encode('ascii'), eol='')
AttributeError: 'NoneType' object has no attribute 'encode'
Solution #1:
You are doing server.login(addr, pw)
, but getting addr
and pw
through environment variables. It seems you have not actually passed them like that.
Solution #2:
This usually occurs when you create the environment variables but forget to restart your editor. So a simple restart should fix the problem.
Solution #3:
Some value you provided to smtplib
is None
. Most likely is I’d the result of one or both of the calls to getent
.
What is your program expected to do if email
or pass
environment variables are not set? You should probably check the result of getent
before continuing.
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .