openrvdas.logger.writers.email_writer
No module-level documentation available.
1#!/usr/bin/env python3 2 3import getpass 4import smtplib 5import socket 6import threading 7import time 8 9from email.message import EmailMessage 10 11 12from logger.writers.writer import Writer # noqa: E402 13 14 15class EmailWriter(Writer): 16 """Send the record as an email message.""" 17 18 def __init__(self, to, sender=None, subject=None, max_freq=3 * 60, **kwargs): 19 """ 20 ``` 21 to Comma-separated list of email addresses 22 23 sender Identity of sender; default is <user>@<hostname> 24 if omitted 25 26 subject Optional custom subject line; default is start of record 27 28 max_freq maximum frequency, in seconds between messages, with which 29 to send email. Default is 3 minutes. 30 ``` 31 NOTE: Of course, you'll need to make sure your machine has SMTP 32 configured and running if you wish to send email anywhere other than 33 localhost. 34 """ 35 super().__init__(**kwargs) # processes 'quiet' and type hints 36 37 if not sender: 38 username = getpass.getuser() 39 hostname = socket.gethostname() 40 sender = username + '@' + hostname 41 self.to = to 42 self.sender = sender 43 self.subject = subject 44 self.max_freq = max_freq 45 46 self.queue = [] 47 self.queue_lock = threading.Lock() 48 self.last_send = 0 49 50 ############################ 51 def _send_email(self, sleep=0): 52 """Internal: Send record (and all previously queued but not sent) records 53 as an email message.""" 54 time.sleep(sleep) 55 56 # Grab messages from queue 57 with self.queue_lock: 58 # If someone has snatched all the messages while we slept, 59 # it's fine - just go home. 60 if not self.queue: 61 return 62 63 # Otherwise, snatch them all for ourselves 64 message = '\n'.join(self.queue) 65 self.queue = [] 66 self.last_send = time.time() 67 68 msg = EmailMessage() 69 msg.set_content(message) 70 71 msg['To'] = self.to 72 msg['From'] = self.sender 73 74 # If no subject specified, just wedge the message in, up to a 75 # newline, which is a prohibited character for header fields 76 if self.subject: 77 msg['Subject'] = self.subject 78 else: 79 if message.find('\n') > 0: 80 subject = message[:message.find('\n')] 81 else: 82 subject = message 83 msg['Subject'] = subject 84 85 # Send the message via our own SMTP server. 86 s = smtplib.SMTP('localhost') 87 s.send_message(msg) 88 s.quit() 89 90 ############################ 91 def write(self, record): 92 """Send record as email, or queue to send if have already sent recently.""" 93 if not record: 94 return 95 96 with self.queue_lock: 97 # Stash record in queue 98 self.queue.append(record) 99 100 # How long before we can send next email? 101 now = time.time() 102 time_to_sleep = max(0, self.max_freq - (now - self.last_send)) 103 104 # Start up a separate thread so we can go ahead and return while 105 # it possibly sleeps and waits. 106 threading.Thread(target=self._send_email, args=(time_to_sleep,), 107 daemon=True).start()
class
EmailWriter(logger.writers.writer.Writer):
16class EmailWriter(Writer): 17 """Send the record as an email message.""" 18 19 def __init__(self, to, sender=None, subject=None, max_freq=3 * 60, **kwargs): 20 """ 21 ``` 22 to Comma-separated list of email addresses 23 24 sender Identity of sender; default is <user>@<hostname> 25 if omitted 26 27 subject Optional custom subject line; default is start of record 28 29 max_freq maximum frequency, in seconds between messages, with which 30 to send email. Default is 3 minutes. 31 ``` 32 NOTE: Of course, you'll need to make sure your machine has SMTP 33 configured and running if you wish to send email anywhere other than 34 localhost. 35 """ 36 super().__init__(**kwargs) # processes 'quiet' and type hints 37 38 if not sender: 39 username = getpass.getuser() 40 hostname = socket.gethostname() 41 sender = username + '@' + hostname 42 self.to = to 43 self.sender = sender 44 self.subject = subject 45 self.max_freq = max_freq 46 47 self.queue = [] 48 self.queue_lock = threading.Lock() 49 self.last_send = 0 50 51 ############################ 52 def _send_email(self, sleep=0): 53 """Internal: Send record (and all previously queued but not sent) records 54 as an email message.""" 55 time.sleep(sleep) 56 57 # Grab messages from queue 58 with self.queue_lock: 59 # If someone has snatched all the messages while we slept, 60 # it's fine - just go home. 61 if not self.queue: 62 return 63 64 # Otherwise, snatch them all for ourselves 65 message = '\n'.join(self.queue) 66 self.queue = [] 67 self.last_send = time.time() 68 69 msg = EmailMessage() 70 msg.set_content(message) 71 72 msg['To'] = self.to 73 msg['From'] = self.sender 74 75 # If no subject specified, just wedge the message in, up to a 76 # newline, which is a prohibited character for header fields 77 if self.subject: 78 msg['Subject'] = self.subject 79 else: 80 if message.find('\n') > 0: 81 subject = message[:message.find('\n')] 82 else: 83 subject = message 84 msg['Subject'] = subject 85 86 # Send the message via our own SMTP server. 87 s = smtplib.SMTP('localhost') 88 s.send_message(msg) 89 s.quit() 90 91 ############################ 92 def write(self, record): 93 """Send record as email, or queue to send if have already sent recently.""" 94 if not record: 95 return 96 97 with self.queue_lock: 98 # Stash record in queue 99 self.queue.append(record) 100 101 # How long before we can send next email? 102 now = time.time() 103 time_to_sleep = max(0, self.max_freq - (now - self.last_send)) 104 105 # Start up a separate thread so we can go ahead and return while 106 # it possibly sleeps and waits. 107 threading.Thread(target=self._send_email, args=(time_to_sleep,), 108 daemon=True).start()
Send the record as an email message.
EmailWriter(to, sender=None, subject=None, max_freq=180, **kwargs)
19 def __init__(self, to, sender=None, subject=None, max_freq=3 * 60, **kwargs): 20 """ 21 ``` 22 to Comma-separated list of email addresses 23 24 sender Identity of sender; default is <user>@<hostname> 25 if omitted 26 27 subject Optional custom subject line; default is start of record 28 29 max_freq maximum frequency, in seconds between messages, with which 30 to send email. Default is 3 minutes. 31 ``` 32 NOTE: Of course, you'll need to make sure your machine has SMTP 33 configured and running if you wish to send email anywhere other than 34 localhost. 35 """ 36 super().__init__(**kwargs) # processes 'quiet' and type hints 37 38 if not sender: 39 username = getpass.getuser() 40 hostname = socket.gethostname() 41 sender = username + '@' + hostname 42 self.to = to 43 self.sender = sender 44 self.subject = subject 45 self.max_freq = max_freq 46 47 self.queue = [] 48 self.queue_lock = threading.Lock() 49 self.last_send = 0
to Comma-separated list of email addresses
sender Identity of sender; default is <user>@<hostname>
if omitted
subject Optional custom subject line; default is start of record
max_freq maximum frequency, in seconds between messages, with which
to send email. Default is 3 minutes.
NOTE: Of course, you'll need to make sure your machine has SMTP configured and running if you wish to send email anywhere other than localhost.
def
write(self, record):
92 def write(self, record): 93 """Send record as email, or queue to send if have already sent recently.""" 94 if not record: 95 return 96 97 with self.queue_lock: 98 # Stash record in queue 99 self.queue.append(record) 100 101 # How long before we can send next email? 102 now = time.time() 103 time_to_sleep = max(0, self.max_freq - (now - self.last_send)) 104 105 # Start up a separate thread so we can go ahead and return while 106 # it possibly sleeps and waits. 107 threading.Thread(target=self._send_email, args=(time_to_sleep,), 108 daemon=True).start()
Send record as email, or queue to send if have already sent recently.