歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Flask-mail擴展的基本使用

Flask-mail擴展的基本使用

日期:2017/3/1 9:41:42   编辑:Linux編程

1.安裝

pip install flask-mail

2.使用

1.配置一些發送郵件的參數例如郵件發送服務器的地址,端口,是否加密等。

2.初始化flask-mail插件。

3.創建Message實例,設置發送的內容,地址和主題等信息

4.使用Mail實例針對Message的實例來發送

3.配置參數詳解

下面這是在使用mail的時候需要指定的一些配置參數,需要在使用mail之前來設置相關的參數。

MAIL_HOSTNAME localhost Hostname or IP address of the email server

MAIL_PORT 25 Port of the email server

MAIL_USE_TLS False Enable Transport Layer Security (TLS) security

MAIL_USE_SSL False Enable Secure Sockets Layer (SSL) security

MAIL_USERNAME None Mail account username

MAIL_PASSWORD None Mail account passwor


4.初始化

和絕大多數的Flask插件一樣,要使用Flask插件的時候需要對插件進行初始化,大都數插件的初始化方式經過Flask封裝後變的統一了,大部分情況下都是想如下方式來進行初始化。其中app是Flask應用的實例。

from flask.ext.mail import Mail

mail = Mail(app)

5.發送郵件

初始化好Mail插件後就生成了一個mail的實例,接下來就需要創建一個Message的實例這裡面包含了要發送的郵件的所有信息,例如郵件發送的地址,郵件的主題,郵件的內容,郵件的html模板等。

from flask.ext.mail import Message

def send_email():

msg = Message("郵件的subject",sender="localhost", recipients=["[email protected]"])

msg.body = "郵件的主題內容"

msg.html = "<h1>郵件的html模板<h1> body"

#發送郵件

mail.send(msg)


msg.html = "<h1>郵件的html模板<h1> body" 這裡的body是一個占位符將會替換msg.body裡面的內容。

發面的郵件發送過程是同步的,當點擊發送郵件的時候頁面會卡住好幾秒直到郵件發送完畢。

6.異步發送郵件

這裡通過線程的方式來實現郵件發送,主進程繼續完成頁面的輸出的。從而達到異步的效果。

from threading import Thread

def send_async_email(app, msg):

with app.app_context():

mail.send(msg)

def send_email():

msg = Message("郵件的subject",sender="localhost", recipients=["[email protected]"])

msg.body = "郵件的主題內容"

msg.html = "<h1>郵件的html模板<h1> body"

#發送郵件

thr = Thread(target=send_async_email, args=[app,msg])

thr.start()

return "OK"

許多Flask的擴展都是假定自己運行在一個活動的應用和請求上下文中,Flask-Mail的send函數使用到current_app這個上下文了,所以當mail.send()函數在一個線程中執行的時候需要人為的創建一個上下文,所有在send_async_email中使用了app.app_context()來創建一個上下文。

原文如下:

Many Flask extensions operate
under the assumption that there are active application and request contexts. Flask-Mail’s
send() function uses current_app, so it requires the application context to be active.
But when the mail.send() function executes in a different thread, the application context
needs to be created artificially using app.app_context().

7.完整實例

下面是一個完整的簡單異步發送郵件的實例:

from flask import Flask

from flask import current_app

from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config.from_object(__name__)

db = SQLAlchemy(app)

from flask.ext.mail import Mail

from flask.ext.mail import Message

from threading import Thread

mail = Mail(app)

app.config['MAIL_SERVER']='localhost'

app.config['MAIL_PORT']=25

def send_async_email(app,msg):

with app.app_context():

mail.send(msg)

@app.route("/mail")

def SendMail():

msg = Message('test',sender='[email protected]',recipients=['[email protected]'])

msg.body = "text body"

msg.html = "<b>HTML</b>body"

thr = Thread(target=send_async_email,args=[app,msg])

thr.start()

return "OK"

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

Python下使用MySQLdb模塊 http://www.linuxidc.com/Linux/2012-06/63620.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved