smtplib、zmail模块发邮件

发布时间 2023-10-08 14:05:56作者: 風£飛

1、zmail发送邮件脚本

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ =
# https://www.jianshu.com/p/b9e11dbbc9cf
# https://github.com/zhangyunhao116/zmail/blob/master/README-cn.md
# pip install docx-mailmerge 将相同格式的模板与一组数据合并
# pip install python-docx 该模块可以用来创建、修改和读取 Microsoft Word 文档(.docx 文件)
# https://blog.51cto.com/u_16099200/6873568 python-docx模块操作word模板
# https://github.com/Bouke/docx-mailmerge
# https://blog.51cto.com/u_16099267/6372841  docx-mailmerge模块操作word模板


import zmail, traceback

# 邮箱信息
# 发件人
from_mailbox = 'xxxx@qq.com'
# 邮箱密码
password = 'xxxxxxxx'
smtp_server = 'smtp.exmail.qq.com'
# 收件人
to_mailbox = ['xxxx@qq.com',
              'xxxx@163.com']
# 抄送人
cc_mailbox = ['xxxx@qq.com',
              'xxxx@qq.com',
              'xxxx@139.com']

# 邮件主题
subject = '发送邮件测试'

# 构建邮件html内容
content_html = """
<html>  
  <head></head>  
  <body>  
    <p>您好:<br>&nbsp; &nbsp; &nbsp; &nbsp;以下为测试邮件<br>
       How are you?<br>  
       Here is the <a href="http://www.baidu.com">link</a> you wanted.<br> 
    </p>
    <img src="cid:image1">
  </body>  
</html>  
"""

# 添加附件
attachment = ['测试附件.xlsx', 'test.png']

# 发送邮件
zm = zmail.server(from_mailbox, password, smtp_host='smtp.exmail.qq.com', smtp_port=465, smtp_ssl=True)
mail = {
    'subject': subject,
    'content_html': content_html,
    'attachments': attachment
}
try:
    zm.send_mail(to_mailbox, mail, cc=cc_mailbox)
    print('邮件发送成功!')
except Exception as e:
    print(traceback.print_exc())
    print('邮件发送失败!')

2、smtplib发送邮件脚本

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ =
# pip install yagmail
# https://github.com/kootenpv/yagmail


import smtplib, os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header
import base64


class SendMail(object):
    def __init__(self, username, passwd, email_host, recv, title, content, cc_list=None, file=None, imagefile=None,
                 ssl=False,
                 port=25, ssl_port=465):
        '''
        :param username: 用户名
        :param passwd: 密码
        :param email_host: smtp服务器地址
        :param recv: 收件人,多个要传list ['a@qq.com','b@qq.com']
        :param cc_list: 抄送人,多个要传list ['c@qq.com','d@qq.com']
        :param title: 邮件标题
        :param content: 邮件正文
        :param file: 附件路径,如果不在当前目录下,要写绝对路径,默认没有附件
        :param imagefile:  图片路径,如果不在当前目录下,要写绝对路径,默认没有图片
        :param ssl: 是否安全链接,默认为普通
        :param port: 非安全链接端口,默认为25
        :param ssl_port: 安全链接端口,默认为465
        '''
        self.username = username  # 用户名
        self.passwd = passwd  # 密码
        self.recv = recv  # 收件人,多个要传list ['a@qq.com','b@qq.com]
        self.cc_list = cc_list  # 抄送人,多个要传list ['a@qq.com','b@qq.com]
        self.title = title  # 邮件标题
        self.content = content  # 邮件正文
        self.file = file  # 附件路径,如果不在当前目录下,要写绝对路径
        self.imagefile = imagefile  # 图片路径,如果不在当前目录下,要写绝对路径
        self.email_host = email_host  # smtp服务器地址
        self.port = port  # 普通端口
        self.ssl = ssl  # 是否安全链接
        self.ssl_port = ssl_port  # 安全链接端口

    def send_mail(self):
        # msg = MIMEMultipart()
        msg = MIMEMultipart('mixed')
        # 发送内容的对象
        if self.file:  # 处理附件的
            file_name = os.path.split(self.file)[-1]  # 只取文件名,不取路径
            try:
                f = open(self.file, 'rb').read()
            except Exception as e:
                raise Exception('附件打不开!!!!')
            else:
                att = MIMEText(f, "base64", "utf-8")
                att["Content-Type"] = 'application/octet-stream'
                # base64.b64encode(file_name.encode()).decode()
                new_file_name = '=?utf-8?b?' + base64.b64encode(file_name.encode()).decode() + '?='
                # 这里是处理文件名为中文名的,必须这么写
                att["Content-Disposition"] = 'attachment; filename="%s"' % (new_file_name)
                msg.attach(att)
        if self.imagefile:
            try:
                sendimagefile = open(self.imagefile, 'rb').read()
            except Exception as e:
                raise Exception('图片无法打开!!!!')
            else:
                image = MIMEImage(sendimagefile)
                image.add_header('Content-ID', '<image1>')  # 设置图片id为image1必需成html中的cid对应
                msg.attach(image)
        text_html = MIMEText(self.content, 'html', 'utf-8')
        msg.attach(text_html)
        # msg.attach(MIMEText(self.content))  # 邮件正文的内容
        msg['Subject'] = self.title  # 邮件主题
        msg['From'] = self.username  # 发送者账号
        # msg['To'] = ','.join(self.recv)  # 接收者账号列表
        msg['To'] = Header(','.join(self.recv))  # 接收者账号列表
        msg['Cc'] = Header(','.join(self.cc_list))  # 抄送人,将列表转换为字符串
        if self.ssl:
            self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.ssl_port)
        else:
            self.smtp = smtplib.SMTP(self.email_host, port=self.port)
        # 发送邮件服务器的对象
        self.smtp.login(self.username, self.passwd)
        try:
            self.smtp.sendmail(self.username, self.recv + self.cc_list, msg.as_string())
            pass
        except Exception as e:
            print('出错了。。', e)
        else:
            print('发送成功!')
        self.smtp.quit()


if __name__ == '__main__':
    """
    发送正文中的图片:由于包含未被许可的信息,网易邮箱定义为垃圾邮件,报554 DT:SPM :<p><img src="cid:image1"></p>
    """
    m = SendMail(
        username='xxxx@sina.com',
        passwd='xxxx',
        email_host='smtp.sina.com',
        recv=['xxxx@qq.com', 'xxxx@qq.com'],
        cc_list=['xxxx@qq.com', 'xxxx@163.com', 'xxxx@139.com'],
        title='发送测试邮件',
        content="""
        <html>  
          <head></head>  
          <body>  
            <p>Hi!<br>  
               How are you?<br>  
               Here is the <a href="http://www.baidu.com">link</a> you wanted.<br> 
            </p>
            <img src="cid:image1">
          </body>  
        </html>  
        """,
        file=r'测试附件.xlsx',
        imagefile=r'test.png',
        ssl=False,
    )
    m.send_mail()