PyEmail小轮子初版开源
开源一个自动化邮件小轮子,代码来源于工作需要,已去除工作敏感信息,后续更新增强版
PyEmail.py
# -*- coding: utf-8 -*-
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.utils import parseaddr, formataddr
import smtplib
import os
class PyEmail():
def __init__(self, from_addr,password,smtp_server='smtp.exmail.qq.com',server_port=25):
self.__to_addr = ''
self.__cc_addr = ''
self.__subject = ''
self.__msgs = ''
self.__text_imgs_path=[]
self.__attach_file_path=''
self.__smtp_server = smtp_server
self.__server_port = server_port
self.__from_addr = from_addr
self.__password = password
@property
def from_addr(self):
return self.__from_addr
@property
def to_addr(self):
return self.__to_addr
@property
def msgs(self):
return self.__msgs
@property
def cc_addr(self):
return self.__cc_addr
@property
def subject(self):
return self.__subject
@property
def attach_file_path(self):
return self.__attach_file_path
@property
def text_imags_path(self):
return self.__text_imags_path
@to_addr.setter
def to_addr(self,to_addr):
self.__to_addr = to_addr
@cc_addr.setter
def cc_addr(self,cc_addr):
self.__cc_addr = cc_addr
@msgs.setter
def msgs(self,msgs):
self.__msgs = msgs
@text_imags_path.setter
def text_imags_path(self,text_imgs_path):
self.__text_imgs_path = text_imgs_path
@subject.setter
def subject(self,subject):
self.__subject = subject
@attach_file_path.setter
def attach_file_path(self,attach_file_path):
self.__attach_file_path = attach_file_path
def __format_addr(self,s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
# 支持excel/pdf/mp3等
def __attach_file(self,attach_file_path,msg):
part = MIMEApplication(open(attach_file_path, 'rb').read())
filename = os.path.split(attach_file_path)[-1]
part.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(part)
def __get_msgImage(self,img_path):
fp = open(img_path,'rb')
msgImage = MIMEImage(fp.read())
fp.close()
return msgImage
def __add_text_img(self,msg,text_imags_path):
i = 1
for img_path in text_imags_path:
msgImage = self.__get_msgImage(img_path)
msgImage.add_header('Content-ID','<image{0}>'.format(i))
msg.attach(msgImage)
i = i + 1
def __init_mail__(self):
msg = MIMEMultipart()
msg['From'] = self.__format_addr(self.__from_addr)
msg['To'] = self.__format_addr(self.__to_addr)
msg['Subject'] = Header(self.__subject, 'utf-8').encode()
msg['Cc']= self.__cc_addr
if(self.__attach_file_path!=''):
self.__attach_file(self.__attach_file_path,msg)
muti_mail = []
muti_mail.append(self.__to_addr)
if(len(self.__cc_addr) != 0 and ('@' in self.__cc_addr)):
muti_mail.append(self.__cc_addr)
if (self.__text_imgs_path != [] ):
self.__add_text_img(msg,self.__text_imgs_path)
return (msg,muti_mail)
def sendEmail(self,mailtype):
try:
server = smtplib.SMTP(self.__smtp_server, self.__server_port,timeout=90)
server.login(self.__from_addr, self.__password)
(msg,muti_mail) = self.__init_mail__()
if(mailtype == 'plain'):
msg.attach(MIMEText(self.__msgs, 'plain', 'utf-8'))
if(mailtype == 'html'):
msg.attach(MIMEText(self.__msgs, 'html', 'utf-8'))
#server.sendmail(self.__from_addr, muti_mail, msg.as_string())
server.quit()
print('send to:',self.__to_addr,' successfully!')
except Exception as e:
print(e)
def testmail(self):
try:
content='this is test email send by python'
msg = MIMEText(content, 'plain', 'utf-8') # 文本邮件
msg['From'] = self.__format_addr('from_email <%s>' %self.__from_addr)
msg['To'] = self.__format_addr('to_email <%s>' %self.__to_addr)
msg['Subject'] = Header('Test Email', 'utf-8').encode()
server = smtplib.SMTP(self.__smtp_server, self.__server_port)
server.starttls() # 调用starttls()方法,就创建了安全连接
server.login(self.__from_addr, self.__password) # 登录邮箱服务器
server.sendmail(self.__from_addr, self.__to_addr, msg.as_string()) # 发送信息
server.quit()
print("加密后邮件发送成功!")
except Exception as e:
print( e)
使用示例
myemail = PyEmail.PyEmail(from_addr,password)
myemail.cc_addr='cc_email@example.com'
myemail.to_addr='to_email@example.com'
myemail.subject='test Subject'
myemail.msgs=msgs
myemail.text_imags_path=['1.jpg','2.jpg']
myemail.attach_file_path='Document1.docx'
myemail.sendEmail(mailtype='html')