python发送邮件2

发送附件:
1.先找一个本地的文件
2.打开文件,读出文件字符串
3.通过MIMT  ext()类来创建一个对象att,传入文件读出内容
4.增加att的头部信息,并指定文件名字
5.添加到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['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()
 
 
 
发送图片:
1.本地必须存在一张图片;
2.打开图片,并读取图片内容
3.创建发邮件相对应的图片对象imgattr = MIMEImage(fimg.read())
4.增加图片的头信息, imgattr.add_header('Content-ID', '<image1>')
  指定了图片的id,图片如果想在正文中显示,必须通过html的格式显示出来:在前端代码中指定图片id
<img src = 'cid:image1'>
1.添加到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['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()
posted @ 2018-01-15 16:03  岁月不可追  阅读(117)  评论(0)    收藏  举报