如何修复AttributeError:“NoneType”对象没有使用smtplib的属性“encode”

2024-05-15 17:46:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图制作一个脚本,允许用户通过gmail发送文件的内容。你知道吗

这是我的密码:

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}\n\n{body}'
        input3 = ('recipient -- ')
        server.sendmail(email, input3, msg)'

它给了我一个错误:

  File "C:/Users/Whit/PycharmProjects/rps/venv/Scripts/maxnum.py", line 36, in <module>
    server.login(addr, pw)
  File "C:\Users\Whit\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "C:\Users\Whit\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 638, in auth
    authobject(challenge).encode('ascii'), eol='')
AttributeError: 'NoneType' object has no attribute 'encode'

Tags: pathinpyserverlineloginopenusers
1条回答
网友
1楼 · 发布于 2024-05-15 17:46:10

您提供给smtplib的某些值是None。最有可能的情况是,我得到了对getent的一个或两个调用的结果。你知道吗

如果未设置emailpass环境变量,您的程序将执行什么操作?在继续之前,您可能应该检查getent的结果。你知道吗

相关问题 更多 >