直接看效果同样
是不是图片 html 附件都实现了啊
首先你要开启 POP3/IMAP/SMTP 服务,以腾讯邮箱为例
根据操作说明是这样的:
http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
记下你的授权码,别给别人看到哦
然后咱们上代码:
导入发送邮件用到库:
# -*- coding: utf-8 -*- """ @Time: 2018/1/23 @Author: songhao @微信公众号: zeropython @File: mail_imge.py """ from email.header import Header from smtplib import SMTP_SSL from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# -*- coding: utf-8 -*-
"""
@Time: 2018/1/23
@Author: songhao
@微信公众号: zeropython
@File: mail_imge.py
"""
from
email.header
import
Header
from
smtplib
import
SMTP_SSL
from
email.mime
.
image
import
MIMEImage
from
email.mime
.
multipart
import
MIMEMultipart
from
email.mime
.
text
import
MIMEText
|
填写相关的配置文件
""" 辛苦码字不易,能不能点击下广告 """ #qq邮箱smtp服务器 host_server = 'smtp.qq.com' #sender_qq为发件人的qq号码 sender_qq = 'xxxx@qq.com' #pwd为qq邮箱的授权码 pwd = 'uvhxrahsxqxxzqjebh' #发件人的邮箱 sender_qq_mail = 'xxx@qq.com' #收件人邮箱 receiver = 'xxx@gmail.com'
1
2
3
4
5
6
7
8
9
10
11
12
13
|
"""
辛苦码字不易,能不能点击下广告
"""
#qq邮箱smtp服务器
host_server
=
'smtp.qq.com'
#sender_qq为发件人的qq号码
sender_qq
=
'xxxx@qq.com'
#pwd为qq邮箱的授权码
pwd
=
'uvhxrahsxqxxzqjebh'
#发件人的邮箱
sender_qq_mail
=
'xxx@qq.com'
#收件人邮箱
receiver
=
'xxx@gmail.com'
|
# 邮件的正文内容 mail_content = "" # 邮件标题 mail_title = 'zeropython' # 邮件正文内容 # msg = MIMEMultipart() msg = MIMEMultipart('related') msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender_qq_mail msg["To"] = Header("zeropython", 'utf-8') ## 接收者的别名 msgAlternative = MIMEMultipart('alternative') msg.attach(msgAlternative) # 邮件正文内容 mail_body = """ <p>你好,zeropython 邮件发送测试...</p> <p>这是使用<span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span>登录qq邮箱发送HTML格式和图片的测试邮件:</p> <p><a href='http://www.168seo.cn'>zeropython</a></p> <p>图片演示:</p> <p><img src="cid:send_image"></p> """ # msg.attach(MIMEText(mail_body, 'html', 'utf-8')) msgText = (MIMEText(mail_body, 'html', 'utf-8')) msgAlternative.attach(msgText) # 指定图片为当前目录 fp = open('meinv.jpeg', 'rb') msgImage = MIMEImage(fp.read()) fp.close() # 定义图片 ID,在 HTML 文本中引用 msgImage.add_header('Content-ID', '<send_image>') msg.attach(msgImage) # 构造附件1,传送当前目录下的 attach.txt 文件 att1 = MIMEText(open('attach.txt', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename="attach.txt"' msg.attach(att1) # ssl登录 smtp = SMTP_SSL(host_server) # set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式 smtp.set_debuglevel(1) smtp.ehlo(host_server) smtp.login(sender_qq, pwd) smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) smtp.quit()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# 邮件的正文内容
mail_content
=
""
# 邮件标题
mail_title
=
'zeropython'
# 邮件正文内容
# msg = MIMEMultipart()
msg
=
MIMEMultipart
(
'related'
)
msg
[
"Subject"
]
=
Header
(
mail_title
,
'utf-8'
)
msg
[
"From"
]
=
sender_qq_mail
msg
[
"To"
]
=
Header
(
"zeropython"
,
'utf-8'
)
## 接收者的别名
msgAlternative
=
MIMEMultipart
(
'alternative'
)
msg
.
attach
(
msgAlternative
)
# 邮件正文内容
mail_body
=
"""
<p>你好,zeropython 邮件发送测试...</p>
<p>这是使用python登录qq邮箱发送HTML格式和图片的测试邮件:</p>
<p><a href='http://www.168seo.cn'>zeropython</a></p>
<p>图片演示:</p>
<p><img src="cid:send_image"></p>
"""
# msg.attach(MIMEText(mail_body, 'html', 'utf-8'))
msgText
=
(
MIMEText
(
mail_body
,
'html'
,
'utf-8'
)
)
msgAlternative
.
attach
(
msgText
)
# 指定图片为当前目录
fp
=
open
(
'meinv.jpeg'
,
'rb'
)
msgImage
=
MIMEImage
(
fp
.
read
(
)
)
fp
.
close
(
)
# 定义图片 ID,在 HTML 文本中引用
msgImage
.
add_header
(
'Content-ID'
,
'<send_image>'
)
msg
.
attach
(
msgImage
)
# 构造附件1,传送当前目录下的 attach.txt 文件
att1
=
MIMEText
(
open
(
'attach.txt'
,
'rb'
)
.
read
(
)
,
'base64'
,
'utf-8'
)
att1
[
"Content-Type"
]
=
'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1
[
"Content-Disposition"
]
=
'attachment; filename="attach.txt"'
msg
.
attach
(
att1
)
# ssl登录
smtp
=
SMTP_SSL
(
host_server
)
# set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp
.
set_debuglevel
(
1
)
smtp
.
ehlo
(
host_server
)
smtp
.
login
(
sender_qq
,
pwd
)
smtp
.
sendmail
(
sender_qq_mail
,
receiver
,
msg
.
as_string
(
)
)
smtp
.
quit
(
)
|