1 Star 0 Fork 0

往日的时光/server_monitor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mail.py 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
往日的时光 提交于 2019-08-23 16:48 . 初始化
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Author : y
@License : (C) Copyright 2013-2019, HJIMI
@Contact : bj-yez-magzine@163.com
@File : mail.py
@Time : 2019-07-04 11:09
@Desc :
'''
import os
import sys
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
def sendmail(to, subject, content):
try:
smtp_username = os.getenv('smtp_username')
smtp_password = os.getenv('smtp_password')
smtp_server = os.getenv('smtp_server')
msg = MIMEText(content, 'plain', 'utf-8')
msg['From'] = _format_addr(u'运维 <%s>' % smtp_username)
msg['To'] = to
msg['Subject'] = Header(subject, 'utf-8').encode()
server = smtplib.SMTP(smtp_server, 25)
# server.set_debuglevel(1)
server.login(smtp_username, smtp_password)
try:
server.sendmail(smtp_username, to.split(','), msg.as_string())
finally:
server.quit()
except Exception:
print('发送邮件出错')
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(),addr), 'utf-8')
if __name__=='__main__':
to = os.getenv('mail_to')
subject = os.getenv('mail_subject')
print('sending...')
sendmail(to, subject, 'what''s this !')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/olddays/server_monitor.git
git@gitee.com:olddays/server_monitor.git
olddays
server_monitor
server_monitor
master

搜索帮助