python 中使用yagmail 发送邮件功能
程序员文章站
2022-06-18 07:55:10
1.使用前先要安装 yagmailpip install yagmail -i https://pypi.douban.com/simple2.使用qq邮箱发送邮件,使用的是授权码,需要先到qq邮箱申...
1.使用前先要安装 yagmail
pip install yagmail -i https://pypi.douban.com/simple
2.使用qq邮箱发送邮件,使用的是授权码,需要先到qq邮箱申请授权码。
邮箱设置-->账户
3.yagmail 模块发送邮件更加简单,四行代码
# -*- encoding: utf-8 -*- import yagmail def e_mali_jj(fr,key,etype,text,to,attachments): ''' :param fr: 发送邮箱 :param key: 授权码 :param etype: 邮件类型 :param text: 文本 :param to: 接受邮箱 :param attachments: 附件文件地址,空则填'' :return: ''' # 链接邮箱服务器 yag=yagmail.smtp(user=fr,password=key,host=etype) # 邮箱正文 contents=[text] # 发送邮件 yag.send(to=to,subject='邮件标题',contents=contents, attachments=attachments) #subject 标题 yag.close() print("邮件发送成功") if __name__ == '__main__': e_mali_jj("123456@qq.com", "gwheybuaamrqbihh", 'smtp.qq.com', "邮件正文内容", "123456@qq.com", "e:\\proto_code\\roshan-01-microscript-proto_test-master-src\\src\\report\\report.html")
邮件发送给多个人,将接受的邮箱放在列表中即可
# 发送邮件 yag.send(to = ['123456@qq.com','678910@qq.com', '10111213@qq.com'], subject='subject', contents = contents, attachments="")
4.发送邮件带附件
# -*- coding:utf-8 -*- import yagmail yag = yagmail.smtp( user="157540957@qq.com", password="kayzilfyziulbhbb1", host='smtp.qq.com') """ user: 发送的邮箱 password: 授权码 """ # 邮箱正文 contents = ['测试发送邮件'] # 附件 attachments = "d:\\code\\0906\\api_test009\\report\\report.html" # 发送邮件 try: yag.send(to = '3437871062@qq.com', subject='subject', contents = contents, attachments=attachments) except exception as e : print("error: 抱歉!发送邮件失败。", e) """ to : 接收者 subject : 邮件主题 contents: 正文 attachments: 附件 """ yag.close()
5.封装
# -*- coding:utf-8 -*- import yagmail def send(user, password, receiver): yag = yagmail.smtp( user=user, password=password, host='smtp.qq.com') """ user: 发送的邮箱 password: 授权码 """ # 邮箱正文 contents = ['测试发送邮件'] # 附件 attachments = "d:\\code\\0906\\api_test009\\report\\report.html" # 发送邮件 try: yag.send(to=receiver, subject='subject', contents = contents, attachments=attachments) except exception as e : print("error: 抱歉!发送邮件失败。", e) """ to : 接收者 subject : 邮件主题 contents: 正文 attachments: 附件 """ yag.close() if __name__ == '__main__': send("123456@qq.com", "kayzilfyziulbhbb1", "45678910@qq.com")
到此这篇关于python 中使用yagmail 发送邮件的文章就介绍到这了,更多相关python yagmail 发送邮件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!