6.2 zmail yagmail 案例

def zmail_email():
    # !/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>       以下为测试邮件<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,   # 主题
        'from': '发送者<xxxx@qq.com>',  # 发送者
        'content_html': content_html,   # html 内容
        'attachments': attachment   # 附件
    }
    try:
        zm.send_mail(to_mailbox, mail, cc=cc_mailbox)
        print('邮件发送成功!')
    except Exception as e:
        print(traceback.print_exc())
        print('邮件发送失败!')

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

    import yagmail

    yag = yagmail.SMTP(user='xxxx@sina.com', password='xxxxxxxx', host='smtp.sina.com')

    to = ['xxxx@qq.com','xxxx@139.com']  # 收件人
    cc = ['xxxx@163.com','xxxx@qq.com']  # 抄送人
    subject = ["【温馨提醒】进度"]  # 主题
    picture = r'test.png'  # 输入附在正文之后的图片路径
    logo = r'logo.png'

    # 正文
    text = '''
    Dear all,
    为提高资金安全管理力度,基本每周邮件提醒催收应收款,本截图或附件是双子上截止到2022.6.19的数据。
    如果在实际工作中,任何收款方式的应收款项,与协议上的回款时间严重不符合的,请告知相关负责人,谢谢'
    '''

    # 落款(个性签名)
    inscribe = """------------------
    風£飛 运维开发部
    XXXXX科技有限公司
    北京市XXXX区XXXX路X号X层x-xxxx室
    电话:xxxxxxxxxx
    邮箱:xxxx@qq.com | 网址:https://www.cnblogs.com/xwupiaomiao
    """
    contents = [text, yagmail.inline(picture), inscribe, yagmail.inline(logo)]

    attachments = '测试附件.xlsx'  # 邮件附件

    yag.send(to=to, cc=cc, subject=subject, contents=contents, attachments=attachments)

    print("已发送")

if __name__ == '__main__':
    zmail_email()
    yagmail_email()