python - 部署到heroku后,无法发送邮件,哪里出了问题?
天蓬老师
天蓬老师 2017-04-18 09:58:26
0
1
559
  1. 部署在heroku后,注册用户时无法发送邮件,本地测试时没有问题。

  2. 本地测试是在虚拟环境下测试,没模拟部署环境测试

  3. google后看到的前几个大多是因为用了gmail的原因,我用的是网易邮箱。

返回的logs:

2016-11-22T10:22:19.945749+00:00 heroku[router]: at=info method=GET path="/auth/unconfirmed" host=oflasky1122a.herokuapp.com request_id=09fc8f05-a0af-45a5-8329-58eef0223d05 fwd="1.62.97.88" dyno=web.1 connect=1ms service=21ms status=200 bytes=4297 2016-11-22T10:22:53.409764+00:00 app[web.1]: Exception in thread Thread-3: 2016-11-22T10:22:53.409772+00:00 app[web.1]: Traceback (most recent call last): 2016-11-22T10:22:53.409774+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/threading.py", line 801, in __bootstrap_inner 2016-11-22T10:22:53.409776+00:00 app[web.1]: self.run() 2016-11-22T10:22:53.409777+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/threading.py", line 754, in run 2016-11-22T10:22:53.409779+00:00 app[web.1]: self.__target(*self.__args, **self.__kwargs) 2016-11-22T10:22:53.409779+00:00 app[web.1]: File "/app/app/email.py", line 9, in send_async_email 2016-11-22T10:22:53.409780+00:00 app[web.1]: mail.send(msg) 2016-11-22T10:22:53.409780+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask_mail.py", line 492, in send 2016-11-22T10:22:53.409781+00:00 app[web.1]: message.send(connection) 2016-11-22T10:22:53.409781+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask_mail.py", line 152, in __exit__ 2016-11-22T10:22:53.409782+00:00 app[web.1]: self.host.quit() 2016-11-22T10:22:53.409782+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/smtplib.py", line 772, in quit 2016-11-22T10:22:53.409783+00:00 app[web.1]: res = self.docmd("quit") 2016-11-22T10:22:53.409783+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/smtplib.py", line 393, in docmd 2016-11-22T10:22:53.409783+00:00 app[web.1]: self.putcmd(cmd, args) 2016-11-22T10:22:53.409784+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/smtplib.py", line 341, in putcmd 2016-11-22T10:22:53.409784+00:00 app[web.1]: self.send(str) 2016-11-22T10:22:53.409785+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/smtplib.py", line 333, in send 2016-11-22T10:22:53.409785+00:00 app[web.1]: raise SMTPServerDisconnected('please run connect() first') 2016-11-22T10:22:53.409785+00:00 app[web.1]: SMTPServerDisconnected: please run connect() first

返回以上错误时smtp的配置是MAIL_PORT = 25,MAIL_USE_TLS = True

相关代码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 从哪里分析呢?可能是哪出了问题呢?

这还有个解决办法不过代码我没有看懂。


stmp配置:

class Config: SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string' SSL_DISABLE = False MAIL_SERVER = 'smtp.163.com' MAIL_PORT = 465 MAIL_USE_SSL = True MAIL_USERNAME = os.environ.get('MAIL_USERNAME') MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') FLASKY_MAIL_SUBJECT_PREFIX = '[oFlasky]' FLASKY_MAIL_SENDER = 'b1a2b3c4d@163.com' FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')

前一次配置文件中的MAIL_USE_SSL没有配置,而是使用的TLS,MAIL_PORT 也是按照网易官网写的25。今天改成以上的配置了,还是返回错误:

2016-11-25T08:06:23.816541+00:00 heroku[router]: at=info method=GET path="/auth/unconfirmed" host=oflasky1125.herokuapp.com request_id=89ce5af6-937b-42e6-903b-37adb7798164 fwd="1.58.89.48" dyno=web.1 connect=1ms service=21ms status=200 bytes=4381 2016-11-25T08:09:16.705329+00:00 app[web.1]: Exception in thread Thread-2: 2016-11-25T08:09:16.705345+00:00 app[web.1]: Traceback (most recent call last): 2016-11-25T08:09:16.705347+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/threading.py", line 801, in __bootstrap_inner 2016-11-25T08:09:16.705348+00:00 app[web.1]: self.run() 2016-11-25T08:09:16.705349+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/threading.py", line 754, in run 2016-11-25T08:09:16.705350+00:00 app[web.1]: self.__target(*self.__args, **self.__kwargs) 2016-11-25T08:09:16.705351+00:00 app[web.1]: File "/app/app/email.py", line 9, in send_async_email 2016-11-25T08:09:16.705351+00:00 app[web.1]: mail.send(msg) 2016-11-25T08:09:16.705352+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask_mail.py", line 491, in send 2016-11-25T08:09:16.705353+00:00 app[web.1]: with self.connect() as connection: 2016-11-25T08:09:16.705353+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask_mail.py", line 144, in __enter__ 2016-11-25T08:09:16.705354+00:00 app[web.1]: self.host = self.configure_host() 2016-11-25T08:09:16.705355+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask_mail.py", line 156, in configure_host 2016-11-25T08:09:16.705355+00:00 app[web.1]: host = smtplib.SMTP_SSL(self.mail.server, self.mail.port) 2016-11-25T08:09:16.705356+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/smtplib.py", line 801, in __init__ 2016-11-25T08:09:16.705357+00:00 app[web.1]: SMTP.__init__(self, host, port, local_hostname, timeout) 2016-11-25T08:09:16.705357+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/smtplib.py", line 256, in __init__ 2016-11-25T08:09:16.705358+00:00 app[web.1]: (code, msg) = self.connect(host, port) 2016-11-25T08:09:16.705359+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/smtplib.py", line 316, in connect 2016-11-25T08:09:16.705359+00:00 app[web.1]: self.sock = self._get_socket(host, port, self.timeout) 2016-11-25T08:09:16.705360+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/smtplib.py", line 806, in _get_socket 2016-11-25T08:09:16.705361+00:00 app[web.1]: new_socket = socket.create_connection((host, port), timeout) 2016-11-25T08:09:16.705362+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/socket.py", line 575, in create_connection 2016-11-25T08:09:16.705362+00:00 app[web.1]: raise err 2016-11-25T08:09:16.705363+00:00 app[web.1]: error: [Errno 110] Connection timed out

错误不一样了。。。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (1)
迷茫

Post your SMTP configuration for sending emails, try changing SMTP port 25 to 587

    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!