#!/usr/bin/python
#coding:utf-8
'''注释
added by tophorse in 2018.01.25
description:根据配置SMTP发邮件
'''
import sys
import os
import time
import datetime
import argparse
import logging  
import logging.handlers
import tophorse
import json
import smtplib
from email.mime.text import MIMEText
from email.header import Header

try:

  if  __name__=="__main__":
    #声明参数
    parser = argparse.ArgumentParser(description='发送邮件\n命令：python stmpmail.py "test1@qq.com;test2@qq.com" "title" "this is content"');
    parser.add_argument('receivers',action="store",help='接收者邮件,支持多个，格式：test@qq.com;test1@163.com');
    parser.add_argument('title',action="store",help='邮件主题,支持html格式');
    parser.add_argument('content',action="store",help='邮件内容,支持html格式');
    args = parser.parse_args();
    recs=args.receivers;
    title=args.title;
    content=args.content;
    #print recs;
    reclist=recs.split(";");
    #print reclist;
    #sys.exit(0);
    try:
      #变量
      sender="notice@qudao.com.cn";#"notice@tophorsekj.net";#str(hjson["sender"]);
      host="mail.tophorsekj.net";#str(hjson["host"]);
      user="";#str(hjson["user"]);
      pwd="";#str(hjson["pwd"]);
      #发松
      msg = MIMEText(content, "html", "utf-8");
      msg["From"] = Header(sender, "utf-8");
      msg["To"] =  Header(recs, "utf-8");
      msg["Subject"] = Header(title, 'utf-8')
      smtpObj = smtplib.SMTP(); 
      smtpObj.connect(host, 25);    # 25 为 SMTP 端口号
      #smtpObj.login(user,pwd); 
      smtpObj.sendmail(sender, reclist, msg.as_string());
      print "邮件发送成功"
      #成功跳出
    except Exception,e1:
      #如果发生错误，就继续用下个配置发送
      print str(e1);
except Exception,e:
  print str(e);