释放双眼,带上耳机,听听看~!
由于阿里云只能用465端口。所以这个zabbix的脚本修改成了465端口的python脚本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 1#!/usr/bin/env python
2# -*- coding: UTF-8 -*-
3import smtplib
4import traceback
5from email.mime.text import MIMEText
6from email.mime.multipart import MIMEMultipart
7import sys
8
9def sendmail(subject, msg, toaddrs, fromaddr, smtpaddr, password):
10 '''
11 @subject:邮件主题
12 @msg:邮件内容
13 @toaddrs:收信人的邮箱地址
14 @fromaddr:发信人的邮箱地址
15 @smtpaddr:smtp服务地址,可以在邮箱看,比如163邮箱为smtp.163.com
16 @password:发信人的邮箱密码
17 '''
18 mail_msg = MIMEMultipart()
19 if not isinstance(subject, unicode):
20 subject = unicode(subject, 'utf-8')
21 mail_msg['Subject'] = subject
22 mail_msg['From'] = fromaddr
23 mail_msg['To'] = ','.join(toaddrs)
24 mail_msg.attach(MIMEText(msg, 'html', 'utf-8'))
25 try:
26 s = smtplib.SMTP_SSL()
27 s.connect(smtpaddr, 465) # 连接smtp服务器
28 s.login(fromaddr, password) # 登录邮箱
29 s.sendmail(fromaddr, toaddrs, mail_msg.as_string()) # 发送邮件
30 s.quit()
31 except Exception, e:
32 print "Error: unable to send email"
33 print traceback.format_exc()
34
35if __name__ == '__main__':
36 fromaddr = "yeyu@XXXX.com"
37 smtpaddr = "smtp.XXXX.com"
38 password = "XXXX"
39
40
41 toaddrs = [sys.argv[1]]
42 subject = sys.argv[2]
43 msg = sys.argv[3]
44
45# toaddrs =sys.argv[1]
46# subject=sys.argv[2]
47# msg = sys.argv[3]
48
49
50 sendmail(subject, msg, toaddrs, fromaddr, smtpaddr, password)
51