1、邮箱的基本用法
import yagmail username='123456@qq.com' #发邮件的邮箱名称 password='123456' #如果是QQ和网易邮箱,需要填写非邮箱密码而是授权码 smtp='smtp.qq.com' #host,如果是163请QQ替换163 mail=yagmail.SMTP(user=username,password=password,host=smtp) #声明邮箱对象 to='zhaowj@efreight.cn' #发送人,发送多人请写成list cc='xiaokuangnvhai@sina.com' #抄送人,抄送多人请写成list subject="biaoti" #邮件标题 content='neirong' #邮件内容 files=["a.txt","book.xls"] #邮件附件绝对路径和相对路径均可 mail.send(to=to,cc=cc,subject=subject,contents=content,attachments=files) #发送邮件
2、邮件发送HTML内容
import yagmail username='540493450@qq.com' #发邮件的邮箱名称 password='whvdzwyafknpbedb' #如果是QQ和网易邮箱,需要填写非邮箱密码而是授权码 smtp='smtp.qq.com' #host,如果是163请QQ替换163 mail=yagmail.SMTP(user=username,password=password,host=smtp) #声明邮箱对象 to='zhaowj@efreight.cn' #发送人,发送多人请写成list cc='xiaokuangnvhai@sina.com' #抄送人,抄送多人请写成list subject="测试报告" #邮件标题 file_object=open('a.html') content=file_object.read() #邮件内容为网页内容 file_object.close() mail.send(to=to,cc=cc,subject=subject,contents=content) #发送邮件
备注:html中无法使用css, js。你发送一个<div>片断就好了,不用写整个html页面,因为即使写了,邮件客户端也会删除body之外(包括<body>这个标签)的内容,只留下<body>标签内部的东西
原文:http://www.jb51.net/web/23842.html
参考:https://segmentfault.com/q/1010000000115371
http://groundwire.org/labs/email-publishing/using-css-and-html-in-email-newsletters/
https://litmus.com/blog/html-email-coding-101-infographic/email-coding-101