python发送邮件简单好用,适合拿来当脚本
import smtplib,os
import pyrfc
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time
class SendMail(object):
def __init__(self):
'''
:param username: 用户名
:param passwd: 密码
:param recv: 收件人,多个要传list ['a@qq.com','b@qq.com]
:param title: 邮件标题
:param content: 邮件正文
:param file: 附件路径,如果不在当前目录下,要写绝对路径,默认没有附件
:param ssl: 是否安全链接,默认为普通
:param email_host: smtp服务器地址,默认为163服务器
:param port: 非安全链接端口,默认为25
:param ssl_port: 安全链接端口,默认为465
'''
self.username = "839438304@qq.com"
self.passwd = "xxxxxxx"
self.recv = ['839438304@qq.com']
self.title = "超级BOM版本错误预警"
self.email_host = "mail.qq.com"
self.port = 25
self.ssl = True
self.ssl_port = 465
Blue_Rfc800 = {
"user": "xxxxx",
"passwd": "xxxxxx",
"ashost": "192.168.1.1",
"sysnr": "00",
"lang": "ZH",
"client": "300"
}
self.sapconn = pyrfc.Connection(**Blue_Rfc800)
def send_mail(self,title,content,recv):
msg = MIMEMultipart()
msg.attach(MIMEText(content, 'html', 'utf-8'))
msg['Subject'] = title
msg['From'] = self.username
msg['To'] = ','.join(recv)
ccrecv = ['caozq@desay.com']
msg['Cc'] = ','.join(ccrecv)
if self.ssl:
self.smtp = smtplib.SMTP_SSL(self.email_host,port=self.ssl_port)
else:
self.smtp = smtplib.SMTP(self.email_host,port=self.port)
self.smtp.login(self.username,self.passwd)
try:
self.smtp.sendmail(self.username,recv,msg.as_string())
pass
except Exception as e:
msg = {"code":"E","msg":e}
else:
msg = {"code":"S","msg":"发送成功"}
self.smtp.quit()
return msg
def get_ZSEND_MM01(self):
in_date = time.strftime("%Y%m%d", time.localtime(time.time()-86400))
result = self.sapconn.call("ZSEND_MM01",IN_DATE=in_date)
content = ""
if result["IT_TAB"]:
msg_text = "<font size ='5' color='red'>此信件为系统自动传送,请勿直接回复!</font><br>\n"\
"<font size ='5' color='red'>以下料号刚发布了非人民币价格,请及时补充关务信息!</font><br>\n"\
"<style> .table1_3 table {width:100%;margin:15px 0}\n" + \
".table1_3 th {background-color:#87CEFA;color:#000000}\n" + \
".table1_3,.table1_3 th,.table1_3 td{font-size:0.95em;text-align:center;padding:4px;border:1px " + \
"solid #dddddd;border-collapse:collapse}\n" + \
".table1_3 tr:nth-child(odd){background-color:#d7eefd;}\n" + \
".table1_3 tr:nth-child(even){background-color:#fdfdfd;}</style>\n" + \
"<table class=table1_3>\n" + \
"<tr align='center'>\n" + \
"<th>采购组织</th><th>物料编号</th><th>物料描述</th><th>基本计量单位</th></tr>\n"
text = ""
for data in result["IT_TAB"]:
text = text+"<tr align='center'>\n" + \
"<td>"+data["EKORG"]+"</td>\n<td>"+ data["MATNR"]+"</td>\n<td>"+data["MAKTX"]+"</td>\n" \
"<td>"+data["MEINS"]+"</td>\n" + \
"</tr>\n "
content = msg_text + text + "</table>"
else:
msg_text = "<font size ='5' color='red'>此信件为系统自动传送,请勿直接回复!</font><br>\n"\
"<font size ='5' color='red'>当日无新增美元料号!</font><br>\n"
content = msg_text
return content
if __name__ == '__main__':
m = SendMail()
content = m.get_ZSEND_MM01()
print(content)
title = "超级BOM版本错误预警"
recv = ['caozq@desay.com','839438304@qq.com']
result = m.send_mail(title,content,recv)
print(result)
|