Python SMTP发送邮件

一、准备

1、知识准备

python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。

2、smtplib用到的方法

1 #coding=utf-8
2 import smtplib #引入包
3 
4 
5 smtpObj = smtplib.SMTP() #创建对象
6 smtpObj.connect() #连接smtp服务器
7 smtpObj.login()   #登录邮箱
8 smtpObj.sendmail()  #发送邮件
9 smtpObj.quit() #退出

3、email用到的模块和方法

from email.mime.text import MIMEText  #格式化文本
from email.header import Header       #格式化邮件头
from email.mime.multipart import MIMEMultipart  #带附件的邮件
from email.mime.image import MIMEImage  #带图片的邮件

msg=MIMEText()  #使用MIMEText()将发送的消息标准化

#标准邮件需要三个头部信息: From, To, 和 Subject。
msg['From']='xxx' #必须定义
msg['To']='xxx' #可以不定义
msg['Subject']=Header()#使用Header()将发送的消息主题标准化


msg=MIMEMultipart()#构造MIMEMultipart对象,用以发送带附件的邮件
msg.attach() #附件

msg=MIMEImage()
msg.add_header()

二、七种邮件内容发送方法实例

1、文件形式的邮件

 1 #coding=utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 from email.header import Header
 5 
 6 sender = 'xxx@163.com' #定义发送邮箱地址
 7 receiver = 'xxx@qq.com'  #定义接收邮箱地址
 8 subject = '这是邮件主题' #定义邮件主题
 9 content = '这是邮件内容'
10 smtpserver = 'smtp.163.com'   #定义邮箱服务器
11 username = 'xxx@163.com' #定义登录邮箱账户
12 password = '****'  #定义登录邮箱密码
13 
14 msg = MIMEText(content,'plain','utf-8' )#第二个参数使用'plain',如果使用'text'发送内容为空,不知道为什么
15 msg['From']=sender #一定要指定'From'属性,否则会报554
16 msg['To']=receiver #多个邮件接收人时,如果报错,可以不指定
17 msg['Subject'] = Header(subject, 'utf-8')
18 
19 #发送邮件
20 if __name__=='__main__':
21     smtp = smtplib.SMTP()
22     smtp.connect('smtp.163.com')
23     smtp.login(username, password)
24     smtp.sendmail(sender, receiver, msg.as_string())
25     smtp.quit()

2、HTML形式的邮件

 1 #coding: utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 
 5 #定义收发邮件的邮箱地址
 6 sender = 'xxx@163.com'
 7 receiver = 'xxx@qq.com'
 8 #定义邮件主题和正文
 9 subject = '这是邮件主题'
10 content = '</pre><h1>这是html格式的邮件内容</h1><pre>'
11 #定义邮箱服务器和邮箱账户
12 smtpserver = 'smtp.163.com'
13 username = 'xxx@163.com'
14 password = '****'
15 
16 #参数'html'指定邮件内容格式
17 msg = MIMEText(content,'html','utf-8')
18 
19 #标准邮件三大构成
20 msg['Subject'] = subject
21 msg['From'] = sender
22 msg['To'] = receiver
23 
24 
25 if __name__=='__main__':
26     smtp = smtplib.SMTP()
27     smtp.connect('smtp.163.com')
28     smtp.login(username, password)
29     smtp.sendmail(sender, receiver, msg.as_string())
30     smtp.quit()

3、带图片的HTML邮件

 1 #coding: utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 from email.mime.image import MIMEImage
 5 from email.mime.multipart import MIMEMultipart
 6 
 7 #定义收发邮件的邮箱地址
 8 sender = 'xxxx@163.com'
 9 receiver = 'xxxx@qq.com'
10 #定义邮件主题和正文
11 subject = '这是邮件主题'
12 # 通过cid将图片文件关联起来
13 content ='<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!'
14 #定义邮箱服务器和邮箱账户
15 smtpserver = 'smtp.163.com'
16 username = 'xxxx@163.com'
17 password = '****'
18 
19 #如果存在内嵌资源,至少要定义multipart/related段。本例中html内嵌图片资源,所以使用related
20 msgRoot = MIMEMultipart('related')
21 msgRoot['Subject'] = subject
22 
23 msgText = MIMEText(content,'html','utf-8')
24 msgRoot.attach(msgText) #关联邮件内容
25 
26 #获取并读取图片文件
27 fp = open('D:\Hydrangeas.jpg', 'rb')
28 msgImage = MIMEImage(fp.read())
29 fp.close()
30 
31 msgImage.add_header('Content-ID', 'image1')
32 msgRoot.attach(msgImage) #关联图片资源
33 
34 
35 if __name__=='__main__':
36     smtp = smtplib.SMTP()
37     smtp.connect('smtp.163.com')
38     smtp.login(username, password)
39     smtp.sendmail(sender, receiver, msgRoot.as_string())
40     smtp.quit()

4、带附件的邮件

 1 #coding: utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 from email.mime.image import MIMEImage
 5 from email.mime.multipart import MIMEMultipart
 6 
 7 #定义收发邮件的邮箱地址
 8 sender = 'xxxx@163.com'
 9 receiver = 'xxxx@qq.com'
10 #定义邮件主题和正文
11 subject = '这是邮件主题'
12 content ='<html><h1>这是一封带附件的邮件</h1></html>'
13 #定义邮箱服务器和邮箱账户
14 smtpserver = 'smtp.163.com'
15 username = 'xxxxx@163.com'
16 password =' ****'
17 
18 #定义邮件三大主题
19 #如果一封邮件中含有附件,那邮件的Content-Type域中必须定义multipart/mixed类型
20 msgRoot = MIMEMultipart('mixed')
21 msgRoot['Subject'] = subject
22 msgRoot['From'] = sender
23 msgRoot['To'] = receiver
24 
25 
26 #构造邮件正文
27 msgText = MIMEText(content,'html','utf-8')
28 msgRoot.attach(msgText)
29 
30 #构造附件
31 fp = open('d:\\Hydrangeas.jpg','rb')
32 msgAtt = MIMEText(fp.read(),'base64','utf-8')
33 msgAtt["Content-Type"] = 'application/octet-stream'
34 msgAtt["Content-Disposition"] = 'attachment; filename="Hydrangeas.jpg"'
35 msgRoot.attach(msgAtt)
36 
37 
38 if __name__=='__main__':
39     smtp = smtplib.SMTP()
40     smtp.connect('smtp.163.com')
41     smtp.login(username, password)
42     smtp.sendmail(sender, receiver, msgRoot.as_string())
43     smtp.quit()

5、群发邮件

#coding: utf-8
import smtplib
from email.mime.text import MIMEText


#定义收发邮件的邮箱地址
sender = 'xxxx@163.com'
#将多个收件人放到数组中
receiver = ['1111@qq.com','2222@sina.com','3333@126.com']
#定义邮件主题和正文
subject = '这是邮件主题'
content ='<html><h1>这是一封群发邮件</h1></html>'
#定义邮箱服务器和邮箱账户
smtpserver = 'smtp.163.com'
username = 'xxxxx@163.com'
password = '****'

#定义邮件三大主题
msg = MIMEText(content,'html','utf-8')
msg['Subject'] = subject
msg['From'] = sender
# 实践发现需要将msg['To'] = receiver修改为以下形式
msg["To"] = ",".join(receiver)




if __name__=='__main__':
    smtp = smtplib.SMTP()
    smtp.connect('smtp.163.com')
    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

6、各种元素都包含的邮件

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage 

sender = 'xxxx@163.com'
receiver = 'xxxx@126.com'
subject = '这是邮件主题'
smtpserver = 'smtp.163.com'
username = 'xxxx@163.com'
password = '****'

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

#构造附件
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att)

if __name__=='__main__':
    smtp = smtplib.SMTP()
    smtp.connect('smtp.163.com')
    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

 

posted @ 2017-07-11 15:43  蓝裙子  阅读(1495)  评论(0编辑  收藏  举报