ITPub博客

首页 > IT职业 > IT生活 > ubuntu16 zabbix 相关配置

ubuntu16 zabbix 相关配置

IT生活 作者:专注的阿熊 时间:2021-05-19 17:25:22 0 删除 编辑

#! /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博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2772992/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    410
  • 访问量
    276834