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('<font color=\"info\">', '').replace('<font color=\"warning\">', '').replace('</font>', '').replace('#', '').replace('*', '')body = body.replace('<font color=\"comment\">', '').replace('<font color=\"warning\">', '').replace('</font>','').replace('>', '').replace('"', "").replace("'", "").replace("`", "").replace("**", "")HTMLBody = """
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mail Test</title><style>
body,html,div,ul,li,button,p,pre,h1,h2,h3,h4,h5,h6 {margin: 0;padding: 0;
}pre { // 兼容多个浏览器white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;*word-wrap: break-word;*white-space : normal ;
}
body,html {background: #fff;line-height: 1.8;
}
h1,h2,h3,h4,h5,h6 {line-height: 1.8;
}
h2.email-title {font-size: 26px;font-weight: 100;margin-bottom: 15px;color: #FF5151;
}</style>
</head>
<body>
<h2 class="email-title">
##xxx程序执行信息,请相关同事注意。##
</h2><pre>
%s
</pre></body></html>""" % (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('<font color=\"info\">', '').replace('<font color=\"warning\">', '').replace('</font>', '').replace('#', '').replace('*', '')body = body.replace('<font color=\"comment\">', '').replace('<font color=\"warning\">', '').replace('</font>','').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 = """
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mail Test</title><style>
body,html,div,ul,li,button,p,pre,h1,h2,h3,h4,h5,h6 {margin: 0;padding: 0;
}pre { // 兼容多个浏览器white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;*word-wrap: break-word;*white-space : normal ;
}
body,html {background: #fff;line-height: 1.8;
}
h1,h2,h3,h4,h5,h6 {line-height: 1.8;
}
h2.email-title {font-size: 26px;font-weight: 100;margin-bottom: 15px;color: #FF5151;
}</style>
</head>
<body>
<h2 class="email-title">
##xxx程序执行信息,请相关同事注意。##
</h2><pre>
%s
</pre></body></html>
"""%(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)

使用python发送邮件和企业微信相关推荐

  1. 使用Python实现给企业微信发送消息功能

    本文将介绍如何使用python3给企业微信发送消息,文中有详细的图文解说及代码示例,对正在学习python的小伙伴很有帮助,需要的朋友可以参考下.编程资料点击领取 目录 一.概述 二.python脚本 ...

  2. 用Python写一个企业微信自动化打卡的脚本

    好的,以下是用Python编写企业微信自动化打卡脚本的大致步骤: 安装必要的Python模块,如selenium.webdriver.pyautogui等. 在代码中使用selenium模拟打开企业微 ...

  3. jenkins使用python脚本发送企业微信通知

    如果只是想实现将jenkins的构建结果发送到企业微信进行通知,最简便的方式是安装Qy Wechat Notification Plugin插件,网上教程很多就不重复写了,可参考:https://ww ...

  4. DailyInfo——利用Python实现基于企业微信的每日图文推送

    DailyInfo 基于企业微信的每日图文推送 项目地址 Gitee地址 Github地址 支持功能 Bing必应 每日壁纸 金山词霸 每日一句 ONE·一个 一图一句 和风天气 多地区天气预报 农历 ...

  5. 设置每天定时给自己发送邮件、企业微信提醒

    起因是,企业微信考勤打卡忘记了.本来手机企业微信会在7:45提醒,但前提是需要登录到手机企业微信并且不能黑屏,才能看到这个提醒.         到办公室登录企业微信后,手机企业微信基本不看,如果此时 ...

  6. Python实现接收企业微信接收消息

    想接收企业微信的消息,首先得开启验证信息API(GET请求)以及接收消息服务(POST请求). GET的请求参考: 企业微信验证接口API 第一部分解析POST请求: 并且解析msg_signatur ...

  7. python脚本给企业微信群发送图片

    转载的,精简了一下 1,先在企微种添加一个机器人,然后复制他的webhook结尾的一串key(识别码),创建就有,然后替换到下面代码中,创建机器人参考链接:https://qinglian.tence ...

  8. 利用python脚本实现企业微信机器人定时天气预报

    实现效果图 代码分析 import requests #这个库用来获取网页信息 from bs4 import BeautifulSoup #这个库用来分析选择网页的信息def get_content ...

  9. python推送企业微信机器人3-图片类型

    参数说明 参数 是否必填 说明 msgtype 是 消息类型,此时固定为image base64 是 图片内容的base64编码 md5 是 图片内容(base64编码前)的md5值 效果展示 Dem ...

最新文章

  1. 机器学习算法1_线性回归
  2. 149.从网络的作用范围分类 150.使用范围分类 151.拓扑结构分类
  3. 【Python基础】数据项目总结 -- 蛋壳公寓租金分析!
  4. github持续集成的设置_如何使用GitHub Actions和Puppeteer建立持续集成管道
  5. Python基石 - 收藏集 - 掘金
  6. java.lang.IllegalStateException: Failed to load ApplicationContext selenium 异常 解决
  7. 机构报告:大数据分析提升企业决策水平
  8. 模具设计进程中应注意哪些问题
  9. Anaconda3下YOLOV3火焰检测
  10. java 图片导出word_【freemaker实现导出word②】代码实现导出word(包括导出list数据和导出图片到word)...
  11. [wireshark] 码率统计
  12. 微型计算机分类可以分为哪些,微型计算机的分类通常以微处理器的什么来划分...
  13. pycharm将计算出来的数据导入进数据库MYSQL
  14. labelme标记图像时JSON标签复制
  15. 《视觉SLAM十四讲》笔记(1-3)
  16. PHP学习:PHP+Apache 安装/配置
  17. 大数据建设意义_大数据技术平台建设方案(ppt)
  18. python_torch_加载数据集_构建模型_构建训练循环_保存和调用训练好的模型
  19. Notification与Delegate实现通讯沙拉实例
  20. #附文件#《2022年期刊分区表》最新完整版已更新!

热门文章

  1. 微信小程序像抖音一样上滑视频
  2. vue全局导入外链js
  3. java烟草库存数据库设计_基于java的烟草销售管理系统的设计与实现
  4. python3用suds调用webService, 当参数是对象数组时
  5. python运算符的分类_Python运算符有哪些类型?Python学习
  6. 那些年,我们一起追的女孩
  7. idea 配置weblogic
  8. 用c语言写一个简易万年历
  9. python获取url返回值_python获取url的返回信息方法
  10. 电子器件激发出的想象力