Advertisement

Python 3.9版本 - 发送邮件并包含图片

阅读量:

通过Python编程实现包含图片附件的电子邮件发送功能

1.设置邮件为多文本格式

复制代码
    MIMEMultipart('related')
    
      
    

2.编写html格式内容包含图片信息

复制代码
    msgHtmlImg = '<img src="cid:image{count}"><br>'
    MIMEText(msgHtmlImg, 'html')
    
      
      
    

3.读取图片并添加邮件头

复制代码
    fp = open(imgpath, 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    
    # Define the image's ID as referenced above
    msgImage.add_header('Content-ID', '<image{count}>'.format(count=i))
    msgRoot.attach(msgImage)
    
      
      
      
      
      
      
      
    

4.发送邮件lib代码

复制代码
    #!/usr/bin/

全部评论 (0)

还没有任何评论哟~