#!/usr/bin/env python # -*- coding:utf-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header import sys, time, subprocess, random # 第三方 SMTP 服务 mail_host="smtp.qq.com" #设置服务器,腾讯企业邮箱SMTP为smtp.exmail.qq.com userinfo_list = [{'user':'user1@qq.com','pass':'pass1'}, {'user':'user2@qq.com','pass':'pass2'}, {'user':'user3@qq.com','pass':'pass3'}] user_inst = userinfo_list[random.randint(0, len(userinfo_list)-1)] mail_user=user_inst['user'] #用户名 mail_pass=user_inst['pass'] #口令 sender = mail_user # 邮件发送者 receivers = ['xx1@qq.com', 'xx2@163.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 p = subprocess.Popen('hostname', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) hostname = p.stdout.readline().split(' ')[0] message_to = '' for i in receivers: message_to += i + ';' def print_help(): note = '''python script.py role ip vip ''' print(note) exit(1) time_stamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) if len(sys.argv) != 2: print_help() else: message_content = '%s [host:%s], %s' %(time_stamp, hostname, sys.argv[1]) subject = 'host:%s notify' %(hostname) message = MIMEText(message_content, 'plain', 'utf-8') message['From'] = Header(sender, 'utf-8') message['To'] = Header(message_to, 'utf-8') message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号 smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException as e: print("Error: 无法发送邮件") print(e)