广告位联系
返回顶部
分享到

python3实现带多张图片、附件的邮件发送

python 来源:互联网搜集 作者:秩名 发布时间:2019-08-10 20:38:11 人浏览
摘要

直接上代码: from email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.header import Header class Mail(object): def __init__(self, host, nickname, username, password,

直接上代码:

from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header
 
class Mail(object):
  def __init__(self, host, nickname, username, password, postfix):
    self.host = host
    self.nickname = nickname
    self.username = username
    self.password = password
    self.postfix = postfix
 
  def send_mail(self, to_list, subject, content, cc_list=[], encode='gbk', is_html=True, images=[]):
    me = str(Header(self.nickname, encode)) + "<" + self.username + "@" + self.postfix + ">"
    msg = MIMEMultipart()
    msg['Subject'] = Header(subject, encode)
    msg['From'] = me
    msg['To'] = ','.join(to_list)
    msg['Cc'] = ','.join(cc_list)
    if is_html:
      mail_msg = ''
      for i in range(len(images)):
        mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
      msg.attach(MIMEText(content + mail_msg, 'html', 'utf-8'))
 
      for i, img_name in enumerate(images):
        with open(img_name, 'rb') as fp:
          img_data = fp.read()
        msg_image = MIMEImage(img_data)
        msg_image.add_header('Content-ID', '<image%d>' % (i+1))
        msg.attach(msg_image)
        # 将图片作为附件
        # image = MIMEImage(img_data, _subtype='octet-stream')
        # image.add_header('Content-Disposition', 'attachment', filename=images[i])
        # msg.attach(image)
    else:
      msg_content = MIMEText(content, 'plain', encode)
      msg.attach(msg_content)
 
    try:
      s = smtplib.SMTP()
      # s.set_debuglevel(1)
      s.connect(self.host)
      s.login(self.username, self.password)
      s.sendmail(me, to_list + cc_list, msg.as_string())
      s.quit()
      s.close()
      return True
    except Exception as e:
      print(e)
      return False
 
def send_mail(to_list, title, content, cc_list=[], encode='utf-8', is_html=True, images=[]):
  content = '<pre>%s</pre>' % content
  m = Mail('smtp.163.com', 'TV-APP TEST', 'tvapp_qa', 'ujlnluutpfespgxz', '163.com')
  m.send_mail(to_list, title, content, cc_list, encode, is_html, images)
 
 
if __name__ == '__main__':
  images = [
    '1.png',
    '2.png',
    '3.png',
    '4.png',
  ]
  import time
  title = 'new images %s' % time.strftime('%H:%M:%S')
  content = 'this is attach images %s' % time.time()
  send_mail(['x@163.com'], title, content, ['xx@163.com', 'xxx@163.com'], 'utf-8', True, images)

后记

调试发送多张图片的时候遇到的问题:

用for循环生成的mail_msg,不能直接attach,需要和content一起attach

mail_msg = ''
for i in range(len(images)):
  mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
  msg.attach(MIMEText(**content** + mail_msg, 'html', 'utf-8'))


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/weixin_44152831/article/details/89214911
相关文章
  • Python Django教程之实现新闻应用程序

    Python Django教程之实现新闻应用程序
    Django是一个用Python编写的高级框架,它允许我们创建服务器端Web应用程序。在本文中,我们将了解如何使用Django创建新闻应用程序。 我们将
  • 书写Python代码的一种更优雅方式(推荐!)

    书写Python代码的一种更优雅方式(推荐!)
    一些比较熟悉pandas的读者朋友应该经常会使用query()、eval()、pipe()、assign()等pandas的常用方法,书写可读性很高的「链式」数据分析处理代码
  • Python灰度变换中伽马变换分析实现

    Python灰度变换中伽马变换分析实现
    1. 介绍 伽马变换主要目的是对比度拉伸,将图像灰度较低的部分进行修正 伽马变换针对的是对单个像素点的变换,也就是点对点的映射 形
  • 使用OpenCV实现迷宫解密的全过程

    使用OpenCV实现迷宫解密的全过程
    一、你能自己走出迷宫吗? 如下图所示,可以看到是一张较为复杂的迷宫图,相信也有人尝试过自己一点一点的找出口,但我们肉眼来解谜
  • Python中的数据精度问题的介绍

    Python中的数据精度问题的介绍
    一、python运算时精度问题 1.运行时精度问题 在Python中(其他语言中也存在这个问题,这是计算机采用二进制导致的),有时候由于二进制和
  • Python随机值生成的常用方法

    Python随机值生成的常用方法
    一、随机整数 1.包含上下限:[a, b] 1 2 3 4 import random #1、随机整数:包含上下限:[a, b] for i in range(10): print(random.randint(0,5),end= | ) 查看运行结
  • Python字典高级用法深入分析讲解
    一、 collections 中 defaultdict 的使用 1.字典的键映射多个值 将下面的列表转成字典 l = [(a,2),(b,3),(a,1),(b,4),(a,3),(a,1),(b,3)] 一个字典就是一个键对
  • Python浅析多态与鸭子类型使用实例
    什么多态:同一事物有多种形态 为何要有多态=》多态会带来什么样的特性,多态性 多态性指的是可以在不考虑对象具体类型的情况下而直
  • Python字典高级用法深入分析介绍
    一、 collections 中 defaultdict 的使用 1.字典的键映射多个值 将下面的列表转成字典 l = [(a,2),(b,3),(a,1),(b,4),(a,3),(a,1),(b,3)] 一个字典就是一个键对
  • Python淘宝或京东等秒杀抢购脚本实现(秒杀脚本

    Python淘宝或京东等秒杀抢购脚本实现(秒杀脚本
    我们的目标是秒杀淘宝或京东等的订单,这里面有几个关键点,首先需要登录淘宝或京东,其次你需要准备好订单,最后要在指定时间快速
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计