yagmail库——简单发邮件

项目主页:https://github.com/kootenpv/yagmail

  1. 安装
pip3 install yagmail
pip3 install keyring
  1. 简单例子
import yagmail
yag = yagmail.SMTP(user = '[email protected]', password = 'xxx', host = 'smtp.163.com')
yag.send(to = '[email protected]', subject = 'This is subject', contents = 'This is content')

注意:

  • 如果你的邮箱没有开通IMAP/POP3/SMTP服务,就会提示如下错误信息:
    smtplib.SMTPAuthenticationError: (550, b'User has no permission')
    以163邮箱为例,解决方案在网页上登录自己邮箱,点击“设置->POP3/SMTP/IMAP”,勾选“POP3/STMP服务”和“IMAP/STMP服务”,弹窗会提示你设置授权码,点击“确定”,点击“开启”,通过验证后,即可开启。记住自己输入的授权码,在password中输入授权码,即可登录。
  • 如果程序运行没出错,而目标邮箱没有收到邮件,请检查垃圾箱。
  1. 给多个邮箱发邮件
yag.send(to = ['[email protected]','[email protected]','[email protected]'], 'subject', 'contents')
  1. 发送附件
yag.send(to = '[email protected]', subject = 'subject', 
         contents = ['hello', 'C:\\Users\\Desktop\\test.txt'])

注意:如果邮件发送之后被退回,那么程序不会显示任何错误信息,但是发件人的邮箱会有退信邮件。

  1. 更多细节
yagmail.register('mygmailusername', 'mygmailpassword')

更多细节请参考项目主页。

你可能感兴趣的:(yagmail库——简单发邮件)