python 的发送邮件使用.
一, python 内置的模块 smtplib 为发送邮件模, 不太好用.
# import smtplib #内置模块, 不好用
二, 使用 yagmail 模块
1, 使用 pip install yagmail, 安装的模块时, 附件名为中文的时候会是乱码.
2, 使用修改过的模块 .whl 的安装包, 安装方式为
pip install xxxxx.whl
3, 发送邮件的步骤
, 在你的邮箱设置里面打开 smtp 服务
, 开启邮箱授权码, 记住这个授权码
4, 发送邮件示例, 用 qq 邮箱服务
- import yagmail
- username = '12345678@qq.com'
- passwd = 'wewwwwwwwwwww' #授权码
- mail = yagmail.SMTP(user=username, password=passwd, host='smtp.qq.com', smtp_ssl=True) #host 根据不同的邮箱不同
- # 如果是 qq 的邮箱或是你们公司邮箱使用的是安全协议的话, 要写 smtp_ssl=True
- #port=25 或是 port=465
- mail.send(to=['123456789@qq.com','23456789877@qq.com'],
- cc='6666666666@qq.com', #抄送
- subject='发送邮件, 啦啦', #邮件标题
- contents='你好, 今天开心吗?',# 邮件的正文
- attachments=[r'C:\Users\Administrator\Desktop\testsendmail.txt',r'C:\Users\Administrator\Desktop\test1.txt']
- )
- # mail.send(to=['11111111@qq.com'], cc=['2222222222@qq.com'])
注: 如果邮件给多个的人时候, to=[] 为列表, 如果为单个用户的时候, 可以使用 to = 指定邮箱
来源: http://www.bubuko.com/infodetail-2611348.html