Python发送邮件的实例代码讲解
作者:初遇ぃ 时间:2021-06-05 02:35:10
一、邮件发送示例
邮件发送示例
flask_email及smtplib原生邮件发送示例,适用于基于Flask框架开发,但是内部设置的定时任务发送邮件/或提供离线接口发送邮件操作
1.flask config配置
# QQ邮箱配置
MAIL_DEBUG = True # 开启debug,便于调试看信息
MAIL_SUPPRESS_SEND = False # 发送邮件,为True则不发送
MAIL_SERVER = 'smtp.qq.com' # 邮箱服务器
MAIL_PORT = 465 # 端口
MAIL_USE_SSL = True # 重要,qq邮箱需要使用SSL
MAIL_USE_TLS = False # 不需要使用TLS
MAIL_USERNAME = '@qq.com' # 填邮箱
MAIL_PASSWORD = '' # 填授权码
FLASK_MAIL_SENDER = '@qq.com' # 邮件发送方
FLASK_MAIL_SUBJECT_PREFIX = '' # 邮件标题
MAIL_DEFAULT_SENDER = '@qq.com' # 填邮箱,默认发送者
2.示例代码
import smtplib
import constant # 定义常量文件
from email.header import Header
from email.mime.text import MIMEText
constant.SMTP_SERVER = 'smtp.qq.com'
constant.PORT = 465
class EmailSender(object):
def __init__(self, subject, receivers, sender='ss@qq.com', password='123456', offline=False, html_body=None,
text_body=None, **kwargs):
self.subject = subject
self.receivers = receivers
self.sender = sender
self.password = password
if offline:
if html_body:
self.send_body = html_body
self._subtype = 'html'
elif text_body:
self.send_body = text_body
self._subtype = 'plain'
self.send_email_offline()
else:
from flask_mail import Mail
self.mail = Mail()
dic = dict(kwargs)
self.send_email(html_body, text_body, attachments=dic.get("attachments"), sync=dic.get("sync"))
def send_email_offline(self):
try:
message = MIMEText(self.send_body, self._subtype, 'utf-8')
message['From'] = self.sender
message['To'] = ','.join(self.receivers)
message['Subject'] = Header(self.subject, 'utf-8')
smtpObj = smtplib.SMTP_SSL(constant.SMTP_SERVER, constant.PORT)
smtpObj.login(self.sender, self.password)
smtpObj.sendmail(
self.sender, self.receivers, message.as_string())
smtpObj.quit()
except smtplib.SMTPException:
return "smtp服务器发送异常 >> 无法发送邮件"
except Exception as e:
return f"邮件发送失败 >> {e}"
def send_email(self, text_body, html_body, attachments=None, sync=False):
from threading import Thread
from flask import current_app
from flask_mail import Message
try:
msg = Message(self.subject, recipients=self.receivers)
msg.body = text_body
msg.html = html_body
if attachments:
for attachment in attachments:
msg.attach(*attachment)
if not sync:
self.mail.send(msg)
else:
Thread(target=self.send_async_email, args=(current_app._get_current_object(), msg)).start()
except Exception as e:
return f"邮件发送失败 >> {e}"
def send_async_email(self, app, msg):
with app.app_context():
try:
self.mail.send(msg)
except Exception as e:
print(f"邮件发送错误信息:{e}")
3.使用
err = EmailSender(subject='吃货询问', receivers=["123@qq.com", "1234@qq.cn"], text_body='吃了没呀?', offline=True)
if err:
print(err)
以上3点就是关于Python发送邮件的全部知识点,感谢大家的学习和对脚本之家的支持。
来源:https://www.cnblogs.com/zhangliang91/p/11684551.html
标签:Python,发送邮件
0
投稿
猜你喜欢
Anaconda的安装与虚拟环境建立
2022-05-26 01:06:22
npm qs模块使用详解
2024-04-25 13:11:14
golang db事务的统一封装的实现
2023-07-02 21:01:51
SQL Server数据库查询优化的常用方法总结
2008-12-10 14:43:00
Python实现暴力破解有密码的zip文件的方法
2023-11-22 22:48:40
mysql myisam 优化设置
2010-03-25 10:18:00
javascript给span标签赋值的方法
2023-09-05 21:28:53
VSCode的使用配置以及VSCode插件的安装教程详解
2023-05-31 14:48:46
python3操作mysql数据库的方法
2023-07-28 15:48:27
Python获取时间范围内日期列表和周列表的函数
2023-03-25 15:16:39
使用Python中的reduce()函数求积的实例
2021-08-14 04:35:47
Apache部署Django项目图文详解
2023-12-17 06:51:05
Python绘图Turtle库的安装问题解决
2022-06-08 03:44:22
MySQL 备份还原数据库批处理
2024-01-16 21:03:52
MySQL一些常用高级SQL语句详解
2024-01-29 02:45:22
opencv-python 开发环境的安装、配置教程详解
2022-04-25 22:14:58
Python数据结构之顺序表的实现代码示例
2021-02-22 07:13:08
Python 虚拟环境工作原理解析
2023-02-21 02:18:50
Python批量解压&压缩文件夹的示例代码
2023-02-17 17:43:29
升级keras解决load_weights()中的未定义skip_mismatch关键字问题
2022-12-27 03:58:07