shellpipenv install flask-mail
和其他扩展类似,我们实例化 lask-Mail 提供的 Mai 类井传入程序实例 完成初始化,如 下所示:
pythonfrom flask mail import Mail
app = Flask( name )
mail = Mail (app)
Flask-Mail 通过连 SMTP ( Simple Mail Transfer Protocol 简单邮件传输协议)服务器来发送邮件
需要配置 SMTP 服务器,常见的配置量如下:
配置键 | 说明 | 默认值 |
---|---|---|
MAIL_SERVER | 用于发送邮件的 SMTP 服务器 | localhost |
MAIL_PORT | 发信端口 | 25 |
MAIL_USE_TLS | 是否使用STARTTLS | False |
MAIL_USE_SSL | 是否使用 SSL/TLS | False |
MAIL_USERNAME | 发信服务器的用户名 | None |
MAIL_PASSWORD | 发信服务务器的密码 | None |
MAIL_DEFAULT SENDER | 默认的发信人 | None |
对发送的邮件进行加密可以避免邮件在发送过程中被第 方截获和篡改 SSL ( Security Socket Layer ,安全套接字层)和 TLS ( Transport Layer Security ,传输层安全)是两种常用的 电子邮件安全协议
MAIL_USE_SSL = True MAIL_PORT = 465
MAIL_USE_TLS = True MAIL_PORT = 587
常用电子邮箱服务提供商的 SMT 配置信息如表所示
电子邮件服务提供商 | MAIL_SERVER(发信服务器) | MAIL_USERNAME | MAIL_PASSWORD | 额外步骤 |
---|---|---|---|---|
Gmail | smtp.gmail.com | 邮箱地址 | 邮箱密码 | 开启"Allow less secure apps",本地设置VPN代理 |
QQ邮箱 | smtp.qq.com | 邮箱地址 | 授权码 | |
新浪邮箱 | smtp.sina.com | 邮箱地址 | 邮箱密码 | |
163邮箱 | smtp.163.com | 邮箱地址 | 授权码 |
app.py邮件服务器配置
pythonimport os
from flask import Flask
from flask mail import Mail
app = Flask(__name__)
app.config.update(
MAIL_SERVER= os.getenv('MAIL_SERVER')
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = os.getenv('MAIL_USERNAME')
MAIL_PASSWORD= os.getenv('MAIL_PASSWORD')
MAIL_DEFAULT_SENDER = ('Grey Li ', os.getenv('MAIL_USERNAME'))
mail = Mail (app)
pythonfrom flask_mail import Message
from app import mail
message = Message (subject='Hello, World', recipients=['Zorn <zorn@example.com>'l , body='Across the Great Wal l we can reach every corner in the world ' )
pythonmail.send(message)
pythonfrom flask_mail import Mail,Message
mail = Mail(app)
def send_mail(subject,to,body):
message = Message(subject,recipients=[to],body=body)
mail.send(message)
在生产环境下,除了自己安装运行邮件服务器外,更方便的做法是使用事务邮件服务 ( Transactional Email Service ),比如 Mailgun (https://www.mailgun.com/)、 Sendgrid ( https://sendgrid.com/)等 这两个邮件服务对免费账户分别提供每月 万封和 3000 封的免费额度,完 全足够测试使用或在小型程序中使用 Mailgun 在注册免费账户时需要填写信用卡,而 Sendgrid 没有这一限制
对于 HTML 邮件正文的编写, 下面是一些常见的“最佳实践”:
html<span sty 1 e=” f ont- family:Arial , Helvetica , sans-serif; font - size :l2px ; color : #OOOOOO ; ” >Hello , Email !</span>
pythonmessage = Message ( . .. )
message.body ='纯文本正'
message.html= '<hl>HTML正文</hl>'
templates/email/subscribe.txt 纯文本邮件模板
Hello { { name } } , Thank you f or subscribing Flask Weekly ! Enjoy the r eading : ) Visit this link to unsubscribe : { { url for ( ' unsubscribe ’, external=True) ) )
为了同事支持纯文本格式和HTML格式的邮件正文,每一类邮件我们都需要分别创建HTML和纯文本格式的模板,对应上面的纯文本模板的HTML格式模板如下:
html<div style="width:580px;padding:20px;" >
<h3>Hello {{name}},</h3>
<p>Thank you for subscribing Flask Weekly !</p>
<p>Enjoy the reading : ) </p>
<small style="color:Jf868e96;”>
click here to <a href="{{url_for('unsubscribe', external=True )}}" >unsubscribe</a>.
</small >
</div>
代码:
pythonfrom flask import render_template
from flask_mail import Message
def send_subscribe_mail(subject,to,**kwargs):
message = Message(subject,recipients=[to],sender='Flask Weekly <%s>'&(os.getenv('MAIL_USERNAME')))
message.body = render_template('emails//subscribe.txt',**kwargs)
message.html = render_template('emails/subscribe.html',**kwargs)
mail.send(message)
pythonfrom threading import Thread
def _send_async_mail(app,message):
with app.app_context():
mail.send(message)
def send_mail(subject,to,body):
message = Message(subject,recipients=[to],body=body)
thr = Thread(target=_send_async_mail,args=[app,message])
thr.start()
return thr
本文作者:Eric
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!