思路如下:
1. 构造MIMEMultipart对象做为根容器
2. 构造MIMEText对象做为邮件显示内容并附加到根容器
3. 构造MIMEBase对象做为文件附件内容并附加到根容器
a. 读入文件内容并格式化
b. 设置附件头
4. 设置根容器属性
5. 得到格式化后的完整文本
6. 用smtp发送邮件
python 怎么生出成一个表格,并发送邮件
你可以在这个网页上看看fiiii.com/qiushibaike_collector/,这个网页上就是自动登录并使用一个免费的163邮箱向其他指定的邮箱发送邮件。
mail_host=”smtp.163.com”
mail_user=”********”
mail_pass=”
********
me=’luotuo512<luotuo512@163.com>’
to_list=[‘xielvbudengyuling@163.com’]
msg = MIMEText(content)
msg[‘Subject’] = sub
msg[‘From’] = me
msg[‘To’] = “;”.join([‘xielvbudengyuling@163.com’])
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(‘luotuo512’,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
logger.debug(msg)
return True
except Exception, e:
logger.exception(“{0}:{1}”.format(Exception,ex))
return False
>>> from django.core.mail import send_mail
>>> subject = ‘this is a test mail’
>>> message = ‘<table><tr><td>123</td><td>456</td></tr></table>’
>>> send_mail(subject, message, sender_mail, [user.mail], fail_silently=False)
在message里面加有样式的html代码,然后在settings.py里面配置sender_mail的邮箱地址,登录用户以及密码,user_mail是你要发送邮件的客户邮箱。