通过python操作smtplib模块发送邮件

# gconf.py

SMTP_SERVER_HOST='smtp.exmail.qq.com'
SMTP_SERVER_PORT=25
SMTP_USER='jack@qq.com'
# 邮箱客户端专用密码
SMTP_PWD='pass'

# 接收邮件
ALARM_RECIVE = ['admin@163.com']


发送邮件代码

mailutils.py

#encoding=utf-8

import smtplib
from email.mime.text import MIMEText
from datetime import datetime

from gconf import SMTP_SERVER_HOST, SMTP_SERVER_PORT, SMTP_USER, SMTP_PWD

def sendemail(to_list, title, content):
    _server = smtplib.SMTP(SMTP_SERVER_HOST, SMTP_SERVER_PORT)
    _server.set_debuglevel(True)
    _server.ehlo()
    _server.login(SMTP_USER, SMTP_PWD)
    _msg = MIMEText(content, 'html', 'utf-8')
    _msg['Subject'] = title
    _msg['To'] = ';'.join(to_list)
    _msg['From'] = 'cmdb系统监控管理员<%s>' % SMTP_USER
    _msg['Date'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    _server.sendmail(SMTP_USER, to_list, _msg.as_string())
    _server.quit()

if __name__ == '__main__':
    sendemail(['admin@163.com'], '监控邮件01', 'cpu内存报警邮件01')

 

posted @ 2017-11-23 13:43  reblue520  阅读(164)  评论(0编辑  收藏  举报