1-8 live lesson python sends email to send attachments and pictures

Send attachment:

Find a local file first
Open file, read file string
Create an object att through the MIMT ext() class, and pass in the file to read the content
Add the header information of att and specify the file name
Add to MSG message 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)

Example

#!/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>Hello Teacher</h1>
    //Hello, 
            //This is an automated message. 
        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()

Send picture:

A picture must exist locally;
Open the picture and read the contents of the picture
Create the corresponding picture object imgattr = MIMEImage(fimg.read())
Add the header information of the picture, imgattr.add_header('content ID ',')
The id of the picture is specified. If the picture wants to be displayed in the body, it must be displayed in html format: specify the picture id in the front-end code

Add to 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>Hello Teacher</h1>
    //Hello, 
            //This is an automatic email. 
        www.ustchacker.com hello
    <html>
<body>
<h1>hello world</h1>
<p>
//Picture demonstration: < 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()

Keywords: Python

Added by ajaybuilder on Sun, 03 May 2020 22:28:26 +0300