脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/15 浏览:3 次 )

本文实例为大家分享了python3实现多张图片附件邮件发送的具体代码,供大家参考,具体内容如下

直接上代码,没有注释!

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="/UploadFiles/2021-04-08/cid:image%d">

后记

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

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

mail_msg = ''
for i in range(len(images)):
  mail_msg += '<p><img src="/UploadFiles/2021-04-08/cid:image%d">

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:python实现邮件发送功能
下一篇:Python字典推导式将cookie字符串转化为字典解析