pyzmail比默认smtplib和mime模块简单很多。

模块首页 http://pyzmail.readthedocs.io/en/latest/

python3.2以上,pip install pyzmail

python3.6,pip install pyzmail36

python3.7,好像不兼容(20180725)import pyzmail

 

import pyzmail
#import smtplib

#发件人
sender = ('测试','xxx@xx.com')
#收件人
recipients = ['xx@xxxl.com']
#邮件标题
subject = '标题test'
#邮件内容
text_content = '内容中文文本'

encoding = 'utf8'
#用gbk编码可以解决foxmail乱码

#构成邮件
payload, mail_from, rcpt_to, msg_id = pyzmail.compose_mail(sender, recipients, subject, encoding, (text_content, encoding))

#smtp用户密码
smtp_host = 'smtp.xx.com'
user = 'xxx@xx.com'
passwd = 'xxxxx'

'''
#使用smtplib模块发邮件
server = smtplib.SMTP(smtp_host)
server.login(user, pwd)
server.sendmail(mail_from, rcpt_to, payload)
server.quit()
'''

#发送邮件
ret=pyzmail.send_mail(payload,mail_from,rcpt_to,smtp_host,
                      smtp_login=user,smtp_password=passwd)

if isinstance(ret, dict):
    if ret:
        print('failed recipients:', ', '.join(ret.keys()))
    else:
        print('success')
else:
    print('error:', ret)

 

相关文章:

  • 2022-01-08
  • 2022-02-17
  • 2022-01-10
  • 2021-07-04
  • 2021-09-11
  • 2021-08-28
猜你喜欢
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-11-29
相关资源
相似解决方案