python 发送邮件的四种方法汇总


Posted in Python onDecember 02, 2020

这里针对smtplib做了一系列封装,可以完成以下四种场景:

  • 发送纯文本的邮件
  • 发送html页面的邮件
  • 发送带附件文件的邮件
  • 发送能展示图片的邮件

以上四种场景,已经做好了二次封装,经测试OK,使用时直接传入对应参数即可,直接上代码

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart


class SendEMail(object):
  """封装发送邮件类"""

  def __init__(self, host, port, msg_from, pwd):

    self.msg_from = msg_from
    self.password = pwd

    # 邮箱服务器地址和端口
    self.smtp_s = smtplib.SMTP_SSL(host=host, port=port)

    # 发送方邮箱账号和授权码
    self.smtp_s.login(user=msg_from, password=pwd)

  def send_text(self, to_user, content, subject, content_type='plain'):
    """
    发送文本邮件
    :param to_user: 对方邮箱
    :param content: 邮件正文
    :param subject: 邮件主题
    :param content_type: 内容格式:'plain' or 'html'
    :return:
    """
    msg = MIMEText(content, _subtype=content_type, _charset="utf8")

    msg["From"] = self.msg_from
    msg["To"] = to_user
    msg["subject"] = subject

    self.smtp_s.send_message(msg, from_addr=self.msg_from, to_addrs=to_user)

  def send_file(self, to_user, content, subject, reports_path, filename, content_type='plain'):
    """
    发送带文件的邮件
    :param to_user: 对方邮箱
    :param content: 邮件正文
    :param subject: 邮件主题
    :param reports_path: 文件路径
    :param filename: 邮件中显示的文件名称
    :param content_type: 内容格式
    """

    file_content = open(reports_path, "rb").read()

    msg = MIMEMultipart()

    text_msg = MIMEText(content, _subtype=content_type, _charset="utf8")
    msg.attach(text_msg)

    file_msg = MIMEApplication(file_content)
    file_msg.add_header('content-disposition', 'attachment', filename=filename)
    msg.attach(file_msg)

    msg["From"] = self.msg_from
    msg["To"] = to_user
    msg["subject"] = subject

    self.smtp_s.send_message(msg, from_addr=self.msg_from, to_addrs=to_user)

  def send_img(self, to_user, subject, content, filename, content_type='html'):
    '''
    发送带图片的邮件
    :param to_user: 对方邮箱
    :param subject: 邮件主题
    :param content: 邮件正文
    :param filename: 图片路径
    :param content_type: 内容格式
    '''
    subject = subject
    msg = MIMEMultipart('related')
    # Html正文必须包含<img src="cid:imageid" alt="imageid" width="100%" height="100%>
    content = MIMEText(content, _subtype=content_type, _charset="utf8")
    msg.attach(content)
    msg['Subject'] = subject
    msg['From'] = self.msg_from
    msg['To'] = to_user

    with open(filename, "rb") as file:
      img_data = file.read()

    img = MIMEImage(img_data)
    img.add_header('Content-ID', 'imageid')
    msg.attach(img)

    self.smtp_s.sendmail(self.msg_from, to_user, msg.as_string())

以上就是python 发送邮件的四种方法汇总的详细内容,更多关于python 发送邮件的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
一个检测OpenSSL心脏出血漏洞的Python脚本分享
Apr 10 Python
python基础教程之面向对象的一些概念
Aug 29 Python
Python标准库之collections包的使用教程
Apr 27 Python
Python内置函数—vars的具体使用方法
Dec 04 Python
浅谈Scrapy框架普通反爬虫机制的应对策略
Dec 28 Python
Python3 SSH远程连接服务器的方法示例
Dec 29 Python
Python3多目标赋值及共享引用注意事项
May 27 Python
PyQt5实现从主窗口打开子窗口的方法
Jun 19 Python
使用Python opencv实现视频与图片的相互转换
Jul 08 Python
python导包的几种方法(自定义包的生成以及导入详解)
Jul 15 Python
关于pandas的离散化,面元划分详解
Nov 22 Python
详解Python 中的 defaultdict 数据类型
Feb 22 Python
如何用PyPy让你的Python代码运行得更快
Dec 02 #Python
python 实现波浪滤镜特效
Dec 02 #Python
python 如何对logging日志封装
Dec 02 #Python
python3中确保枚举值代码分析
Dec 02 #Python
python使用yaml 管理selenium元素的示例
Dec 01 #Python
python3处理word文档实例分析
Dec 01 #Python
python3中布局背景颜色代码分析
Dec 01 #Python
You might like
php radio 单选框获取与保持值的实现代码
2010/05/15 PHP
三个类概括PHP的五种设计模式
2012/09/05 PHP
优化PHP代码技巧的小结
2013/06/02 PHP
Android ProgressBar进度条和ProgressDialog进度框的展示DEMO
2013/06/19 PHP
php实现MD5加密16位(不要默认的32位)
2013/08/12 PHP
PHP英文字母大小写转换函数小结
2014/05/03 PHP
基于swoole实现多人聊天室
2018/06/14 PHP
深入研究PHP中的preg_replace和代码执行
2018/08/15 PHP
PHP iconv()函数字符编码转换的问题讲解
2019/03/22 PHP
JavaScript Event学习第四章 传统的事件注册模型
2010/02/07 Javascript
JavaScript下利用fso判断文件是否存在的代码
2010/12/11 Javascript
判定是否原生方法的JS代码
2013/11/12 Javascript
js监听鼠标点击和键盘点击事件并自动跳转页面
2014/09/24 Javascript
JavaScript轮播图简单制作方法
2017/02/20 Javascript
angular-cli修改端口号【angular2】
2017/04/19 Javascript
jquery在vue脚手架中的使用方式示例
2017/08/29 jQuery
Angular4.0动画操作实例详解
2019/05/10 Javascript
vue读取本地的excel文件并显示在网页上方法示例
2019/05/29 Javascript
js判断复选框是否选中的方法示例【基于jQuery】
2019/10/10 jQuery
python用列表生成式写嵌套循环的方法
2018/11/08 Python
python的launcher用法知识点总结
2020/08/07 Python
windows下python 3.9 Numpy scipy和matlabplot的安装教程详解
2020/11/28 Python
巴西电子产品购物网站:Saldão da Informática
2018/01/09 全球购物
秋季校运动会广播稿
2014/02/23 职场文书
《回乡偶书》教学反思
2014/04/12 职场文书
安全月活动总结
2014/05/05 职场文书
公司授权委托书格式样本
2014/10/01 职场文书
会计出纳岗位职责
2015/03/31 职场文书
食品仓管员岗位职责
2015/04/01 职场文书
行政二审代理词
2015/05/25 职场文书
婚礼领导致辞大全
2015/07/28 职场文书
寒假生活随笔
2015/08/15 职场文书
涨工资申请书应该怎么写?
2019/07/08 职场文书
据Python爬虫不靠谱预测可知今年双十一销售额将超过6000亿元
2021/11/11 Python
vue cli4中mockjs在dev环境和build环境的配置详情
2022/04/06 Vue.js
vue实现拖拽交换位置
2022/04/07 Vue.js