【问题标题】:How do I now (since June 2022) send an email via Gmail using a Python script?我现在(自 2022 年 6 月起)如何使用 Python 脚本通过 Gmail 发送电子邮件?
【发布时间】:2022-08-03 05:55:00
【问题描述】:

我有一个 Python 脚本可以做到这一点。我必须在 Gmail 帐户中启用某些东西。大概 3 年的时间里,脚本然后像这样运行:

import smtplib, ssl
...
subject = \'some subject message\'
body = \"\"\"text body of the email\"\"\"
sender_email = \'my_gmail_account_name@gmail.com\'
receiver_email = \'some_recipient@something.com\'

# Create a multipart message and set headers
message = MIMEMultipart()
message[\'From\'] = \'Mike\'
message[\'To\'] = receiver_email
message[\'Subject\'] = subject
# Add body to email
message.attach(MIMEText(body, \'plain\'))
# Open file in binary mode
with open( client_zip_filename, \'rb\') as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase(\'application\', \'octet-stream\')
    part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
    \'Content-Disposition\',
    f\'attachment; filename={subject}\',
)
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL(\'smtp.gmail.com\', 465, context=context) as server:
    print( \'waiting to login...\')
    server.login(sender_email, password)
    print( \'waiting to send...\')
    server.sendmail(sender_email, receiver_email, text)
print( \'email appears to have been sent\')

今年 5 月左右,我收到了来自 Google 的消息,称使用脚本电子邮件的权限将被收紧。 “哦,亲爱的”,我想。

6 月的某个时候,我发现上面的脚本不再起作用,并引发了一个异常,特别是在 server.login(sender_email, password) 行:

  ...
  File \"D:\\My documents\\software projects\\operative\\sysadmin_py\\src\\job_backup_routine\\__main__.py\", line 307, in main
    server.login(sender_email, password)
  File \"c:\\users\\mike\\appdata\\local\\programs\\python\\python39\\lib\\smtplib.py\", line 745, in login
    raise last_exception
  File \"c:\\users\\mike\\appdata\\local\\programs\\python\\python39\\lib\\smtplib.py\", line 734, in login
    (code, resp) = self.auth(
  File \"c:\\users\\mike\\appdata\\local\\programs\\python\\python39\\lib\\smtplib.py\", line 657, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b\'5.7.8 Username and Password not accepted. 
Learn more at\\n5.7.8  https://support.google.com/mail/?p=BadCredentials p14-20020aa7cc8e000000b00435651c4a01sm8910838edt.56 - gsmtp\')

...因此,我对此并不完全感到惊讶,现在已经开始寻找解决方案。

  • 我有这样的想法,即前进的道路是一种叫做“OAuth 同意”的东西(我不知道这是什么......)

  • 我找到了this answer 并尝试按照那里的步骤进行操作。这是我尝试执行步骤 1 的帐户:

  • 我去了这个谷歌配置页面并选择了“my_gmail_account_name”,我想从这个账户发送电子邮件......

  • 新建“项目”,名称:test-project-2022-07-18

  • 位置:默认(“无组织”)

  • 单击创建

  • 点击下一步

  • 点击启用

  • 单击该图标以启用“Google Developer Console”

  • 在汉堡菜单(左上角)有一个项目“APIs and services”...一个项目有“Credentials”——点击

  • 左侧列表中的一项是“OAuth 同意屏幕”

  • 另一个项目是“凭据”。单击此:然后,在顶部,\“+ CREATE CREDENTIALS\”

  • 在下拉菜单中,选择“OAuth 客户端 ID”

  • 单击“配置同意屏幕”

  • 单选按钮:“内部”和“外部”。选择了后者。

  • 单击“创建”

  • 在“应用信息”下:

  • \"应用名称\": sysadmin_py

  • \"用户支持邮箱\": my_gmail_account_name@gmail.com

  • \"开发者联系信息\": my_gmail_account_name@gmail.com

  • 单击“保存并继续”

  • 然后发现自己在一个关于“范围”的页面上,带有一个按钮“添加或删除范围”......

此时我的意思是按照“步骤 1”指令“d。选择应用程序类型其他,输入名称“Gmail API 快速入门”并单击“创建”按钮...但没有这类的就在眼前!

该答案的更新是在 2021 年 4 月完成的。一年后,谷歌的界面似乎发生了根本性的变化。或者,也许我走错了路,消失在一个兔子洞里。

我不知道该怎么做。任何人都可以帮忙吗?

标签: python google-cloud-platform gmail email-client


【解决方案1】:

Google 最近对不太安全的应用程序的访问进行了更改(在此处阅读:https://myaccount.google.com/lesssecureapps)。

为了使您的脚本再次工作,您需要为其设置一个新的应用程序密码。这样做的指导如下:

  • 转到我的帐户在 Gmail 中并点击安全. 之后,向下滚动以选择登录 Google选项。
  • 现在,点击应用密码. (笔记:启用两步验证时可以看到此选项)。要启用两步验证:
    1. 在登录 Google 中,单击两步验证选项,然后输入密码。
    2. 然后通过输入手机上收到的 OTP 代码打开两步验证。

(这里是同一页面的快速链接:https://myaccount.google.com/apppasswords

  • 在这里,您可以看到一个应用程序列表,选择所需的一个。
  • 接下来,选择选择设备选项并单击用于操作 Gmail 的设备。
  • 现在,点击产生.
  • 之后,输入密码显示在黄色栏中。
  • 最后,点击完毕.

(来源:https://www.emailsupport.us/blog/gmail-smtp-not-working/

只需切换脚本用于这个新生成的应用程序密码的密码。这对我有用,我希望你也一样。

我希望这有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-28
    • 2015-01-16
    • 2016-09-09
    • 2013-01-13
    • 2020-10-14
    • 2012-10-01
    • 2012-01-07
    相关资源
    最近更新 更多