[英]Unable to send email through Python from Gmail on Ubuntu
我在 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 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.