Add Class Mail.
This commit is contained in:
parent
38ffac0b70
commit
76fcaa9bb1
|
@ -0,0 +1,122 @@
|
||||||
|
import os,smtplib
|
||||||
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
from email.mime.base import MIMEBase
|
||||||
|
from email.header import Header
|
||||||
|
from email import encoders
|
||||||
|
class SMTPMailer():
|
||||||
|
__send__ = 0
|
||||||
|
__essl__ = False
|
||||||
|
__host__ = {
|
||||||
|
"host": '',
|
||||||
|
"port": 25
|
||||||
|
}
|
||||||
|
__user__ = {
|
||||||
|
"user": '',
|
||||||
|
"pass": ''
|
||||||
|
}
|
||||||
|
__mail__ = {
|
||||||
|
"fr": {"name": '', "addr": ''},
|
||||||
|
"to": [],
|
||||||
|
"cc": [],
|
||||||
|
"bc": [],
|
||||||
|
"attachs": [],
|
||||||
|
"subject": '',
|
||||||
|
"content": ''
|
||||||
|
}
|
||||||
|
def enableSSL(self, ch=True):
|
||||||
|
self.__essl__ = ch
|
||||||
|
|
||||||
|
def host(self, host, port):
|
||||||
|
self.__host__["host"] = host
|
||||||
|
self.__host__["port"] = port
|
||||||
|
|
||||||
|
def user(self, user, password):
|
||||||
|
self.__user__["user"] = user
|
||||||
|
self.__user__["pass"] = password
|
||||||
|
|
||||||
|
def sendFrom(self, name='', addr=''):
|
||||||
|
self.__mail__["fr"]["name"] = name
|
||||||
|
self.__mail__["fr"]["addr"] = addr
|
||||||
|
|
||||||
|
def sendTo(self,recvlist):
|
||||||
|
if isinstance(recvlist, str):
|
||||||
|
recvlist = [recvlist]
|
||||||
|
self.__mail__["to"] = recvlist
|
||||||
|
|
||||||
|
def sendCc(self,recvlist):
|
||||||
|
if isinstance(recvlist, str):
|
||||||
|
recvlist = [recvlist]
|
||||||
|
self.__mail__["cc"] = recvlist
|
||||||
|
|
||||||
|
def sendBc(self,recvlist):
|
||||||
|
if isinstance(recvlist, str):
|
||||||
|
recvlist = [recvlist]
|
||||||
|
self.__mail__["bc"] = recvlist
|
||||||
|
|
||||||
|
def subject(self,text):
|
||||||
|
self.__mail__["subject"] = text
|
||||||
|
|
||||||
|
def content(self, text):
|
||||||
|
self.__mail__["content"] = text
|
||||||
|
|
||||||
|
def attachs(self,filelist):
|
||||||
|
if isinstance(filelist, str):
|
||||||
|
filelist = [filelist]
|
||||||
|
for value in filelist:
|
||||||
|
if not os.path.exists(value):
|
||||||
|
raise Exception('文件未找到 | File not found.')
|
||||||
|
self.__mail__["attachs"] = filelist
|
||||||
|
|
||||||
|
def send(self):
|
||||||
|
if self.__send__:
|
||||||
|
return {"code": 1, "message": 'Mail has been sent.'}
|
||||||
|
message = MIMEMultipart()
|
||||||
|
messageFrom = Header()
|
||||||
|
messageFrom.append(self.__mail__["fr"]["name"])
|
||||||
|
messageFrom.append('<' + self.__mail__["fr"]["addr"] + '>')
|
||||||
|
message["From"] = messageFrom
|
||||||
|
message["To"] = ';'.join(self.__mail__["to"])
|
||||||
|
message["Cc"] = ';'.join(self.__mail__["cc"])
|
||||||
|
message["Bcc"] = ';'.join(self.__mail__["bc"])
|
||||||
|
message['Subject'] = Header(self.__mail__["subject"])
|
||||||
|
message.attach(MIMEText(self.__mail__["content"], 'html', 'utf-8'))
|
||||||
|
for value in self.__mail__["attachs"]:
|
||||||
|
mime = MIMEBase('application', 'octet-stream')
|
||||||
|
mime.add_header('Content-Disposition', 'attachment', filename=os.path.basename(value))
|
||||||
|
mime.set_payload(open(value, 'rb').read())
|
||||||
|
encoders.encode_base64(mime)
|
||||||
|
message.attach(mime)
|
||||||
|
try:
|
||||||
|
if self.__essl__:
|
||||||
|
smtp = smtplib.SMTP_SSL(self.__host__["host"], self.__host__["port"])
|
||||||
|
else:
|
||||||
|
smtp = smtplib.SMTP(self.__host__["host"], self.__host__["port"])
|
||||||
|
except:
|
||||||
|
return {"code": 1, "message": 'Connection failure.'}
|
||||||
|
try:
|
||||||
|
smtp.login(self.__user__["user"], self.__user__["pass"])
|
||||||
|
except:
|
||||||
|
return {"code": 1, "message": 'Authentication failed.'}
|
||||||
|
try:
|
||||||
|
smtp.sendmail(self.__user__["user"], self.__mail__["to"] + self.__mail__["cc"] + self.__mail__["bc"], message.as_string())
|
||||||
|
smtp.quit()
|
||||||
|
self.__send__ = 1
|
||||||
|
return {"code": 0, "message": ''}
|
||||||
|
except Exception as error:
|
||||||
|
return {"code": 1, "message": error}
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# Example.
|
||||||
|
mailer = SMTPMailer()
|
||||||
|
mailer.enableSSL(True)
|
||||||
|
mailer.host('smtp.qq.com', 465)
|
||||||
|
mailer.user('', '')
|
||||||
|
mailer.sendFrom('', '')
|
||||||
|
mailer.sendTo([''])
|
||||||
|
mailer.sendCc([''])
|
||||||
|
mailer.sendBc([''])
|
||||||
|
mailer.subject('这是一封测试邮件')
|
||||||
|
mailer.content('<p>邮件发送HTML格式文件测试</p><p><a href="http://www.baidu.com/">这是一个链接</a></p>')
|
||||||
|
mailer.attachs(['./example.xlsx'])
|
||||||
|
print(mailer.send())
|
|
@ -2,7 +2,9 @@ import re,chardet,platform,subprocess,socket
|
||||||
def ping(host='', version=None):
|
def ping(host='', version=None):
|
||||||
def sh(cmd='', timeout=None):
|
def sh(cmd='', timeout=None):
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=timeout)
|
result = subprocess.run(
|
||||||
|
cmd, shell=True,
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=timeout)
|
||||||
if result.stdout:
|
if result.stdout:
|
||||||
return result.stdout.decode(chardet.detect(result.stdout)["encoding"] or 'UTF-8')
|
return result.stdout.decode(chardet.detect(result.stdout)["encoding"] or 'UTF-8')
|
||||||
return ''
|
return ''
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
import smtplib
|
||||||
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
from email.mime.base import MIMEBase
|
||||||
|
from email.header import Header
|
||||||
|
from email import encoders
|
||||||
|
|
||||||
|
mail_host = {
|
||||||
|
"host": 'smtp.qq.com',
|
||||||
|
"port": 465
|
||||||
|
}
|
||||||
|
mail_user = {
|
||||||
|
"user": 'admin@fanscloud.net',
|
||||||
|
"pass": 'gyzzvmjdlofnbedj'
|
||||||
|
}
|
||||||
|
sender = "admin@fanscloud.net"
|
||||||
|
|
||||||
|
mailToList = [
|
||||||
|
"1634991757@qq.com",
|
||||||
|
"fanscloud@foxmail.com"
|
||||||
|
]
|
||||||
|
mailCcList = [
|
||||||
|
"zhaoyafan@foxmail.com"
|
||||||
|
]
|
||||||
|
mailBcList = [
|
||||||
|
|
||||||
|
]
|
||||||
|
html = '''
|
||||||
|
<p>Python 邮件发送HTML格式文件测试...</p>
|
||||||
|
<p><a href="http://www.runoob.com">这是一个链接</a></p>
|
||||||
|
'''
|
||||||
|
message = MIMEMultipart()
|
||||||
|
messageFrom = Header()
|
||||||
|
messageFrom.append('云凡网络')
|
||||||
|
messageFrom.append('<admin@fanscloud.net>')
|
||||||
|
print(messageFrom)
|
||||||
|
message["From"] = messageFrom
|
||||||
|
message["To"] = ';'.join(mailToList)
|
||||||
|
message["Cc"] = ';'.join(mailCcList)
|
||||||
|
message["Bcc"] = ';'.join(mailBcList)
|
||||||
|
message['Subject'] = Header('主题333')
|
||||||
|
message.attach(MIMEText(html, 'html', 'utf-8'))
|
||||||
|
|
||||||
|
|
||||||
|
# 构造附件2,传送当前目录下的 runoob.txt 文件
|
||||||
|
# att2 = MIMEText(open('./1.xlsx', 'rb').read(), 'base64', 'utf-8')
|
||||||
|
# att2["Content-Type"] = 'application/octet-stream'
|
||||||
|
# file = Header('attachment; filename=')
|
||||||
|
#
|
||||||
|
# att2["Content-Disposition"] = file
|
||||||
|
# message.attach(att2)
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# print(file)
|
||||||
|
|
||||||
|
mime = MIMEBase('application','octet-stream')
|
||||||
|
mime.add_header('Content-Disposition', 'attachment', filename='文件文件.xlsx')
|
||||||
|
mime.set_payload(open('./1.xlsx', 'rb').read())
|
||||||
|
# 用Base64编码:
|
||||||
|
encoders.encode_base64(mime)
|
||||||
|
# 添加到MIMEMultipart:
|
||||||
|
message.attach(mime)
|
||||||
|
|
||||||
|
print("加密后的发送内容\n", message.as_string()) # 打印输出加密后的发送内容
|
||||||
|
#
|
||||||
|
#
|
||||||
|
smtpObj = smtplib.SMTP_SSL(mail_host['host'], mail_host['port'])
|
||||||
|
# smtpObj.connect(mail_host['host'], mail_host['port']) # 链接 SMTP 服务器
|
||||||
|
smtpObj.login(mail_user['user'], mail_user['pass']) # 登录邮箱验证
|
||||||
|
smtpObj.sendmail(sender, mailToList, message.as_string()) # 发送邮件; "message" 通过 "as_string()" 进行发送内容字符串的加密
|
Loading…
Reference in New Issue