Smtp电子邮件失败

时间:2020-04-30 16:51:25

标签: python email

我是Python的新手,正在尝试测试一个简单的Python脚本来向自己发送电子邮件。我看了一个视频教程,完成后我不断收到此错误消息: enter image description here

我试图使用PIP安装Smtplib,但我也不断收到错误消息

错误:找不到满足smtplib要求的版本(来自版本:无)

错误:找不到smtplib的匹配分布

我在网上寻找任何帮助,但我确实很新,所以我不确定自己在做什么。

我的代码:

import os
import smtplib

EMAIL_ADRESS = os.environ.get('EMAIl_ADRESS')
EMAIl_PASSWORD = os.environ.get('EMAIl_PASSWORD')

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

    smtp.login(EMAIL_ADRESS, EMAIl_PASSWORD)

    subject = 'Testing python emails'
    body = 'This is a test of the python email script'


    msg = f'Subject: {subject}\n\n{body}'

    smtp.sendmail(EMAIL_ADRESS, 'gonetogrif@gmail.com', msg)
    print("email sent")

我在电子邮件和密码中使用了环境变量

完整的错误消息:gyazo.com/32aa4c8f51922658afc3aaf7a9797d06

1 个答案:

答案 0 :(得分:1)

您的代码没有错,但是,如果您要从电子邮件地址发送电子邮件,则需要允许“安全性较低的应用访问权限”。

https://myaccount.google.com/lesssecureapps

如果启用此选项,您将接收/发送电子邮件。
enter image description here