python 邮件脚本

【python 邮件脚本】胸怀万里世界, 放眼无限未来。这篇文章主要讲述python 邮件脚本相关的知识,希望能为你提供帮助。

#!/usr/bin/python # -*- coding: UTF-8 -*-import smtplib import os import json import commands from smtplib import SMTP_SSL from email.mime.text import MIMEText#salt执行返回结果 def salt_result(): salt = commands.getoutput("cmd") lines = salt.split(\'\\n\') salt_new = {}for i in lines: salt_json = eval(i) num = salt_json.values() len_num = len(list(num)[0]) if len_num > 7: for key, values in salt_json.items(): salt_new[key] = str(values).split(\'\\n\')salt_res = json.dumps(salt_new) return salt_res #发送邮件 def sendMail(message,Subject,sender_show,recipient_show,to_addrs,cc_show=\'\'):user = \'用户名\' password = \'密码\' mail_host = \'smtp.exmail.qq.com\'msg = MIMEText(message, \'plain\', _charset="utf-8") msg["Subject"] = Subject msg["from"] = sender_show msg["to"] = recipient_show msg["Cc"] = cc_showtry: smtpObj = smtplib.SMTP_SSL(mail_host) smtpObj.connect(mail_host, 465) smtpObj.login(user,password) smtpObj.sendmail(user,to_addrs=to_addrs.split(\',\'), msg=msg.as_string()) smtpObj.quit() print "邮件发送成功" except smtplib.SMTPException: smtpObj.quit() print "Error: 无法发送邮件"if __name__ ==\'__main__\': message = salt_result() #print message Subject = \'主题\' sender_show = \'发送者\' recipient_show = \'接收者\' to_addrs = \'接收者\' sendMail(message,Subject,sender_show,recipient_show,to_addrs)


    推荐阅读