【发布时间】:2020-07-13 11:23:27
【问题描述】:
我已经有了用 python 发送电子邮件的代码:
def send_email_gmail(subject, message, destination):
""" Send an e-mail using gmail with message to destination email.
Arguments:
message {str} -- message string to send.
destination {str} -- destination email (as string)
"""
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
# not a real email account nor password, its all ok!
server.login('me123@gmail.com', 'fakepassword111!!!')
# craft message
msg = EmailMessage()
message = f'{message}\n'
msg.set_content(message)
msg['Subject'] = subject
msg['From'] = 'me123@gmail.com'
msg['To'] = destination
# send msg
server.send_message(msg)
我已经阅读了解决常见错误的多个问题(Login credentials not working with Gmail SMTP 或 SMTPAuthenticationError when sending mail using gmail and python):
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sadfdgjsfgrp=1&dsfgscc=1dsdfgsfg&pldsfgt=AKsdfsdfggsdfggnsbu\n5.7.14 G0crCr0qSvWTng9xRE_pd3WnK3S2sDMsdfgsdfgX0J-xoetn7aHyFQi2qYrQisdfgsdfgKIwMCcgD7zLB1t7Z\n5.7.14 -OjHjpJqasdftBuTi9wh0sYlNW637SmPLuMnnLGn_WcZX5TGH4sddsfgXYar-Aasdfw0ctWfLhasdffPQV>\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/787521345364524 n21sm17577sadfdsf46qtn.17 - gsmtp')
无论如何,我按照这些答案的建议做了,但我仍然遇到错误。所以我决定我不想再为此使用 gmail。我从一个虚假帐户发送电子邮件只是为了发送电子邮件,所以它的安全性对我来说并不重要。
那么如何更改上面的代码,使其适用于在 python/code 中发送电子邮件更可靠的不同电子邮件服务?
想法答案将包含自包含并包含有效的示例脚本。
编辑1:
我当然检查过在我的假 gmail 上打开不太安全的应用程序功能,复制粘贴该页面内容的文本:
Turn off less secure app access
Your account is vulnerable to malicious activity because you’re allowing apps & devices that use less secure sign-in technology to access your account. You should turn off this type of access. Google will automatically turn this setting OFF if it’s not being used. Learn more
还有一个黄色感叹号警告我。
编辑2
EmailMessage()的输出:
按照建议,我粘贴了这个(空消息)。
【问题讨论】:
-
选择另一个您拥有凭据的 SMTP 服务器,并将 smtplib 指向它。或者最好使用一些非 SMTP 电子邮件网关,例如 sendgrid、mailgun、mailjet,...
-
这个网站有一个服务器列表和它们的端口:arclab.com/en/kb/email/…
-
抱歉,我不是这方面的专家,但我不知道拥有服务器凭据意味着什么。我有可以创建用于发送电子邮件的虚假电子邮件地址的凭据。这足够了吗,还是我必须做其他事情?我不在乎我使用什么,gmail 或其他什么,只要它总是发送电子邮件并且不痛苦。
-
@ThomasBrefeld 我不是这方面的专家。我不知道如何使用您发送给我的列表来更改我的脚本。有什么想法/帮助吗?
-
@Pinocchio 几年前我使用过 google 的 SMTP 服务器,它运行良好。我记得我遇到了很多问题,直到我允许使用该帐户的安全性较低的应用程序,之后一切似乎都正常了。我确定您已经尝试过了,但您能否确保该帐户允许安全性较低的应用程序。您还可以发布“EmailMessage()”还是它返回的内容?