03-发送邮件

  • 阅读: 478
  • 更新: 2022-06-11

yagmail 库使你发送邮件更加简单快捷,用就对了
项目地址:https://github.com/kootenpv/yagmail

1. 安装

1
pip install yagmail

2. 用法示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import yagmail


def sendmail(receivers, subject, content, attachments=None):
    try:
        smtpObj = yagmail.SMTP(
            user='2496242409@qq.com',
            password='lbzez******beccd',  # 在 qq 邮箱设置中开启IMAP/SMTP服务并申请授权码
            host='smtp.qq.com',
            port=465,
        )
        smtpObj.send(receivers, subject, content, attachments)
        print('邮件发送成功')
    except Exception as e:
        print(f'邮件发送失败,{e}')


receivers = ['123456789@qq.com']
subject = '我是主题'
content = '我是邮件正文'
attachments = [
    '/tmp/123.txt',
    'turtle_数字时钟.py',
]
sendmail(receivers, subject, content, attachments)

附件只需要传入文件的路径即可
抄送、密送也都支持,具体查看项目文档


=== 全文完 ===


欢迎加入QQ群:855013471

京公网安备 11011302003970号 京ICP备2022012301号-1