Smtplib and email encapsulate python to send mail module class sharing
- 2020-04-02 13:26:54
- OfStack
#!/usr/bin/python
# encoding=utf-8
# Filename: send_email.py
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
class SendEmail:
# Constructor: initializes the basic information
def __init__(self, host, user, passwd):
lInfo = user.split("@")
self._user = user
self._account = lInfo[0]
self._me = self._account + "<" + self._user + ">"
server = smtplib.SMTP()
server.connect(host)
server.login(self._account, passwd)
self._server = server
# Send file or html mail
def sendTxtMail(self, to_list, sub, content, subtype='html'):
# If you are sending a text message _subtype Set to plain
# If you send a html Mail, _subtype Set to html
msg = MIMEText(content, _subtype=subtype, _charset='utf-8')
msg['Subject'] = sub
msg['From'] = self._me
msg['To'] = ";".join(to_list)
try:
self._server.sendmail(self._me, to_list, msg.as_string())
return True
except Exception, e:
print str(e)
return False
# Send file with attachment or html mail
def sendAttachMail(self, to_list, sub, content, subtype='html'):
# Create an instance with an attachment
msg = MIMEMultipart()
# Add the attachment 1
att1 = MIMEText(open(r'D:javaworkPyTestsrcmain.py','rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# Here, filename You can write whatever you want, what's the name, what's the name in the message
att1["Content-Disposition"] = 'attachment; filename="main.py"'
msg.attach(att1)
# Add the attachment 2
att2 = MIMEText(open(r'D:javaworkPyTestsrcmain.py','rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="main.txt"'
msg.attach(att2)
# Add email content
msg.attach(MIMEText(content, _subtype=subtype, _charset='utf-8'))
msg['Subject'] = sub
msg['From'] = self._me
msg['To'] = ";".join(to_list)
try:
self._server.sendmail(self._me, to_list, msg.as_string())
return True
except Exception, e:
print str(e)
return False
# Send file with attachment or html mail
def sendImageMail(self, to_list, sub, content, subtype='html'):
# Create an instance with an attachment
msg = MIMEMultipart()
# Add email content
msg.attach(MIMEText(content, _subtype=subtype, _charset='utf-8'))
# Add picture attachment
image = MIMEImage(open(r'D:javaworkPyTestsrctest.jpg','rb').read())
# File name shown in the attachment list
image.add_header('Content-Disposition', 'attachment;filename=p.jpg')
msg.attach(image)
msg['Subject'] = sub
msg['From'] = self._me
msg['To'] = ";".join(to_list)
try:
self._server.sendmail(self._me, to_list, msg.as_string())
return True
except Exception, e:
print str(e)
return False
# Destructor: frees resources
def __del__(self):
self._server.quit()
self._server.close()
mailto_list = ['xxx@163.com']
mail = SendEmail('smtp.163.com', 'xxx@163.com', 'xxxxxx')
if mail.sendTxtMail(mailto_list, " Test email ", "hello world ! <br><br><h1> Hello, send text file test <h1>"):
print " Send a success "
else:
print " Send failure "
if mail.sendAttachMail(mailto_list, " Test email - With two accessories ", "hello world ! <br><br><h1> Hello, send text file test <h1>"):
print " Send a success "
else:
print " Send failure "
if mail.sendImageMail(mailto_list, " Test email - Bring a picture attachment ", "hello world ! <br><br><h1> Hello, send text file test <h1>"):
print " Send a success "
else:
print " Send failure "