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

网站做的长图能导出吗软文发稿平台

网站做的长图能导出吗,软文发稿平台,新能源电动汽车哪个牌子的质量好,做直销会员网站在 Web 应用中,邮件通知是常见的功能,如: 用户注册验证邮件密码找回邮件订单确认邮件订阅通知邮件 Flask 可以通过 Flask-Mail、Flask-Mailman 或 第三方 API(如 SendGrid、Mailgun) 发送邮件。本章介绍:…

在 Web 应用中,邮件通知是常见的功能,如:

  • 用户注册验证邮件
  • 密码找回邮件
  • 订单确认邮件
  • 订阅通知邮件

Flask 可以通过 Flask-MailFlask-Mailman 或 第三方 API(如 SendGrid、Mailgun) 发送邮件。本章介绍:

  1. 使用 Flask-Mail 发送邮件
  2. 发送带附件的邮件
  3. 使用 Flask-Mailman 发送邮件
  4. 使用 Celery 异步发送邮件
  5. 使用 SendGrid 发送邮件

9.1 安装 Flask-Mail

首先,安装 Flask-Mail:

pip install Flask-Mail

然后,在 app.py 中进行配置:

from flask import Flask, render_template, request
from flask_mail import Mail, Messageapp = Flask(__name__)# 邮件服务器配置
app.config['MAIL_SERVER'] = 'smtp.gmail.com'  # 邮件服务器(Gmail、QQ 邮箱)app.config['MAIL_PORT'] = 587  # 端口号app.config['MAIL_USE_TLS'] = True  # 使用 TLS 传输层安全协议app.config['MAIL_USERNAME'] = 'your-email@gmail.com'  # 你的邮箱app.config['MAIL_PASSWORD'] = 'your-password'  # 邮箱密码或授权码app.config['MAIL_DEFAULT_SENDER'] = ('Your App', 'your-email@gmail.com')  # 发件人名称mail = Mail(app)

9.2 发送简单邮件

定义一个邮件发送函数:

@app.route('/send_email')
def send_email():msg = Message("Hello from Flask", recipients=["recipient@example.com"])msg.body = "This is a test email sent from Flask!"mail.send(msg)return "Email sent successfully!"

运行应用并测试

启动 Flask:

flask run

然后访问:

http://127.0.0.1:5000/send_email

9.3 发送带 HTML 格式的邮件

Flask-Mail 支持 HTML 邮件:

@app.route('/send_html_email')
def send_html_email():msg = Message("HTML Email from Flask", recipients=["recipient@example.com"])msg.html = "<h1>Welcome</h1><p>This is an <b>HTML email</b> sent from Flask.</p>"mail.send(msg)return "HTML Email sent!"

9.4 发送带附件的邮件

@app.route('/send_email_with_attachment')
def send_email_with_attachment():msg = Message("Email with Attachment", recipients=["recipient@example.com"])msg.body = "Please find the attachment."with app.open_resource("static/sample.pdf") as attachment:msg.attach("sample.pdf", "application/pdf", attachment.read())mail.send(msg)return "Email with attachment sent!"

9.5 使用 Flask-Mailman 发送邮件

Flask-Mail 维护较少,推荐使用 Flask-Mailman 作为替代:

pip install Flask-Mailman

修改 app.py:

from flask_mailman import Mail, EmailMessagemail = Mail(app)@app.route('/send_mailman_email')d
ef send_mailman_email():msg = EmailMessage(subject="Hello from Flask-Mailman",body="This is a test email from Flask-Mailman!",to=["recipient@example.com"])msg.send()return "Email sent using Flask-Mailman!"

9.6 使用 Celery 异步发送邮件

避免 Flask 请求阻塞,建议使用 Celery 进行异步邮件发送。

安装 Celery

pip install celery

配置 Celery

在 celery_config.py:

from celery import Celerydef make_celery(app):celery = Celery(app.import_name,backend=app.config['CELERY_RESULT_BACKEND'],broker=app.config['CELERY_BROKER_URL'])celery.conf.update(app.config)return celery

修改 app.py:

from celery_config import make_celeryapp.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'celery = make_celery(app)

定义异步任务

@celery.task
def send_async_email(recipient):msg = Message("Async Email", recipients=[recipient])msg.body = "This email was sent asynchronously using Celery!"mail.send(msg)

调用异步任务

@app.route('/send_async_email/<email>')
def send_async(email):task = send_async_email.apply_async(args=[email])return f"Email is being sent asynchronously. Task ID: {task.id}"

运行 Celery Worker

celery -A app.celery worker --loglevel=info

9.7 使用 SendGrid 发送邮件

9.7.1 注册 SendGrid 并获取 API Key

  • 访问 SendGrid 注册账号
  • 在 "API Keys" 处生成 API Key

9.7.2 安装 SendGrid 库

pip install sendgrid

9.7.3 配置 SendGrid

import sendgrid
from sendgrid.helpers.mail import MailSENDGRID_API_KEY = "your_sendgrid_api_key"def send_email_via_sendgrid(to_email):sg = sendgrid.SendGridAPIClient(api_key=SENDGRID_API_KEY)message = Mail(from_email="your-email@gmail.com",to_emails=to_email,subject="Hello from SendGrid",html_content="<strong>This is a test email sent via SendGrid.</strong>")response = sg.send(message)return response.status_code

调用:

@app.route('/send_sendgrid/<email>')
def send_via_sendgrid(email):status = send_email_via_sendgrid(email)return f"SendGrid Email Sent! Status: {status}"

9.8 发送批量邮件

@app.route('/send_bulk_email')
def send_bulk_email():recipients = ["user1@example.com", "user2@example.com"]msg = Message("Bulk Email", recipients=recipients)msg.body = "This is a bulk email sent to multiple users."mail.send(msg)return "Bulk Email Sent!"

9.9 结语

本章介绍了 Flask 发送邮件的多种方法:

  • Flask-Mail(传统方式)
  • Flask-Mailman(更好的维护支持)
  • Celery 异步邮件(解决阻塞问题)
  • SendGrid API(适用于企业邮件)

下一章,我们将探讨 Flask 的日志与错误处理

http://www.dtcms.com/wzjs/304813.html

相关文章:

  • 中建八局第一建设公司网站个人博客网站设计毕业论文
  • 两个网站做响应式网站seo实训报告
  • 做视频网站如何生成url网店推广实训报告
  • 网站建设的6个基本步骤成功的网络营销案例
  • 兰州市城市建设设计院官方网站seo工具查询
  • 网站开发支付功能怎么做可口可乐营销策划方案
  • 企业信息公示管理系统河南成都网站改版优化
  • 上海建网站价格友链申请
  • 乐清市建设路小学网站上海正规seo公司
  • 求助如何做网站推广百度大数据搜索引擎
  • 做女朋友的网站百度关键词推广怎么做
  • 同ip网站怎么做百度公司好进吗
  • 重庆奉节网站建设公司推荐肥城市区seo关键词排名
  • jsp实战网站开发视频搜索引擎大全网站
  • 公司设计网站需要多久做网页怎么做
  • 徐州网站快速优化排名他达拉非片的作用及功效副作用
  • 怎样用自己电脑做网站磐石网站seo
  • 网站建设之前必须 域名备案迅雷下载磁力天堂
  • 玉树电子商务网站建设公司万能软文模板
  • 广州seo优化公司排名保定百度seo公司
  • 网摘网站推广法人民日报最新新闻
  • 湖南做网站的公司站长之家域名
  • 网站建设佰金手指科杰十七在线网页制作工具
  • 网站建设类型域名注册商怎么查
  • b2b和b2c有什么区别北京建站优化
  • 温州网站建设钱宁波seo推广咨询
  • 长春网站优化免费发布信息网网站
  • 网站策划初级方案模板搜索引擎营销特点
  • 网站推广工作aso优化师工作很赚钱吗
  • 威海做网站的公司四川网站推广公司