import platform
import smtplib
from email.header import Header
from email.mime.text import MIMEText
import requestsdef sendMail(subject, body, receiver_address, oncopy_address):"""windows和linux环境都支持发送邮件:param subject: 邮件主题:param body: 内容:param receiver_address: 收件人,';' 分隔:param oncopy_address: 抄送人,';' 分隔:return:"""subject = subject.replace('', '').replace('', '').replace('', '').replace('#', '').replace('*', '')body = body.replace('', '').replace('', '').replace('','').replace('>', '').replace('"', "").replace("'", "").replace("`", "").replace("**", "")HTMLBody = """
Mail Test
##xxx程序执行信息,请相关同事注意。##
%s
""" % (body.strip())sender = 'xxxx.com' # 发送者smtpserver = 'xxxxxx.com'smtpserver_port = 25msg = MIMEText(HTMLBody, _subtype='html', _charset='utf-8')msg['Subject'] = Header(subject, 'utf-8')msg['From'] = Header(sender, 'utf-8') # 发送者msg['To'] = receiver_address # 收件人,将列表转换为字符串以;隔开msg['Cc'] = oncopy_address # 抄送人,将列表转换为字符串以;隔开smtp = ''try:smtp = smtplib.SMTP(smtpserver, smtpserver_port)smtp.connect(smtpserver, smtpserver_port)# smtp.login(username,password)smtp.starttls()smtp.sendmail(sender, receiver_address.split(";") + oncopy_address.split(";"), msg.as_string())print("sendmail finshed!")except Exception as err:print("send mail failed")finally:smtp.quit()# 需按照pypiwin32包,命令为:pip install pypiwin32
import win32com.client as win32
def send_mail(subject, body, receiver_address, oncopy_address):"""Windows环境下发送邮件:param subject::param body::param receiver_address::param oncopy_address::return:"""subject = subject.replace('', '').replace('', '').replace('', '').replace('#', '').replace('*', '')body = body.replace('', '').replace('', '').replace('','').replace('>', '').replace('"', "").replace("'", "").replace("`", "").replace("**", "")# 调用Outlook applicationoutlook = win32.Dispatch('Outlook.Application')mail_item = outlook.CreateItem(0) # 0: olMailItemmail_item.To = receiver_address # 收件人mail_item.CC = oncopy_address # 抄送人mail_item.Subject = subject # 主题mail_item.BodyFormat = 2 # 2: Html format# 邮件bodymail_item.HTMLBody = """
Mail Test
##xxx程序执行信息,请相关同事注意。##
%s
"""%(body.strip())# 添加附件# mail_item.Attachments.Add(xlfile)mail_item.Send()def send_wechat(subject, body, url):"""发送企业微信报警文本类型:文本内容,最长不超过2048个字节,必须是utf8编码markdown类型:markdown内容,最长不超过4096个字节,必须是utf8编码:param subject:主题:param body:内容:param url:企业微信机器人id:return:"""content="""
{subject}
{body}""".format(subject=subject,body=body)content = content.encode('utf-8')[:4096].decode('utf-8')system = platform.system()headers = {"accept": "*/*","Content-Type": "application/json"}data = {"appName": "wework","format": "json","param": {"addressList": [url],"appName": "wework","attach": [],"businessSubject": "异常告警","contentBody": {"content": content,"contentType": "markdown","title": ""},"type": "wework"},"sign": "","source": "","timestamp": "","version": ""}# windows本地跑代码if system == 'Windows':print("Windows跳过发微信步骤")###上线需要关闭# url = 'http://xxxxxxx'# r = requests.post(url, headers=headers, json=data, verify=False)# print(r.text)else:print("发送企业微信")url = 'http://xxxxxxx'r = requests.post(url, headers=headers, json=data, verify=False)print(r.text)
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!