python - Flask SMTP 身份验证错误

标签 python email smtp flask flask-mail

我在向网页添加联系页面时遇到问题。我正在使用 Flask 来做到这一点。 我不明白的部分是:

app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = '''<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e0fde4e8f5e9e0c5e2e8e4ece9abe6eae8" rel="noreferrer noopener nofollow">[email protected]</a><script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");
l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';
r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;
s+=String.fromCharCode(c);}s=document.createTextNode(s);
l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>]'''

app.config["MAIL_PASSWORD"] = 'password'

msg = Message(form.subject.data, sender='''<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a1adacb6a3a1b682a5afa3abaeeca1adaf" rel="noreferrer noopener nofollow">[email protected]</a><script
type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");
l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';
r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;
s+=String.fromCharCode(c);}s=document.createTextNode(s);
l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>', recipients=['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64011c0509140801240309050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a><script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");
l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';
r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;
s+=String.fromCharCode(c);}s=document.createTextNode(s);
l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>]''')

请解释一下我应该在代码中的以下位置放置哪个电子邮件地址,我应该在哪里添加我的电子邮件 ID?另一个应该是什么。我没有自己的域。

app.config["MAIL_USERNAME"] = '''<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cda8b5aca0bda1a88daaa0aca4a1e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>
app.config["MAIL_PASSWORD"] = 'password'

msg = Message(form.subject.data, sender='''<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cba8a4a5bfaaa8bf8baca6aaa2a7e5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a>
recipients=['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a7f627b776a767f5a7d777b737634797577" rel="noreferrer noopener nofollow">[email protected]</a> . . 

最佳答案

该教程似乎有点过于复杂。不知道为什么你需要那么多 JavaScript。以下是我已经测试过的对我有用的方法:

gmail 的配置设置应该类似于:

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="592a3c373d3c2b193e34383035773a3634" rel="noreferrer noopener nofollow">[email protected]</a>'
app.config['MAIL_PASSWORD'] = 'password'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True

在上面的代码中,将 app.config['MAIL_USERNAME'] 替换为您的实际 Gmail 地址。密码也一样。使用此设置,电子邮件将从您的 Gmail 帐户发送到收件人。上述所有信息均适用于发件人,即您的 Gmail 帐户。

然后,要发送电子邮件,只需执行以下操作

msg = Message(subject,sender="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4734222923223507202a262e2b6924282a" rel="noreferrer noopener nofollow">[email protected]</a>",recipients=['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="493c3a2c3b78092e24282025672a2624" rel="noreferrer noopener nofollow">[email protected]</a>','<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c5c3d5c282f0d7ddd1d9dc9ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>'])
msg.body = "test email"
mail.send(msg)

在上面的代码中,将recipient替换为您想要向其发送电子邮件的任何人。收件人是一个列表。因此您可以根据需要输入 1 个或多个电子邮件地址。

最后,使用上面2,flask-mail会发送一封来自[email protected]的邮件(即您)发送至[email protected] , [email protected]等(即接收者)

关于python - Flask SMTP 身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22230372/

相关文章:

python - 字典在 for 循环结束时变为 NoneType

python - 为什么 AdamOptimizer 似乎没有应用正确的梯度?

java - 如何使用 JavaMail API 接收已发送电子邮件的确认信息?

c# - 通过带有自签名 SSL 证书的邮件服务器从 .net 应用程序发送电子邮件

laravel - Google Workplace 535-5.7.8 用户名和密码未被接受

python - 如何在 Python 中覆盖 PySerial 的 "__del__"方法?

python - 仅当列值是字符串时才将它们转换为小写

c# - 在 ASP.NET 中以编程方式发送带附件的邮件

ruby-on-rails - Rails - 电子邮件链接验证和电话号码验证以及 URL 验证

python - 如何通过 Django 发送电子邮件?