flask 郵件發不出去,怎麼解決?
email.py:
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(str(app.config["FLASKY_MAIL_SUBJECT_PREFIX"]) + str(" ") + str(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
config.py:
class Config:
SECRET_KEY = os.environ.get("SECRET_KEY") or "hard to guess string"
MAIL_SERVER = "smtp.163.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 = "Flasky &"
FLASKY_ADMIN = "xxx@163.com"
SSL_DISABLE = False
錯誤顯示為:
Traceback (most recent call last):File "c:python34Lib hreading.py", line 920, in bootstrapinner
self.run()File "c:python34Lib hreading.py", line 868, in runself.target(*self.args, **self._kwargs)File "G:myblogappemail.py", line 11, in send_async_emailmail.send(msg)File "G:myblogvenvlibsite-packagesflask_mail.py", line 491, in sendwith self.connect() as connection:File "G:myblogvenvlibsite-packagesflask_mail.py", line 144, in enter
self.host = self.configure_host()File "G:myblogvenvlibsite-packagesflask_mail.py", line 158, in configure_hosthost = smtplib.SMTP(self.mail.server, self.mail.port)File "c:python34Libsmtplib.py", line 242, in init(code, msg) = self.connect(host, port)File "c:python34Libsmtplib.py", line 321, in connectself.sock = self.getsocket(host, port, self.timeout)File "c:python34Libsmtplib.py", line 292, in getsocketself.source_address)File "c:python34Libsocket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):File "c:python34Libsocket.py", line 533, in getaddrinfofor res in _socket.getaddrinfo(host, port, family, type, proto, flags):socket.gaierror: [Errno 11004] getaddrinfo failed不知道問題出在哪裡,求大神指點一二,python3.4,win7
--------------------------------------------------------------------
埠的問題已經修改,也已開啟SMTP,使用的是授權密碼,依然出現同樣的錯誤,求指教
你的配置有錯誤:
MAIL_PORT = 25
MAIL_USE_TLS = True
1. 首先在163的網頁裡面,把stmp的功能打開,否則肯定不能發的
2. 用客戶端授權碼代替用戶密碼,這個也要在163設置的
3. 如果使用非SSL協議,則埠號是 25
MAIL_PORT = 25
MAIL_USE_TLS = False
MAIL_USE_SSL = False
如果使用SSL協議,則埠號是 465 或者 994
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL = True
你的其他配置沒有問題,多試驗吧,肯定能發的。首先試驗用同步方式發送,再試驗非同步方式。
目測這裡出錯:
FLASKY_MAIL_SENDER = "Flasky &
應該把等號右邊換成你自己具體的郵箱。
推薦閱讀:
※如何有效地使用 Gmail 的 Archive(歸檔)功能?
※號稱端到端加密的瑞士安全電子郵件服務 ProtonMail 會有什麼樣的前景?
※為什麼國外人顯得比中國人更重視 Email ?具體說下國外的習慣?
※Email 的發送時間可以偽造嗎?
※在國內用什麼郵件服務發送郵件最好?