1.学习Flask Web开发这本书,在8.6.2发送确认邮件的时候没收到电子邮件,邮箱我改成qq邮箱了,但是报错了,感觉像是和线程有关。
(1)email.py的代码
from threading import Thread from flask import current_app, render_template from flask_mail import Message from .import mail def send_async_email(app,msg): with app.app_context(): mail.send(msg) def send_email(to, subject, template, **kwargs): app = current_app._get_current_object() msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject, sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to]) msg.body = render_template(template + '.txt', **kwargs) msg.html = render_template(template + '.html', **kwargs) thr = Thread(target=send_async_email, args=[app,msg]) thr.start() return thr
(2)和邮件相关的配置
MALL_SERVER = 'smtp.qq.com' MAIL_PORT = 25 MAIL_USE_TLS = True MAIL_USERNAME = os.environ.get('MAIL_USERNAME') MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') FLASKY_MAIL_SUBJECT_PREFIX = '[FLASKY]' FLASKY_MAIL_SENDER = '480***140@qq.com'
(3)注册用户的时候发送确认代码的路由
@auth.route('/register', methods=['GET', 'POST']) def register(): form = RegistrationForm() if form.validate_on_submit(): user = User(email=form.email.data, username=form.username.data, password=form.password.data) 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 your email.') return redirect(url_for('auth.login')) return render_template('auth/register.html', form=form)
(4)报错信息
Exception in thread Thread-2: Traceback (most recent call last): File "E:\python2.7.9\Lib\threading.py", line 810, in __bootstrap_inner self.run() File "E:\python2.7.9\Lib\threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) File "F:\flasky\app\emali.py", line 9, in send_async_email mail.send(msg) File "F:\flasky\venv\lib\site-packages\flask_mail.py", line 491, in send with self.connect() as connection: File "F:\flasky\venv\lib\site-packages\flask_mail.py", line 144, in __enter__ self.host = self.configure_host() File "F:\flasky\venv\lib\site-packages\flask_mail.py", line 158, in configure_host host = smtplib.SMTP(self.mail.server, self.mail.port) File "E:\python2.7.9\Lib\smtplib.py", line 256, in __init__ (code, msg) = self.connect(host, port) File "E:\python2.7.9\Lib\smtplib.py", line 316, in connect self.sock = self._get_socket(host, port, self.timeout) File "E:\python2.7.9\Lib\smtplib.py", line 291, in _get_socket return socket.create_connection((host, port), timeout) File "E:\python2.7.9\Lib\socket.py", line 571, in create_connection raise err error: [Errno 10061]
2.然后我回到第六章再重新撸了一遍邮件的内容。
在python shell中发送电子邮件的部分。还是没成功。
报错的内容和上面感觉很相似,但是特么的看不懂啊。
Traceback (most recent call last): File "<console>", line 2, in <module> File "D:\flasky\flasky\venv\lib\site-packages\flask_mail.py", line 415, in send with self.connect() as connection: File "D:\flasky\flasky\venv\lib\site-packages\flask_mail.py", line 123, in __enter__ self.host = self.configure_host() File "D:\flasky\flasky\venv\lib\site-packages\flask_mail.py", line 137, in configure_host host = smtplib.SMTP(self.mail.server, self.mail.port) File "E:\python2.7.9\Lib\smtplib.py", line 256, in __init__ (code, msg) = self.connect(host, port) File "E:\python2.7.9\Lib\smtplib.py", line 316, in connect self.sock = self._get_socket(host, port, self.timeout) File "E:\python2.7.9\Lib\smtplib.py", line 291, in _get_socket return socket.create_connection((host, port), timeout) File "E:\python2.7.9\Lib\socket.py", line 571, in create_connection raise err error: [Errno 10060] >>>
3.一开始撸第六章的时候是能够成功发送邮件的,不过是9月份的事了,不知道和时间有木有关系。扣扣的SMTP服务我又关掉然后重新开启了一遍也还是不行。
4.一开始用官方代码没改成qq邮箱的时候也是报的一样的错,不知道啥情况啊。