Python通过smtp服务发送电子邮件给指定用户(适用于Zabbix邮件报警)

简介:

  当下免费的邮件服务很多,例如163企业邮箱、QQ企业邮箱等。不需要自己搭建邮件服务器发送邮件给指 定用户,只需要注册任何一个支持smtp协议的邮箱就可以实现发送邮件。发送邮件可以通过Linux命令、自己编写的Shell脚本,也可以通过Python写的Python脚本。

  如下代码是一个简单却实用的示例。默认无参数执行时,发送预设的邮件主题和邮件内容到预设的用户。带参数执行时将指定的主题和邮件内容发送到指定的用户。带参数执行可用于Zabbix邮件报警脚本。

  对于Zabbix2.x可以直接填写脚本名字。对于Zabbix3.x,需要指定参数,第一个是参数1,第二个是参数2,以此类推。

  如下图所示:

wKioL1dWKSaSK9pPAAJVA_4VyT4855.jpg-wh_50

 代码如下:

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
52
53
54
55
56
57
58
#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
import  smtplib
import  string
import  sys
 
 
def  usage():
     print ( """
     Function: send email to somebody using smtp protocol
 
     Usage:
         no parameters:   python %s
         with parameters: python %s <mailto> <subject> <message body>
 
     Example: python %s "sendto" "subject" "message"
""" %  (__file__, __file__, sys.argv[ 0 ])
     sys.exit( 0 )
 
 
EMAIL_HOST  =  "smtp.example.domain"               # change it
EMAIL_PORT  =  25                                  # default smtp port
EMAIL_HOST_USER  =  'noreply@example.domain'       # change it
EMAIL_HOST_PASSWORD  =  'your password'            # change it
DEFAULT_FROM_EMAIL  =  'noreply@example.domain'    # change it
CRLF  =  "\r\n"                                    # for Windows user read easily
 
EMAIL_TO  =  "example@example.domain"                                                                      # user defined variable, in Zabbix is {ALERT.SENDTO}
SUBJECT  =  "An email notification from Python"                                                            # user defined variable, in Zabbix is {ALERT.SUBJECT}
text  =  "if you saw this content, it means it works and this is default content with no parameters."      # user defined variable, in Zabbix is {ALERT.MESSAGE}
 
argc  =  len (sys.argv)
if  not  (argc  = =  1  or  argc  = =  4 ):
     print ( "Error: incorrect number of arguments or unrecognized option" )
     usage()
if  argc  = =  1 :
     pass
else :
     if  sys.argv[ 1 is  not  None  and  sys.argv[ 2 is  not  None  and  sys.argv[ 3 is  not  None :
         EMAIL_TO  =  sys.argv[ 1 ]
         SUBJECT  =  sys.argv[ 2 ]
         text  =  sys.argv[ 3 ]
 
BODY  =  string.join((
     "From: %s"  %  DEFAULT_FROM_EMAIL,
     "To: %s"  %  EMAIL_TO,
     "Subject: %s"  %  SUBJECT,
     "",
     text
), CRLF)
 
server  =  smtplib.SMTP()
server.connect(EMAIL_HOST, EMAIL_PORT)
server.starttls()
server.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
server.sendmail(DEFAULT_FROM_EMAIL, [EMAIL_TO], BODY)
server.quit()

tag:Python邮件报警,Zabbix邮件报警脚本,Python发送邮件

--end--





本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1786821,如需转载请自行联系原作者


相关文章
|
1月前
|
JSON API 数据库
解释如何在 Python 中实现 Web 服务(RESTful API)。
解释如何在 Python 中实现 Web 服务(RESTful API)。
23 0
|
3月前
|
存储 搜索推荐 数据安全/隐私保护
python实战讲解之使用Python批量发送个性化邮件
python实战讲解之使用Python批量发送个性化邮件
|
16天前
|
数据采集 Java API
python并发编程: Python使用线程池在Web服务中实现加速
python并发编程: Python使用线程池在Web服务中实现加速
17 3
python并发编程: Python使用线程池在Web服务中实现加速
|
28天前
|
存储 监控 数据库
Python语言的通用操作系统服务
Python语言的通用操作系统服务
|
1月前
|
负载均衡 Java Nacos
python flask服务如何注册到nacos
一文讲清楚python flask服务如何注册到nacos
79 2
python flask服务如何注册到nacos
|
1月前
|
安全 数据安全/隐私保护 Python
Python生成电子邮件
Python生成电子邮件
9 0
|
1月前
|
设计模式 安全 Shell
Python生成Web服务
Python生成Web服务
14 0
|
2月前
|
人工智能 机器人 API
Python和阿里云AI服务搭建
使用Python和阿里云AI服务搭建一个简单的聊天机器人的教程 1. 注册阿里云账号并登录。 2. 开通阿里云AI服务,并创建一个智能对话机器人。 3. 获取API密钥和AccessToken。 4. 安装Python环境和SDK。
249 8
|
2月前
|
存储 安全 计算机视觉
用 Python 脚本实现电脑唤醒后自动拍照 截屏并发邮件通知
用 Python 脚本实现电脑唤醒后自动拍照 截屏并发邮件通知
|
3月前
|
存储 Shell API
Python 自动化指南(繁琐工作自动化)第二版:十八、发送电子邮件和短信
Python 自动化指南(繁琐工作自动化)第二版:十八、发送电子邮件和短信

推荐镜像

更多