通过Python发邮件脚本

cat sendmail.py


#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
import sys
from email.mime.text import MIMEText
import time
#reload(sys)
#sys.setdefaultencoding('utf8')
current_time=time.strftime('%Y-%m-%d%H:%M',time.localtime(time.time()))
mail_host='mail.163.com'
mail_user='rsc@163.com'
mail_pwd='123'
def send_email(mailto,get_sub,content):
    #msg = MIMEText( content.encode('utf8'),_subtype = 'html', _charset = 'utf8')
    msg = MIMEText(content,_subtype='plain',_charset='gb2312')
    msg['From']=mail_user
    msg['Subject']=get_sub
    #msg['To']=",".join(mailto)
    msg['To']=mailto
    try:
s=smtplib.SMTP_SSL(mail_host,465)
  s.login(mail_user,mail_pwd)
s.sendmail(mail_user,mailto,msg.as_string())
s.close()
    except Exception as e:
        print 'Exception: ', e
    title=sys.argv[2]
    cont="""
    ---------------------------------
    摘要:  %s
    ---------------------------------
    时间:  %s
    ---------------------------------
    """%(sys.argv[3],current_time)
    to_list = ['%s' %(sys.argv[1]),]
    with open('/tmp/sendmail_qs.log','ab') as f:
      f.write('%s  Send to  address:  %s Title: %s Substring: %s \n'%(current_time,sys.argv[1],title,sys.argv[3]))
if __name__ == "__main__":
    send_email(sys.argv[1], sys.argv[2], sys.argv[3])


sys.argv[1] :收件人邮箱

 sys.argv[2] 邮箱标题

, sys.argv[3] 邮件正文内容 

YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS