• python发送邮件和企业微信


    1. import platform
    2. import smtplib
    3. from email.header import Header
    4. from email.mime.text import MIMEText
    5. import requests
    6. def sendMail(subject, body, receiver_address, oncopy_address):
    7. """
    8. windows和linux环境都支持发送邮件
    9. :param subject: 邮件主题
    10. :param body: 内容
    11. :param receiver_address: 收件人,';' 分隔
    12. :param oncopy_address: 抄送人,';' 分隔
    13. :return:
    14. """
    15. subject = subject.replace('', '').replace('', '').replace('', '').replace('#', '').replace('*', '')
    16. body = body.replace('', '').replace('', '').replace('','').replace('>', '').replace('"', "").replace("'", "").replace("`", "").replace("**", "")
    17. HTMLBody = """
    18. Mail Test
    19. ##xxx程序执行信息,请相关同事注意。##
    20. %s
  • """ % (body.strip())
  • sender = 'xxxx.com' # 发送者
  • smtpserver = 'xxxxxx.com'
  • smtpserver_port = 25
  • msg = 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 application
  • outlook = win32.Dispatch('Outlook.Application')
  • mail_item = outlook.CreateItem(0) # 0: olMailItem
  • mail_item.To = receiver_address # 收件人
  • mail_item.CC = oncopy_address # 抄送人
  • mail_item.Subject = subject # 主题
  • mail_item.BodyFormat = 2 # 2: Html format
  • # 邮件body
  • mail_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)
  • 相关阅读:
    【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
    DDT数据驱动性能测试(一)
    【iOS】——SDWebImage源码学习
    FPGA-结合协议时序实现UART收发器(四):串口驱动模块uart_drive、例化uart_rx、uart_tx
    flask获取post请求参数
    SparkContext 与 SparkContext 之间的区别是什么
    bootstrap学习(四)
    MYSQL——毫秒值和日期类型数据的转换,DATE_SUB的用法
    SpringBoot实现分页查询
    【蓝桥备战】前缀和+差分+高精度
  • 原文地址:https://blog.csdn.net/Gzigithub/article/details/126932373