利用python實(shí)現(xiàn)簡(jiǎn)單的郵件發(fā)送客戶端示例
腳本過于簡(jiǎn)單,供學(xué)習(xí)和參考。主要了解一下smtplib庫(kù)的使用和超時(shí)機(jī)制的實(shí)現(xiàn)。使用signal.alarm實(shí)現(xiàn)超時(shí)機(jī)制。
#!/usr/bin/env python # -*- coding: utf-8 -*- import time import sys import logging import smtplib import socket import signal import ConfigParser from datetime import datetime from email import encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr CONF_PATH = "/etc/zabbix/alarm_email.conf" logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s]: %(message)s', filename='/var/log/zabbix/send_alarm_email.log') class EmailObject: def __init__(self,to_addr,content): self.timeout = 10 self.retry = 3 self.cp = self._parse_config() self.cpl = self._parse_config().sections() self.conf = dict(self.cp.items(self.cpl[0])) # common how to use one self.to_addr = to_addr self.content = content # get ConfigParser,for section selection def _parse_config(self): cp = ConfigParser.ConfigParser() cp.read(CONF_PATH) return cp # set base config def _conf_parse(self): self.subject = "zabbix告警" self.from_addr = self.conf["from_addr"] self.password = self.conf["password"] self.smtp_server = self.conf["smtp_server"] def _msg_parse(self): #msg = self.content.split("*") #state = "alarm" if msg[0] == "PROBLEM" else "ok" #severity = msg[1] #head_time = map(int,msg[2].split(".")) #tail_time = map(int,msg[3].split(":")) ## if not host? #event_type = "host." + msg[4] #reason = msg[5].replace("_"," ") #alarm_id = int(msg[6]) #message = msg return self.content def _change_server(self): # if len = 1 and this fun is called,means that all servers hava been tried if(len(self.cpl) > 1): self.cpl.pop(0) self.retry = 3 self.conf = dict(self.cp.items(self.cpl[0])) logging.info("Change server to {}".format(self.cpl[0])) self.send_email() else: logging.warning("No server could be used,try to config more server(now is {}) or increase the timeout [{}]!".format(self.cp.sections(),self.timeout)) exit() def send_email(self): # signal handle def handler(signum,frame): if self.retry > 0: raise AssertionError else: self._change_server() self._conf_parse() from_addr = self.from_addr password = self.password smtp_server = self.smtp_server timeout = self.timeout to_addr = self.to_addr msg = MIMEText(self.content,'plain','utf-8') msg['Subject'] = Header(self.subject, 'utf-8') msg['From'] = 'AlarmEmail'+'<'+from_addr+'>' msg['To'] = "******@******.com" try: signal.signal(signal.SIGALRM,handler) signal.alarm(timeout) server = smtplib.SMTP_SSL(smtp_server,465) server.login(from_addr, password) server.sendmail(from_addr,to_addr, msg.as_string()) logging.info("Send email successfully!From:[{}],To:[{}],Content:[{}]".format(from_addr,to_addr,self.content)) server.quit() exit() except AssertionError: self.retry -= 1 logging.info("Begin to resend email for the {}th times".format(3-self.retry)) self.send_email() except smtplib.SMTPAuthenticationError,e: logging.error("Server [{}] authentication failed".format(smtp_server)) self._change_server() ''' example: from emailtest import emailtest eb = emailtest.EmailObject("******@******.com","test content") eb.send_email() tips: increase timeout: eb.timeout = 10 increase retry times: eb.retry = 5 '''
配置文件參考如下:
[default]
from_addr = ******@******.com
password = ******
smtp_server = smtp.******.com
[163]
from_addr = ******@163.com
password = ******
smtp_server = smtp.163.com
[qq]
from_addr = ******@qq.com
password = ******
smtp_server = smtp.qq.com
以上這篇利用python實(shí)現(xiàn)簡(jiǎn)單的郵件發(fā)送客戶端示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)郵件發(fā)送功能
- python生成每日?qǐng)?bào)表數(shù)據(jù)(Excel)并郵件發(fā)送的實(shí)例
- python實(shí)現(xiàn)QQ郵箱/163郵箱的郵件發(fā)送
- python2.7實(shí)現(xiàn)郵件發(fā)送功能
- python3.4實(shí)現(xiàn)郵件發(fā)送功能
- python模塊smtplib實(shí)現(xiàn)純文本郵件發(fā)送功能
- python郵件發(fā)送smtplib使用詳解
- Python實(shí)現(xiàn)的查詢mysql數(shù)據(jù)庫(kù)并通過郵件發(fā)送信息功能
- Python實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫(kù)并把備份數(shù)據(jù)庫(kù)郵件發(fā)送
- python實(shí)現(xiàn)12306搶票及自動(dòng)郵件發(fā)送提醒付款功能
- python實(shí)現(xiàn)自動(dòng)發(fā)送郵件發(fā)送多人、群發(fā)、多附件的示例
- python定時(shí)利用QQ郵件發(fā)送天氣預(yù)報(bào)的實(shí)例
- python實(shí)現(xiàn)SMTP郵件發(fā)送功能
- python使用smtplib模塊通過gmail實(shí)現(xiàn)郵件發(fā)送的方法
- python實(shí)現(xiàn)郵件自動(dòng)發(fā)送
相關(guān)文章
python pillow庫(kù)的基礎(chǔ)使用教程
這篇文章主要介紹了python pillow庫(kù)的基礎(chǔ)使用教程,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01Python Matplotlib繪制動(dòng)畫的代碼詳解
使用matplotlib可以很容易地創(chuàng)建動(dòng)畫框架。在本文中我們就將利用Matplotlib制作幾個(gè)簡(jiǎn)單的動(dòng)畫,文中的示例代碼講講詳細(xì),感興趣的可以了解下2022-05-05Python對(duì)list列表進(jìn)行去重的幾種方法
python?列表就是我們js中的數(shù)組了,我們下文整理幾個(gè)常用的python?列表去重實(shí)現(xiàn)方法,非常的簡(jiǎn)單好用,通過代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-10-10python 實(shí)現(xiàn)圖片上傳接口開發(fā) 并生成可以訪問的圖片url
今天小編就為大家分享一篇python 實(shí)現(xiàn)圖片上傳接口開發(fā) 并生成可以訪問的圖片url,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12讓Python腳本暫停執(zhí)行的幾種方法(小結(jié))
這篇文章主要介紹了讓Python腳本暫停執(zhí)行的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07