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()
相关文章推荐
- python之简单邮件发送(带附件、图片)
- Python中Django发送带图片和附件的邮件
- python发送邮件的实例代码(支持html、图片、附件)
- python发送邮件,含有正文,附件,正文中含有图片(图片直接在邮件内容中显示)
- python发送包含html、图片、附件和链接的邮件
- python发送邮件的实例代码(支持html、图片、附件)
- Python-SMTP发送邮件(HTML、图片、附件)
- python发送邮件的实例代码(支持html、图片、附件)
- python应用系列教程——python使用smtp协议发送邮件:html文本邮件、图片邮件、文件附件邮件
- 【Python】发送带文字图片附件的邮件
- Python smtplib发送邮件 包含文本、附件、图片等
- python发送包含html、图片、附件和链接的邮件
- 【Python】发送带文字图片附件的邮件
- python发送包含html、图片、附件和链接的邮件
- Python smtplib发送邮件 包含文本、附件、图片
- python发送包含html、图片、附件和链接的邮件
- Python3.5 email发送邮件,包含txt、图片、HTML、附件
- python爬虫实现带附件+html内容以及图片的邮件发送
- C#邮件发送 批量发送邮件 发送邮件嵌入图片 添加附件
- python实现自动发送邮件发送多人、群发、多附件的示例