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

发送附件:

先找一个本地的文件
打开文件,读出文件字符串
通过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>老师好</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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值