python 邮件脚本
Posted 牛牛VS妞妞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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_show
try:
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)
以上是关于python 邮件脚本的主要内容,如果未能解决你的问题,请参考以下文章