ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python-发邮件框架【带图片和附件】

2022-09-14 19:03:39  阅读:250  来源: 互联网

标签:发邮件 images Python self list content msg 附件 image


#!/usr/bin/env python
# -*- coding:utf-8 –*-
import smtplib
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          # 邮箱地址 mail.163.com
        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, 587)
            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
    nickname = '发件人姓名'
    email = 'zhoujt'
    password = 'yourpasswd'
    m = Mail('mail.ke.com', nickname, email, password, 'ke.com')
    m.send_mail(to_list, title, content, cc_list, encode, is_html, images)


if __name__ == '__main__':
    images = [
        't_image/docker-k8s.png',
        't_image/kubernetes.png'
    ]
    import time

    title = '发送图片 %s' % time.strftime('%H:%M:%S')
    # content = 'this is attach images %s' % time.time()
    content = '新增图片'
    try:
        send_mail(['[email protected]'], title, content, [], 'utf-8', True, images)
        # 收件人zhoujt 标题title 内容content 抄送[] 字符 网页版=True 发送图片
        print('successful')
    except smtplib.SMTPException as e:
        print('error\n', e)

  

标签:发邮件,images,Python,self,list,content,msg,附件,image
来源: https://www.cnblogs.com/security-guard/p/send_email.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[[email protected]]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有