gpt4 book ai didi

python - 无法通过 Python 从 Ubuntu 上的 Gmail 发送电子邮件

转载 作者:行者123 更新时间:2023-12-04 19:27:23 28 4
gpt4 key购买 nike

我在 Ubuntu 20 上运行 DigitalOcean 服务器并尝试发送通知电子邮件。该代码在我通过浏览器手动登录到 gmail 帐户的 Windows 上运行良好。但是,在 Ubuntu 上,我收到 534 身份验证错误。问题是 Google 不支持非 JavaScript 浏览器,所以我无法通过任何命令行登录,例如 Lynx 或 Links/Links2。

Traceback (most recent call last):
File "test_mail.py", line 35, in <module>
server.login(sender_email,password)
File "/usr/lib/python3.8/smtplib.py", line 743, in login
raise last_exception
File "/usr/lib/python3.8/smtplib.py", line 732, in login
(code, resp) = self.auth(
File "/usr/lib/python3.8/smtplib.py", line 655, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu\n5.7.14 aRDSZeqgJKpUYAv2MTRm76Oo727636gw5NP42UvNlPQh0VEh8P07whxbnBOiG2MTq7HZ3\n5.7.14 dBDFINUjU1AZM8pqPiwKjer9l9JNmPdYv0DPu_aJAV3pwbKiI7rCBX1ouOqKJzlJ>\n5.7.14 Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 h22sm1030609wmq.14 - gsmtp')
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path

sender_email = "myemail@gmail.com"
receiver_email = "targetmail@outlook.com"
password = "mypassword"
SUBJECT = 'test email'

msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = SUBJECT
TEXT = 'This is my message'
msg.attach(MIMEText(TEXT, 'plain'))

file_location = 'test.xlsx'
filename = 'test.xlsx'

attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email,password)
text = msg.as_string()
server.sendmail(sender_email, receiver_email, text)
server.quit()

最佳答案

您应该启用“不太安全的应用程序”访问(找不到文档链接,谷歌计划最终关闭此功能 AFAIR)或使用“应用程序特定密码”(https://support.google.com/accounts/answer/185833)

关于python - 无法通过 Python 从 Ubuntu 上的 Gmail 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69810841/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com