使用smtplib发送邮件

smtplib,email 发送邮件的一般流程
先构造邮件的message部分
使用email的mine

最常见的MIME首部是以Content-Type开头的:
1) Content-Type: multipart/mixed 它表明这封Email邮件中包含各种格式的MIME实体但没有具体给出每个实体的类型。
2) Content-Type: multipart/alternative 如果同一封Email邮件既以文本格式又以HTML格式发送,那么要使用Content-Type: multipart/alternative。这两种邮件格式实际上是显示同样的内容但是具有不同的编码。
3) Content-Type: multipart/related 用于在同一封邮件中发送HTML文本和图像或者是其他类似类型。
邮件主体的编码:主要是包括quoted-printable与base64两种类型的编码。Base64和Quoted-Printable都属于MIME(多用途部分、多媒体电子邮件和 WWW 超文本)的一种编码标准,用于传送诸如图形、声音和传真等非文本数据)。
以上内容转自http://www.tuicool.com/articles/byINN3

1.mimemultipart对象 为邮件的容器

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header

msg = MIMEMultipart("alternative")
msg["Subject"] = "test subject"
msg["From"] = from
msg["To"] = to

2.把邮件的内容attach到msg上

text_part = MIMEText("text part")
msg.attach(text_part)//attach一个text
#======================================
html_file = open("D:/d.html","rb")
html_text = MIMEText(html_file.read(),'html','utf-8')
html_file.close()
msg.attach(html_text)//attach一个html
#======================================
png_file = open("D:/256.png",'rb')
png_part = MIMEImage(png_file.read())
png_part.add_header('Content-ID','<image1>')
msg.attach(png_part)//attach一个png到html内容中
png_file.close()
#======================================
file_attachment.add_header('Content-Disposition', 'attachment', filename = "filename")//attach为附件的写法
#======================================
#如果使用MIMEApplication,则
text1 = MIMEApplication(text,"html")
text1.add_header("Content-Type","text/html")
msg.attach(text1)

3.使用smtplib登录smtp服务器

s = smtplib.SMTP_SSL("smtp.qq.com","465")
s.login(user,pwd)
s.sendmail(user,to,msg.as_string())
s.close()

有很多地方还是不懂,仍需要学习

官方文档
email:https://docs.python.org/2/library/email-examples.html
smtplib:https://docs.python.org/2/library/smtplib.html

关于MIME的文章
http://blog.csdn.net/xiemk2005/article/details/6111614

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值