Skip to content

发送邮件

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

1. 安装

shell
pip install yagmail

2. 用法示例

python
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)

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