外部参考Python

时间:2018-04-05 13:46:08

标签: python

我正在使用python脚本,当我的多个服务中的一个服务器关闭时,它将运行。它使用监控系统中的几个外部参数,例如{HOST.NAME},这些参数将被转换为' Ubuntu 1'例如。

因为我打算同时发送明文和HTML电子邮件,所以需要在脚本本身中进行配置。我对Python特别陌生,所以我不确定如何重新引用这些对象。电子邮件本身有效,但给定的参数无法正确读取。这就是我到目前为止所拥有的。

    #!/usr/bin/env python

list1=[0, 1, 2, 3, 4];

import mimetypes, os, smtplib, sys
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from email.utils import make_msgid


me = "xx@yy.com"
you = "yy@xx.com"
DESTINATION=sys.argv[1]
SUBJECT=sys.argv[2]
MESSAGE=sys.argv[3]
PASS='xxyyxx'
SRVSMTP='somesmtpserver:587'

msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
msg['Date'] = formatdate()
msg['Message-ID'] = make_msgid()

hostnam= '{HOST.NAME}'
trgname= '{TRIGGER.NAME}'
trgstat= '{TRIGGER.STATUS}'
trgseve= '{TRIGGER.SEVERITY}'

text = """A problem has been detected on 'hostnam' \n \n Trigger: 'trgname' \n Status: 'trgstat' \n Severity: 'trgseve' \n \n Om een vervangende bestelling te plaatsen voor 'hostnam' , wordt u verzocht om een e-mail verzenden met daar in de naam van het product naar het e-mail adres EMAIL2@LIAME.nl \n
Dit bericht is automatisch gegenereerd door het Zabbix Notification system.\n Er wordt niet gereageerd op dit e-mail adres. \n
"""
html = """
<html>
<head>
        <title></title>
</head>
<body>
<p><img alt="Related image" src="imagehere" /></p>

<p style="margin-left: 40px;">Er is een probleem opgetreden bij<strong>&nbsp; 'hostnam'</strong>&nbsp;.</p>

<p style="margin-left: 80px;"><strong>Trigger:</strong> 'trgname'<br />
<strong>Status</strong>: 'trgstat' <br />
<strong>Severity:</strong> 'trgseve' |</p>

<p style="margin-left: 40px;"><a href="mailto:xxyyxx@aa.com">Klik hier</a> om een vervangende bestelling te plaatsen voor de&nbsp;<strong>'hostnam'.</strong><br />
&nbsp;</p>


<p style="margin-left: 40px;"><span style="font-size:9px;">Dit bericht is automatisch gegenereerd, er zal op dit e-mail adres niet worden gereageerd.<br />
&nbsp;</p>
</body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
smtpserver = smtplib.SMTP(SRVSMTP)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(me, PASS)


# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
smtpserver.sendmail(me, you, msg.as_string())
smtpserver.quit()

0 个答案:

没有答案