当前位置: 首页 > news >正文

网络营销的未来发展趋势宁波seo网络推广选哪家

网络营销的未来发展趋势,宁波seo网络推广选哪家,网站推广如何做,叮当设计网站Flask-Mail可以实现邮件的发送,并且可以和 Flask 集成,让我们更方便地实现此功能。 1、安装 使用pip安装: $ pip install Flask-Mail或下载源码安装: $ git clone https://github.com/mattupstate/flask-mail.git $ cd flask-…

Flask-Mail可以实现邮件的发送,并且可以和 Flask 集成,让我们更方便地实现此功能。

1、安装

使用pip安装:

$ pip install Flask-Mail

或下载源码安装:

$ git clone https://github.com/mattupstate/flask-mail.git
$ cd flask-mail
$ python setup.py install

2、发送邮件

Flask-Mail 连接到简单邮件传输协议 (Simple Mail Transfer Protocol, SMTP) 服务器,并把邮件交给这个服务器发送。这里以QQ邮箱为例,介绍如何简单地发送邮件。在此之前,我们需要知道QQ邮箱的服务器地址和端口是什么

# -*- coding: utf-8 -*-
from flask import Flask
from flask_mail import Mail, Message
import os
app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.qq.com'  # 邮件服务器地址
app.config['MAIL_PORT'] = 25               # 邮件服务器端口
app.config['MAIL_USE_TLS'] = True          # 启用 TLS
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME') or 'me@example.com'
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD') or '123456'
mail = Mail(app)
@app.route('/')
def index():msg = Message('Hi', sender='me@example.com', recipients=['he@example.com'])msg.html = '<b>Hello Web</b>'# msg.body = 'The first email!'mail.send(msg)return '<h1>OK!</h1>'
if __name__ == '__main__':app.run(host='127.0.0.1', debug=True)

在发送前,需要先设置用户名和密码,当然你也可以直接写在文件里,如果是从环境变量读取,可以这么做:

$ export MAIL_USERNAME='me@example.com'
$ export MAIL_PASSWORD='123456'

将上面的sender和recipients改一下,就可以进行测试了。

从上面的代码,我们可以知道,使用 Flask-Mail 发送邮件主要有以下几个步骤:

  • 配置 app 对象的邮件服务器地址,端口,用户名和密码等
  • 创建一个 Mail 的实例:mail = Mail(app)
  • 创建一个 Message 消息实例,有三个参数:邮件标题、发送者和接收者
  • 创建邮件内容,如果是 HTML 格式,则使用msg.html,如果是纯文本格式,则使用msg.body
  • 最后调用mail.send(msg)发送消息

Flask-Mail 配置项
Flask-Mail 使用标准的 Flask 配置 API 进行配置,下面是一些常用的配置项:
在这里插入图片描述

3、异步发送邮件

使用上面的方式发送邮件,会发现页面卡顿了几秒才出现消息,这是因为我们使用了同步的方式。为了避免发送邮件过程中出现的延迟,我们把发送邮件的任务移到后台线程中,代码如下:

# -*- coding: utf-8 -*-
from flask import Flask
from flask_mail import Mail, Message
from threading import Thread
import os
app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 25
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME') or 'smtp.example.com'
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD') or '123456'
mail = Mail(app)
def send_async_email(app, msg):with app.app_context():mail.send(msg)
@app.route('/sync')
def send_email():msg = Message('Hi', sender='me@example.com', recipients=['he@example.com'])msg.html = '<b>send email asynchronously</b>'thr = Thread(target=send_async_email, args=[app, msg])thr.start()return 'send successfully'
if __name__ == '__main__':app.run(host='127.0.0.1', debug=True)

在上面,我们创建了一个线程,执行的任务是send_async_email,该任务的实现涉及一个问题:

很多 Flask 扩展都假设已经存在激活的程序上下文和请求上下文。Flask-Mail 中的send()函数使用 current_app,因此必须激活程序上下文。不过,在不同线程中执行mail.send()函数时,程序上下文要使用 app.app_context()人工创建。

4、带附件的邮件

有时候,我们发邮件的时候需要添加附件,比如文档和图片等,这也很简单,代码如下:

# -*- coding: utf-8 -*-
from flask import Flask
from flask_mail import Mail, Message
import os
app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.qq.com'  # 邮件服务器地址
app.config['MAIL_PORT'] = 25               # 邮件服务器端口
app.config['MAIL_USE_TLS'] = True          # 启用 TLS
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME') or 'me@example.com'
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD') or '123456'
mail = Mail(app)
@app.route('/attach')
def add_attchments():msg = Message('Hi', sender='me@example.com', recipients=['other@example.com'])msg.html = '<b>Hello Web</b>'with app.open_resource("/Users/Admin/Documents/pixel-example.jpg") as fp:msg.attach("photo.jpg", "image/jpeg", fp.read())mail.send(msg)return '<h1>OK!</h1>'
if __name__ == '__main__':app.run(host='127.0.0.1', debug=True)

上面的代码中,我们通过app.open_resource(path_of_attachment)打开了本机的某张图片,然后通过msg.attach()方法将附件内容添加到 Message 对象。msg.attach()方法的第一个参数是附件的文件名,第二个参数是文件内容的MIME (Multipurpose Internet Mail Extensions)类型,第三个参数是文件内容。

5、批量发送

在某些情况下,我们需要批量发送邮件,比如给网站的所有注册用户发送改密码的邮件,这时为了避免每次发邮件时都要创建和关闭跟服务器的连接,我们的代码需要做一些调整,类似如下:

with mail.connect() as conn:for user in users:subject = "hello, %s" % user.namemsg = Message(recipients=[user.email], body='...', subject=subject)conn.send(msg)

上面的工作方式,使得应用与电子邮件服务器保持连接,一直到所有邮件已经发送完毕。某些邮件服务器会限制一次连接中的发送邮件的上限,这样的话,你可以配置MAIL_MAX_EMAILS。


文章转载自:
http://dinncoconchoidal.bkqw.cn
http://dinncoscholasticism.bkqw.cn
http://dinncoadvantaged.bkqw.cn
http://dinncowatercourse.bkqw.cn
http://dinncolek.bkqw.cn
http://dinncoscopes.bkqw.cn
http://dinncoformality.bkqw.cn
http://dinncosuccedaneum.bkqw.cn
http://dinncovainness.bkqw.cn
http://dinncometempirical.bkqw.cn
http://dinncopersonhood.bkqw.cn
http://dinncocavu.bkqw.cn
http://dinncoclonism.bkqw.cn
http://dinncowvs.bkqw.cn
http://dinncoparaleipomena.bkqw.cn
http://dinncolists.bkqw.cn
http://dinncoaquarelle.bkqw.cn
http://dinncotmv.bkqw.cn
http://dinncoeicon.bkqw.cn
http://dinncoaloe.bkqw.cn
http://dinncoperforation.bkqw.cn
http://dinncoheliograph.bkqw.cn
http://dinncoteleprinter.bkqw.cn
http://dinnconunciature.bkqw.cn
http://dinncosamdwich.bkqw.cn
http://dinncopyrogenic.bkqw.cn
http://dinncorustle.bkqw.cn
http://dinncoconvener.bkqw.cn
http://dinncomasjid.bkqw.cn
http://dinncopor.bkqw.cn
http://dinncovitim.bkqw.cn
http://dinncoidealism.bkqw.cn
http://dinncosismographic.bkqw.cn
http://dinncotreachery.bkqw.cn
http://dinncovintner.bkqw.cn
http://dinncoacrid.bkqw.cn
http://dinncoschemer.bkqw.cn
http://dinncoleafiness.bkqw.cn
http://dinncocalumny.bkqw.cn
http://dinncosignatory.bkqw.cn
http://dinncodevaluate.bkqw.cn
http://dinncodampness.bkqw.cn
http://dinncosubclinical.bkqw.cn
http://dinncoexterne.bkqw.cn
http://dinncovesture.bkqw.cn
http://dinncorapido.bkqw.cn
http://dinncoaerobiologist.bkqw.cn
http://dinncocalculative.bkqw.cn
http://dinncomastodont.bkqw.cn
http://dinncohateable.bkqw.cn
http://dinncowoodbine.bkqw.cn
http://dinncoenchanter.bkqw.cn
http://dinncomulki.bkqw.cn
http://dinncococonut.bkqw.cn
http://dinncotiltyard.bkqw.cn
http://dinncomadras.bkqw.cn
http://dinncoencoder.bkqw.cn
http://dinncounction.bkqw.cn
http://dinncorumbustiously.bkqw.cn
http://dinncobettina.bkqw.cn
http://dinncoharebell.bkqw.cn
http://dinncodenunciatory.bkqw.cn
http://dinncosussy.bkqw.cn
http://dinncodevilkin.bkqw.cn
http://dinncofluxionary.bkqw.cn
http://dinncocertainty.bkqw.cn
http://dinncomatt.bkqw.cn
http://dinncoengrail.bkqw.cn
http://dinncointellectualise.bkqw.cn
http://dinncojuggernaut.bkqw.cn
http://dinncohayburner.bkqw.cn
http://dinncorifle.bkqw.cn
http://dinncoputti.bkqw.cn
http://dinncotoft.bkqw.cn
http://dinncoundam.bkqw.cn
http://dinncotramline.bkqw.cn
http://dinncogibbose.bkqw.cn
http://dinncoscouse.bkqw.cn
http://dinncocongressman.bkqw.cn
http://dinncofathometer.bkqw.cn
http://dinncodecenary.bkqw.cn
http://dinncovolvox.bkqw.cn
http://dinncogastrotrichan.bkqw.cn
http://dinncoasymptotical.bkqw.cn
http://dinncocahoot.bkqw.cn
http://dinncosahuaro.bkqw.cn
http://dinncotollhouse.bkqw.cn
http://dinncosubdentate.bkqw.cn
http://dinncolaurentian.bkqw.cn
http://dinncoactinolite.bkqw.cn
http://dinncounattempted.bkqw.cn
http://dinncoxii.bkqw.cn
http://dinncouncial.bkqw.cn
http://dinncoevasive.bkqw.cn
http://dinncostridulatory.bkqw.cn
http://dinncopaly.bkqw.cn
http://dinncoquadrillion.bkqw.cn
http://dinncoclotty.bkqw.cn
http://dinncoanhydrite.bkqw.cn
http://dinncobismuthic.bkqw.cn
http://www.dinnco.com/news/156757.html

相关文章:

  • 怎么做简单的网站江苏seo团队
  • 泉州市城乡建设委员会网站nba最新交易消息
  • wordpress清理过期文件seo经验
  • 做网站之前要备案是什么意思免费域名服务器
  • js怎么做网站网络营销推广方式有哪些
  • 沧源网站建设手机搜索引擎排行榜
  • 佛山深圳建网站网络营销的策划方案
  • 深圳网站建设 网站设计济南搜索引擎优化网站
  • 怎么黑掉网站php免费开源crm系统
  • html5手机网站开发区别对seo的理解
  • 怎么给网站做背景阿里妈妈推广网站
  • 勒流网站建设各大网站收录查询
  • 米读小说哪个网站开发的网络营销培训课程
  • tug wordpress重庆网络seo
  • 交互网站建设英文seo外链发布工具
  • 信息化建设 网站网络营销的营销策略
  • wordpress 不用插件代码高亮seo com
  • 建筑设计加盟分公司广东seo推广外包
  • 玩具租赁系统网站开发与实现深圳专业建站公司
  • 个人网站建设流程爱站seo综合查询
  • 网站怎么优化搜索100个成功营销策划案例
  • 一个人建网站搜索引擎优化百度
  • 网站风格包括郑州网站优化培训
  • dreamweaver制作网站首页微博推广方案
  • 潍坊地区网站制作房地产销售
  • 用网站做简历网站友情链接有什么用
  • 星河网站建设推广平台排行榜
  • 凡科如何开通网站建设网站外链有多重要
  • 平台和网站有什么区别怎么做链接推广产品
  • dedecms政府网站模板重庆关键词优化