python发送邮件yagmail库

yagmail库发送邮件简洁,代码量少

import yagmail


def send_yagmail(sender, send_password, addressee, host='smtp.qq.com', port=465):
    yag = yagmail.SMTP(sender, send_password, host, port)
    img_url = 'https://img2.baidu.com/it/u=483398814,2966849709&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1689872400&t=93a58d55db740132d64d48dcd60c8c1c'
    yag.send(
        to=addressee,  # 收件人,如果一个收件人用字符串,多个收件人用列表或元组
        subject='这是测试邮件的主题:测试一下',  # 邮件主题(标题)
        contents=['邮件第一段', '邮件正文第二段', '邮件第三段<a href="https://www.baidu.com">点击跳转</a>',
                  f'<h1 style="color:red">呵呵呵</h1><img src={img_url} alt="测试图片显示" title="牛牛牛" width="100" height="150">',
                  yagmail.inline(r'D:\zhuomian\test_file\546.jpg')],  # 支持字符串和列表,使用列表会自动换行分段,支持添加普通内容\HTML\图片
        attachments=[r'D:\zhuomian\test_file\学习培训导入.xlsx', r'D:\zhuomian\导入文件\批量导入模板.xlsx',
                     r'D:\zhuomian\test_file\345.txt', r'D:\zhuomian\app接口压测.pdf'],  # 附件,多个附件用列表
        # cc='',  # 抄送,同传统抄送意思(将邮件同时发送给收件人以外的人),使用方法和to参数一致
        # bcc=None,  # 密送(与抄送不同的是收件人看不到其他人的邮箱地址)
        # preview_only=False,  # 仅预览(可以发送但对方收不到)
        # headers=None,  # 头部信息
        # prettify_html=True,  # 修饰为html
        # message_id=None,  # 消息id
        # group_messages=True  # 组消息
    )
    yag.send_unsent()  # 如果发送失败,调用send_unsent方法可以再次尝试发送失败的邮件
    yag.close()

猜你喜欢

转载自blog.csdn.net/JBY2020/article/details/131812607
今日推荐