python - Flask Web开发,用户注册完需要邮箱确认问题
PHP中文网
PHP中文网 2017-04-17 18:01:18
0
5
861

我在自学Flask,在看《FlaskWeb开发:基于Python的Web应用开发实战》这本书,目前已经做到了用户注册这一块,卡在了用户注册完需要邮箱确认才可以更完整的编辑信息,是不是用Venv模拟环境下是不能发送邮件的?这一点很迷惑,希望有前辈能指点一下。


界面图片如下:

代码是从官方github克隆的。

from ..email import send_email
@auth.route('/register', methods = ['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
# ...
db.session.add(user)
db.session.commit()
token = user.generate_confirmation_token()
send_email(user.email, 'Confirm Your Account',
'auth/email/confirm', user=user, token=token)
flash('A confirmation email has been sent to you by email.')
return redirect(url_for('main.index'))
return render_template('auth/register.html', form=form)

报错

send: 'ehlo [192.168.0.66]\r\n'
reply: '250-smtp.googlemail.com at your service, [119.130.184.228]\r\n'
reply: '250-SIZE 35882577\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-CHUNKING\r\n'
reply: '250 SMTPUTF8\r\n'
reply: retcode (250); Msg: smtp.googlemail.com at your service, [119.130.184.228
]
SIZE 35882577
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
PIPELINING
CHUNKING
SMTPUTF8
send: 'STARTTLS\r\n'
reply: '220 2.0.0 Ready to start TLS\r\n'
reply: retcode (220); Msg: 2.0.0 Ready to start TLS
send: 'ehlo [192.168.0.66]\r\n'
reply: '250-smtp.googlemail.com at your service, [119.130.184.228]\r\n'
reply: '250-SIZE 35882577\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-CHUNKING\r\n'
reply: '250 SMTPUTF8\r\n'
reply: retcode (250); Msg: smtp.googlemail.com at your service, [119.130.184.228
]
SIZE 35882577
8BITMIME
AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
ENHANCEDSTATUSCODES
PIPELINING
CHUNKING
SMTPUTF8
send: 'mail FROM:<flasky@example.com> size=1748\r\n'
reply: '530-5.5.1 Authentication Required. Learn more at\r\n'
reply: '530 5.5.1  https://support.google.com/mail/answer/14257 eh9sm8453423pad.
47 - gsmtp\r\n'
reply: retcode (530); Msg: 5.5.1 Authentication Required. Learn more at
5.5.1  https://support.google.com/mail/answer/14257 eh9sm8453423pad.47 - gsmtp
send: 'rset\r\n'
reply: '250 2.1.5 Flushed eh9sm8453423pad.47 - gsmtp\r\n'
reply: retcode (250); Msg: 2.1.5 Flushed eh9sm8453423pad.47 - gsmtp
send: 'quit\r\n'
reply: '221 2.0.0 closing connection eh9sm8453423pad.47 - gsmtp\r\n'
reply: retcode (221); Msg: 2.0.0 closing connection eh9sm8453423pad.47 - gsmtp
Exception in thread Thread-3:
Traceback (most recent call last):
  File "c:\python27\Lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "c:\python27\Lib\threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\Administrator\myproject\flasky\app\email.py", line 9, in send_a
sync_email
    mail.send(msg)
  File "C:\Users\Administrator\myproject\venv\lib\site-packages\flask_mail.py",
line 416, in send
    message.send(connection)
  File "C:\Users\Administrator\myproject\venv\lib\site-packages\flask_mail.py",
line 351, in send
    connection.send(self)
  File "C:\Users\Administrator\myproject\venv\lib\site-packages\flask_mail.py",
line 168, in send
    message.as_string())
  File "c:\python27\Lib\smtplib.py", line 731, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1  h
ttps://support.google.com/mail/answer/14257 eh9sm8453423pad.47 - gsmtp', 'Flasky
 Admin <flasky@example.com>')
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
PHPzhong

Tips, I have solved this problem. I also use Gmail and the error I encountered is the same as what you posted. I guess the questioner should also be using PyCharm, right?

Yes, the pot is PyCharm! Configure flask to read MAIL_USERNAMEMAIL_PASSWORDthese two configuration items from the environment variables of venv, and at the same time use venv as the interpreter to run the program in PyCharm, such a situation will occur.

The reason is that the environment variables are not read in PyCharm, which means that the two configuration items mentioned above have a value of None when the program is running (you can see this by setting a breakpoint in config.py), so it will This 530 error was reported because you didn’t log in to your email at all = =

I used venv to write the export of environment variables into the venv's activate file (the file used when starting venv). I think PyCharm did not read the contents of this file. To solve this problem, there are two ways:

  1. Use the terminal to run the program, and it has been tested and sent successfully;

  2. PyCharm has a configuration menu for the current project interpreter, which contains an environment variable configuration. If you add environment variables here, you can use PyCharm to run the program and happily send a confirmation email;

By the way, thank you for this question - flask-mail used environment variables to extract account passwords and sent emails for the inspiration it gave me.

Peter_Zhu

Look at the end of your error log, http 530, and you have not logged in to the mailbox. Check your account and password, or you cannot access your mailbox locally at all

伊谢尔伦

The official email address is Google. You can change it to qq or 163.

Ty80

530, '5.5.1 Authentication Required.

This prompt indicates that you need to be authenticated before you can send emails, and you cannot send anonymous emails.

小葫芦

After reading a circle of error logs, I think the problem is that you have not modified the email information and are using the original code: flasky@example.com email, so you obviously cannot have its password.

So what you need to do is find the email and password configuration in the code and modify it to your own. As for the fact that you can't connect to gmail, the printed content shows that you can connect, so there is no need to worry about this problem.

If you want to be more specific, you can open the config.py file, find line 13, and then modify these two variables

MAIL_USERNAME = '你的邮箱'
MAIL_PASSWORD = '你的邮箱密码'

Try running it again, it should be fine.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!