python 发送邮件

[root@testnet ~]# cat  /mnt/mail/send_mail2.py
#_*_ coding:utf-8 _*_
import smtplib, sys
reload(sys)
sys.setdefaultencoding('utf8')
if len(sys.argv) != 2:
    print("Usage: python send_mail.py <your_argument>")
    sys.exit(1)

from email.mime.text import MIMEText
from email.header import Header
msg_from='test13643374810@qq.com' #发送方邮箱
passwd='FFBLKAFJSGQQUXF' #填入发送方邮箱的授权码
#msg_to='dengxw@qq.com' #收件人邮箱
to_addrs = ['shenzl@qq.com','zengyz@qq.com','duxw@qq.com']

mess_type_dict = {"mess_SparkBalanceSync":"FOUR LEVEL  ALERT:       EMR CLUSTER SPARK SERVICE:    SparkBalanceSync UNNORMAL. please check online",
                  "mess_SparkApnUserbill":"FOUR LEVEL  ALERT:       EMR CLUSTER SPARK SERVICE:    SparkApnUserbill UNNORMAL. please check online."
                  



}


def send_alert_mail(content):
    subject="EMR cluster SPARK service ALERTS" #主题
    msg = MIMEText(content)
    msg['Subject'] = subject
    msg['From'] = msg_from
    msg['To'] = Header(",".join(to_addrs)) 
    #msg['To'] = Header(to_addrs)
    try:
        s = smtplib.SMTP("smtp.163.com",25)
        s.login(msg_from, passwd)
        s.sendmail(msg_from, to_addrs, msg.as_string())
        print "发送成功"
    except  Exception as inst:
        print "发送失败:  {}".format(inst)
    finally:
        s.quit()

if __name__ == '__main__':
    argument = sys.argv[1]    
    send_alert_mail('Server 11.21.50.87 Alert: {}'.format(mess_type_dict[argument]))
[root@testnet ~]# 

参考: 【python基础知识】18.实操-使用python自动群发邮件_python 邮件群发-CSDN博客

posted @ 2017-11-19 23:58  littlevigra  阅读(290)  评论(1编辑  收藏  举报