mik*_*ent 9 python gmail email-client google-cloud-platform
我有一个 Python 脚本可以做到这一点。我必须在 Gmail 帐户中启用某些功能。大概三年来,脚本都是这样运行的:
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')
Run Code Online (Sandbox Code Playgroud)
今年五月左右,我收到谷歌的一条消息,称将收紧使用脚本电子邮件的权限。“哦,亲爱的”,我想。
六月的某个时候,我发现上面的脚本不再起作用,并引发了一个异常,特别是在这一行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')
Run Code Online (Sandbox Code Playgroud)
... 因此,我对此并不完全感到惊讶,现在已经开始寻找解决方案。
我有一个想法,前进的道路是所谓的“OAuth 同意”(我不知道这是什么......)
我找到了这个答案并尝试按照那里的步骤操作。以下是我尝试遵循步骤 1 的说明:
我进入此 Google 配置页面并选择“my_gmail_account_name”,我想要从该帐户发送电子邮件...
新“项目”,名称:test-project-2022-07-18
位置:默认(“无组织”)
单击“创建”
单击“下一步”
单击启用
单击该图标以启用“Google Developer Console”
在汉堡菜单(左上角)中有一项“API 和服务”...其中一项是“凭据” - 单击
左侧列表中的一项是“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 月完成。一年后,谷歌的界面似乎发生了根本性的变化。或者也许我走错了路,消失在兔子洞里。
我不知道该怎么做。有人可以帮忙吗?
小智 25
Google 最近对不太安全的应用程序的访问进行了更改(请阅读此处:https: //myaccount.google.com/lesssecureapps)。
为了使您的脚本再次工作,您需要为其创建一个新的应用程序密码。操作说明如下:
(这是同一页面的快速链接: https: //myaccount.google.com/apppasswords)
(来源: https: //www.emailsupport.us/blog/gmail-smtp-not-working/)
只需切换脚本用于新生成的应用程序密码的密码即可。这对我有用,我希望对你也有用。
我希望这有帮助!