简体   繁体   English

从Flask-Mail发送邮件(SMTPSenderRefused 530)

[英]Sending a mail from Flask-Mail (SMTPSenderRefused 530)

The app configuration used in a Flask Mail application (following Miguel Grinberg Flask developlemt book) : Flask Mail应用程序中使用的应用程序配置(遵循Miguel Grinberg Flask developlemt书籍):

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

The Mail Username and Password variables have been set correctly and rechecked. 邮件用户名和密码变量已正确设置并重新检查。 While trying to send a message using the following code, 在尝试使用以下代码发送消息时,

from flask.ext.mail import Message
from hello import mail
msg = Message('test subject', sender='same as MAIL_USERNAME', recipients=['check@mail.com'])
msg.body = 'text body'
msg.html = '<b>HTML</b> body'
with app.app_context():
    mail.send(msg)

While sending, the application is again and again resulting in the following error: 发送时,应用程序会一次又一次地导致以下错误:

SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qb10sm6828974pbb.9 - gsmtp', u'configured MAIL_USERNAME')

Any workaround for the error? 该错误的解决方法是什么?

While digging into the issues faced, I rechecked the SMTP settings for Google, 在深入研究所面临的问题时,我重新检查了Google的SMTP设置,

Google SMTP设置

Changing the 改变了

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'

to

app.config['MAIL_SERVER'] = 'smtp.gmail.com'

did the trick. 做了伎俩。

Also make sure that the full username is used as Gmail SMTP username , ie, example@gmail.com as shown in the image above. 另外,请确保将完整用户名用作Gmail SMTP用户名 ,即example@gmail.com,如上图所示。

Hope this helps!!! 希望这可以帮助!!!

I also followed this book and get the same problem, after some digging here and there, I found out the root cause of the problem. 我也跟着这本书并得到了同样的问题,经过一些挖掘,我找到了问题的根本原因。 However, I am not sure whether it will be the same case for you or not. 但是,我不确定你是否也会这样。

app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

As you can see your flask app gets the your email credentials through os.environ.get() , and if you set this environment variables temporarily in your system, in my case Mac OSX, after your terminal session they will be gone, so you need to set them again for the next time you enter the terminal, like below: 正如您所看到的,您的烧瓶应用程序通过os.environ.get()获取您的电子邮件凭据,如果您在系统中临时设置此环境变量,在我的Mac OSX案例中,在您的终端会话之后它们将会消失,因此您需要在下次进入终端时再次设置它们,如下所示:

export MAIL_USERNAME=**YOUR EMAIL**
export PASSWORD=**YOUR PASSWORD**

I got this error because of this scenario, in order to set them permanently you need to include these variables into .bash_profile file in your home directory. 由于这种情况,我收到此错误,为了永久设置它们,您需要将这些变量包含在主目录中的.bash_profile文件中。

You need to change your Google account settings. 您需要更改自己的Google帐户设置。 On this page , turn on the option to "Allow less secure apps". 此页面上 ,启用“允许安全性较低的应用”选项。

As that page says: 正如那页所说:

Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. 某些应用和设备使用安全性较低的登录技术,这会使您的帐户更容易受到攻击。 You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks. 您可以关闭我们建议的这些应用的访问权限,或者如果您想要使用它们,请尽快启用访问权限。 Learn more 学到更多

Do these 2 things to resolve: 做这两件事要解决:

  1. Use this link and turn on 'Allow less secure apps'- https://myaccount.google.com/lesssecureapps 使用此链接启用“允许安全性较低的应用” - https://myaccount.google.com/lesssecureapps

  2. Use hard-coded value for email and password and it worked fine. 使用硬编码的电子邮件和密码值,它工作正常。 Simply in the file ' init .py', edit the following section: 只需在文件' init .py'中,编辑以下部分:

Don't use os.environ.get 不要使用os.environ.get

app.config['MAIL_USERNAME'] = 'youremail@gmail.com'
app.config['MAIL_PASSWORD'] = 'yourpassword'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM