ubuntu16 zabbix 相關配置

專注的阿熊發表於2021-05-19

#! /usr/bin/env python

import smtplib

import sys

from email.mime.text import MIMEText

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

class SendEMail(object):

     """ 封裝傳送郵件類 """

     def __init__(self):

         self.host = "smtp.163.com"

         self.port = 465

         self.user = "q17852242605@163.com"

         self.pwd = "QXMPCRXSISTUCAEH"

         # 第一步:連線到 smtp 伺服器

         # self.smtp_s = smtplib.SMTP_SSL(host=self.host, port=self.port)

         # 第二步:登陸 smtp 伺服器

         # self.smtp_s.login(user=self.user, password=self.pwd)

     def send_text(self, to_user, subject, content):

         """

         傳送文字郵件

         :param to_user: 對方郵箱

         :param content: 郵件正文

         :param subject: 郵件主題

         :return:

         """

         # 第三步:準備郵件

         # 使用 email 構造郵件

         msg = MIMEText(content, _subtype='plain', _charset="utf8")

         # 新增發件人

         msg["From"] = self.user

         # 新增收件人

         # msg["To"] = ",".join(to_user)

         msg["To"] = to_user

         # 新增郵件主題

         msg["subject"] = subject

         # 第四步:傳送郵件

         try:

             smtp_s = smtplib.SMTP_SSL(host=self.host, port=self.port)

             smtp_s.login(user=self.user, password=self.pwd)

             smtp_s.send_message(msg, from_addr=self.user, to_addrs=to_user)

             print(1)

         except smtplib.SMTPException as e:

             print(e)

     def send_file(self, to_user, subject, content, reports_path, file_name):

         """

         傳送附件郵件

         :param to_user: 對方郵箱

         :param content: 郵件正文

         :param subject: 郵件主題

         :param reports_path: 附件路徑

         :param file_name: 傳送時附件名稱

         """

         # 讀取報告檔案中的內容

         file_content = open(reports_path, "rb").read()

         # 2. 使用 email 構造郵件

         # 1 )外匯跟單gendan5.com構造一封多元件的郵件

         msg = MIMEMultipart()

         # (2) 往多元件郵件中加入文字內容

         text_msg = MIMEText(content, _subtype='plain', _charset="utf8")

         msg.attach(text_msg)

         # (3) 往多元件郵件中加入檔案附件

         file_msg = MIMEApplication(file_content)

         file_msg.add_header('content-disposition', 'attachment', filename=file_name)

         msg.attach(file_msg)

         # 新增發件人

         msg["From"] = self.user

         # 新增收件人

         msg["To"] = to_user

         # 新增郵件主題

         msg["subject"] = subject

         # 第四步:傳送郵件

         try:

             self.smtp_s = smtplib.SMTP_SSL(host=self.host, port=self.port)

             self.smtp_s.login(user=self.user, password=self.pwd)

             self.smtp_s.send_message(msg, from_addr=self.user, to_addrs=to_user)

             print(1)

         except smtplib.SMTPException as e:

             print(e)

  if __name__ == "__main__":

     SendEMail = SendEMail()

     to_user = sys.argv[1]

     subject = sys.argv[2]

     content = sys.argv[3]

     SendEMail.send_text(to_user, subject, content)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2772992/,如需轉載,請註明出處,否則將追究法律責任。

相關文章