您的位置:首页 > 编程语言 > Python开发

1-8直播课 python发送邮件 发送附件及图片

2018-01-11 10:40 489 查看

发送附件:

先找一个本地的文件

打开文件,读出文件字符串

通过MIMT ext()类来创建一个对象att,传入文件读出内容

增加att的头部信息,并指定文件名字

添加到msg消息中msg.attach(att)

attfile = 'test.py'
basename = os.path.basename(attfile)
fp = open(attfile, 'rb')
att = email.mime.text.MIMEText(fp.read(), 'html', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment',filename=('utf-8', '', basename))#three-tuple of (charset, language, value),
# encoders.encode_base64(att)
msg.attach(att)


示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/1/2 15:36
# @Author : lingxiangxiang
# @File : sendhtml.py
import smtplib
import email.mime.multipart
import email.mime.text
import os
from email import encoders
msg = email.mime.multipart.MIMEMultipart()
msg['from'] = '18910148469@163.com'
msg['to'] = '974644081@qq.com;1414873973@qq.com;lingjing@jd.com'
msg['subject'] = 'ajing1111'
content = '''
<h1>老师好</h1>
你好,
这是一封自动发送的邮件。
www.ustchacker.com hello
'''
txt = email.mime.text.MIMEText(_text=content, _subtype="html")
msg.attach(txt)
#############
attfile = 'test.py' basename = os.path.basename(attfile) fp = open(attfile, 'rb') att = email.mime.text.MIMEText(fp.read(), 'html', 'utf-8') att["Content-Type"] = 'application/octet-stream' att.add_header('Content-Disposition', 'attachment',filename=('utf-8', '', basename))#three-tuple of (charset, language, value), # encoders.encode_base64(att) msg.attach(att)##########
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com', '25')
smtp.login('18910148469@163.com', 'LingJing2315')
smtp.sendmail(from_addr='18910148469@163.com', to_addrs=['974644081@qq.com', '1414873973@qq.com', 'lingjing@jd.com'], msg=msg.as_string())
smtp.quit()


发送图片:

本地必须存在一张图片;

打开图片,并读取图片内容

创建发邮件相对应的图片对象imgattr = MIMEImage(fimg.read())

增加图片的头信息, imgattr.add_header(‘Content-ID’, ‘’)

指定了图片的id,图片如果想在正文中显示,必须通过html的格式显示出来:在前端代码中指定图片id

添加到message的信息中

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/1/2 15:36
# @Author : lingxiangxiang
# @File : sendhtml.py
import smtplib
import email.mime.multipart
import email.mime.text
from email.mime.image import MIMEImage
import os
# from email import encoders, MIMEImage
msg = email.mime.multipart.MIMEMultipart()
msg['from'] = '18910148469@163.com'
msg['to'] = '974644081@qq.com;1414873973@qq.com;lingjing@jd.com'
msg['subject'] = 'ajing1111'
content = '''
<h1>老
4000
师好</h1>
你好,
这是一封自动发送的邮件。
www.ustchacker.com hello
<html>
<body>
<h1>hello world</h1>
<p>
图片演示:<br><img src = 'cid:image1'></br>
</p>
</body>
</html>
'''
txt = email.mime.text.MIMEText(_text=content, _subtype="html")
msg.attach(txt)
#############
attfile = 'test.py' basename = os.path.basename(attfile) fp = open(attfile, 'rb') att = email.mime.text.MIMEText(fp.read(), 'html', 'utf-8') att["Content-Type"] = 'application/octet-stream' att.add_header('Content-Disposition', 'attachment',filename=('utf-8', '', basename))#three-tuple of (charset, language, value), # encoders.encode_base64(att) msg.attach(att)##########
#################
img = "1.jpg"
fimg = open(img, "rb")
imgattr = MIMEImage(fimg.read())
fimg.close()
imgattr.add_header('Content-ID', '<image1>')
msg.attach(imgattr)
################
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com', '25')
smtp.login('18910148469@163.com', 'LingJing2315')
smtp.sendmail(from_addr='18910148469@163.com', to_addrs=['974644081@qq.com', '1414873973@qq.com', 'lingjing@jd.com'], msg=msg.as_string())
smtp.quit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: