跳转到帖子
  • 游客您好,欢迎来到黑客世界论坛!您可以在这里进行注册。

    赤队小组-代号1949(原CHT攻防小组)在这个瞬息万变的网络时代,我们保持初心,创造最好的社区来共同交流网络技术。您可以在论坛获取黑客攻防技巧与知识,您也可以加入我们的Telegram交流群 共同实时探讨交流。论坛禁止各种广告,请注册用户查看我们的使用与隐私策略,谢谢您的配合。小组成员可以获取论坛隐藏内容!

    TheHackerWorld官方

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


HACK1949

推荐的帖子

#!/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)

  

链接帖子
意见的链接
分享到其他网站

黑客攻防讨论组

黑客攻防讨论组

    You don't have permission to chat.
    • 最近浏览   0位会员

      • 没有会员查看此页面。
    ×
    ×
    • 创建新的...