代码是从官方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>')
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_USERNAME
和MAIL_PASSWORD
these 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:Use the terminal to run the program, and it has been tested and sent successfully;
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.
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.
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
Try running it again, it should be fine.