python3.4 Realizes the mail sending function
- 2020-10-07 18:45:54
- OfStack
This article shares the specific code of python's E-mail sending function for your reference. The specific content is as follows
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
user = '*******@qq.com'
pwd = '*******'
to = ['******@139.com', '******@qq.com']
msg = MIMEMultipart()
msg['Subject'] = ' Here is the theme ...'
content1 = MIMEText(' Here's the text! ', 'plain', 'utf-8')
msg.attach(content1)
attfile = 'C:\\Users\\hengli\\Pictures\\CameraMan\\ Ha ha .doc'
basename = os.path.basename(attfile)
fp = open(attfile, 'rb')
att = MIMEText(fp.read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment',filename=('gbk', '', basename))
encoders.encode_base64(att)
msg.attach(att)
#-----------------------------------------------------------
s = smtplib.SMTP('smtp.qq.com')
s.login(user, pwd)
s.sendmail(user, to, msg.as_string())
print(' Send a success ')
s.close()