Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sending to a list of emails

  • Loading branch information...
commit 8c850cc90199913165738a1f18a793f9b7bbef9e 1 parent e505f7c
@theduderog theduderog authored
View
20 superlance/process_state_email_monitor.py
@@ -27,6 +27,7 @@
"""
class ProcessStateEmailMonitor(ProcessStateMonitor):
+ COMMASPACE = ', '
@classmethod
def create_from_cmd_line(cls):
@@ -35,8 +36,8 @@ def create_from_cmd_line(cls):
parser = OptionParser()
parser.add_option("-i", "--interval", dest="interval", type="float", default=1.0,
help="batch interval in minutes (defaults to 1 minute)")
- parser.add_option("-t", "--toEmail", dest="to_email",
- help="destination email address")
+ parser.add_option("-t", "--toEmail", dest="to_emails",
+ help="destination email address(es) - comma separated")
parser.add_option("-f", "--fromEmail", dest="from_email",
help="source email address")
parser.add_option("-s", "--subject", dest="subject",
@@ -48,7 +49,7 @@ def create_from_cmd_line(cls):
(options, args) = parser.parse_args()
- if not options.to_email:
+ if not options.to_emails:
parser.print_help()
sys.exit(1)
if not options.from_email:
@@ -65,7 +66,7 @@ def __init__(self, **kwargs):
ProcessStateMonitor.__init__(self, **kwargs)
self.from_email = kwargs['from_email']
- self.to_email = kwargs['to_email']
+ self.to_emails = kwargs['to_emails']
self.subject = kwargs.get('subject')
self.smtp_host = kwargs.get('smtp_host', 'localhost')
self.digest_len = 76
@@ -78,6 +79,7 @@ def send_batch_notification(self):
def log_email(self, email):
email_for_log = copy.copy(email)
+ email_for_log['to'] = self.COMMASPACE.join(email['to'])
if len(email_for_log['body']) > self.digest_len:
email_for_log['body'] = '%s...' % email_for_log['body'][:self.digest_len]
self.write_stderr("Sending notification email:\nTo: %(to)s\n\
@@ -86,7 +88,7 @@ def log_email(self, email):
def get_batch_email(self):
if len(self.batchmsgs):
return {
- 'to': self.to_email,
+ 'to': self.to_emails,
'from': self.from_email,
'subject': self.subject,
'body': '\n'.join(self.get_batch_msgs()),
@@ -98,17 +100,17 @@ def send_email(self, email):
if self.subject:
msg['Subject'] = email['subject']
msg['From'] = email['from']
- msg['To'] = email['to']
+ msg['To'] = self.COMMASPACE.join(email['to'])
try:
- self.send_smtp(msg)
+ self.send_smtp(msg, email['to'])
except Exception, e:
self.write_stderr("Error sending email: %s\n" % e)
- def send_smtp(self, mimeMsg):
+ def send_smtp(self, mime_msg, to_emails):
s = smtplib.SMTP(self.smtp_host)
try:
- s.sendmail(mimeMsg['From'], [mimeMsg['To']], mimeMsg.as_string())
+ s.sendmail(mime_msg['From'], to_emails, mime_msg.as_string())
except:
s.quit()
raise
View
4 superlance/tests/crashmailbatch_test.py
@@ -4,7 +4,7 @@
class CrashMailBatchTests(unittest.TestCase):
from_email = 'testFrom@blah.com'
- to_email = 'testTo@blah.com'
+ to_emails = ('testTo@blah.com')
subject = 'Test Alert'
unexpected_err_msg = 'Process bar:foo (pid 58597) died unexpectedly'
@@ -17,7 +17,7 @@ def _make_one_mocked(self, **kwargs):
kwargs['stdout'] = StringIO()
kwargs['stderr'] = StringIO()
kwargs['from_email'] = kwargs.get('from_email', self.from_email)
- kwargs['to_email'] = kwargs.get('to_email', self.to_email)
+ kwargs['to_emails'] = kwargs.get('to_emails', self.to_emails)
kwargs['subject'] = kwargs.get('subject', self.subject)
obj = self._get_target_class()(**kwargs)
View
4 superlance/tests/fatalmailbatch_test.py
@@ -4,7 +4,7 @@
class FatalMailBatchTests(unittest.TestCase):
from_email = 'testFrom@blah.com'
- to_email = 'testTo@blah.com'
+ to_emails = ('testTo@blah.com')
subject = 'Test Alert'
unexpected_err_msg = 'Process bar:foo failed to start too many times'
@@ -17,7 +17,7 @@ def _make_one_mocked(self, **kwargs):
kwargs['stdout'] = StringIO()
kwargs['stderr'] = StringIO()
kwargs['from_email'] = kwargs.get('from_email', self.from_email)
- kwargs['to_email'] = kwargs.get('to_email', self.to_email)
+ kwargs['to_emails'] = kwargs.get('to_emails', self.to_emails)
kwargs['subject'] = kwargs.get('subject', self.subject)
obj = self._get_target_class()(**kwargs)
View
23 superlance/tests/process_state_email_monitor_test.py
@@ -8,7 +8,8 @@ class ProcessStateEmailMonitorTestException(Exception):
class ProcessStateEmailMonitorTests(unittest.TestCase):
from_email = 'testFrom@blah.com'
- to_email = 'testTo@blah.com'
+ to_emails = ('testTo@blah.com', 'testTo2@blah.com')
+ to_str = 'testTo@blah.com, testTo2@blah.com'
subject = 'Test Alert'
def _get_target_class(self):
@@ -21,7 +22,7 @@ def _make_one(self, **kwargs):
kwargs['stdout'] = StringIO()
kwargs['stderr'] = StringIO()
kwargs['from_email'] = kwargs.get('from_email', self.from_email)
- kwargs['to_email'] = kwargs.get('to_email', self.to_email)
+ kwargs['to_emails'] = kwargs.get('to_emails', self.to_emails)
kwargs['subject'] = kwargs.get('subject', self.subject)
obj = self._get_target_class()(**kwargs)
@@ -40,7 +41,7 @@ def _make_one_mock_send_smtp(self, **kwargs):
def test_send_email_ok(self):
email = {
'body': 'msg1\nmsg2',
- 'to': 'testTo@blah.com',
+ 'to': self.to_emails,
'from': 'testFrom@blah.com',
'subject': 'Test Alert',
}
@@ -51,18 +52,18 @@ def test_send_email_ok(self):
self.assertEquals(1, monitor.send_smtp.call_count)
smtpCallArgs = monitor.send_smtp.call_args[0]
mimeMsg = smtpCallArgs[0]
- self.assertEquals(email['to'], mimeMsg['To'])
+ self.assertEquals(self.to_str, mimeMsg['To'])
self.assertEquals(email['from'], mimeMsg['From'])
self.assertEquals(email['subject'], mimeMsg['Subject'])
self.assertEquals(email['body'], mimeMsg.get_payload())
- def _raiseSTMPException(self, mimeMsg):
+ def _raiseSTMPException(self, mime, to_emails):
raise ProcessStateEmailMonitorTestException('test')
def test_send_email_exception(self):
email = {
'body': 'msg1\nmsg2',
- 'to': 'testTo@blah.com',
+ 'to': self.to_emails,
'from': 'testFrom@blah.com',
'subject': 'Test Alert',
}
@@ -82,7 +83,7 @@ def test_send_batch_notification(self):
#Test that email was sent
expected = {
'body': 'msg1\nmsg2',
- 'to': 'testTo@blah.com',
+ 'to': self.to_emails,
'from': 'testFrom@blah.com',
'subject': 'Test Alert',
}
@@ -91,19 +92,19 @@ def test_send_batch_notification(self):
#Test that email was logged
self.assertEquals("""Sending notification email:
-To: testTo@blah.com
+To: %s
From: testFrom@blah.com
Subject: Test Alert
Body:
msg1
msg2
-""", monitor.stderr.getvalue())
+""" % (self.to_str), monitor.stderr.getvalue())
def test_log_email_with_body_digest(self):
bodyLen = 80
monitor = self._make_one_mock_send_email()
email = {
- 'to': 'you@fubar.com',
+ 'to': ['you@fubar.com'],
'from': 'me@fubar.com',
'subject': 'yo yo',
'body': 'a' * bodyLen,
@@ -121,7 +122,7 @@ def test_log_email_with_body_digest(self):
def test_log_email_without_body_digest(self):
monitor = self._make_one_mock_send_email()
email = {
- 'to': 'you@fubar.com',
+ 'to': ['you@fubar.com'],
'from': 'me@fubar.com',
'subject': 'yo yo',
'body': 'a' * 20,
Please sign in to comment.
Something went wrong with that request. Please try again.