gpt4 book ai didi

利用python实现简单的邮件发送客户端示例

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章利用python实现简单的邮件发送客户端示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

脚本过于简单,供学习和参考。主要了解一下smtplib库的使用和超时机制的实现。使用signal.alarm实现超时机制.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/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实现简单的邮件发送客户端示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.

原文链接:http://blog.csdn.net/u012013290/article/details/78867506 。

最后此篇关于利用python实现简单的邮件发送客户端示例的文章就讲到这里了,如果你想了解更多关于利用python实现简单的邮件发送客户端示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com