ubuntu16 zabbix 相關配置
#! /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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Git 相關配置Git
- nginx配置相關Nginx
- JDBC 相關配置JDBC
- spring相關配置Spring
- ubuntu中Django相關配置UbuntuDjango
- php-fpm相關配置PHP
- vue-router相關配置Vue
- web.xml相關配置WebXML
- WebDAV 配置及相關工具Web
- zabbix中文配置指南
- redhat 6.2 配置 zabbixRedhat
- VScode配置xdebug相關配置VSCode
- 超順 | Ubuntu16 安裝配置aws gpuUbuntuGPU
- zabbix-agent主動模式zabbix-proxy配置模式
- Mariadb之日誌相關配置
- mac iPhone郵箱相關配置MaciPhone
- Springmvc相關配置總結SpringMVC
- SpringMvc相關配置的作用SpringMVC
- [LNMP]php-fpm相關配置LNMPPHP
- Solaris 10 網路卡相關配置
- solaris 10的ftp相關配置FTP
- Spark的相關引數配置Spark
- 語義搜尋相關配置
- zabbix的安裝配置
- zabbix-server端配置Server
- webpack4-05-配置vue相關WebVue
- Git命令的使用和相關配置Git
- spring boot配置檔案相關Spring Boot
- [網路配置相關]——netstat命令
- 高校郵件系統配置相關
- SpringBoot--MVC相關配置Spring BootMVC
- oracle817歸檔配置相關---Oracle
- zabbix 安裝配置介紹
- 安裝配置zabbix-serverServer
- Ubuntu安裝zabbix配置教程Ubuntu
- 4.2.10.1 Oracle Restart 相關變數配置OracleREST變數
- mac下安裝nginx及相關配置MacNginx
- Linux-靜態路由相關配置Linux路由