Skip to content
Newer
Older
100644 93 lines (76 sloc) 3.36 KB
10770b5 @markrcote Added unit tests.
markrcote authored
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 # You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 import ConfigParser
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
6 import logging
7 import socket
10770b5 @markrcote Added unit tests.
markrcote authored
8 from sendemail import sendemail
9
cd7c3c5 @bclary Bug 1161631 - Autophone - part 2 - revamp logging, r=gbrown.
bclary authored
10 # Set the logger globally in the file, but this must be reset when
11 # used in a child process.
12 logger = logging.getLogger()
10770b5 @markrcote Added unit tests.
markrcote authored
13
cd7c3c5 @bclary Bug 1161631 - Autophone - part 2 - revamp logging, r=gbrown.
bclary authored
14 class Mailer(object):
1102e03 @bclary bug 853961 - revamp logging, r=mcote.
bclary authored
15
2b6d54e @markrcote Bug 795195 - Added DISCONNECTED state; updated for DM exceptions chan…
markrcote authored
16 def __init__(self, cfgfile, subject_prefix=''):
10770b5 @markrcote Added unit tests.
markrcote authored
17 self.cfgfile = cfgfile
2b6d54e @markrcote Bug 795195 - Added DISCONNECTED state; updated for DM exceptions chan…
markrcote authored
18 self.subject_prefix = subject_prefix
c544d65 @bclary bug 1037077 - clean up mailer, part 2, r=mcote.
bclary authored
19 self.from_address = None
20 self.mail_dest = None
21 self.mail_username = None
22 self.mail_password = None
23 self.mail_server = None
24 self.mail_port = None
25 self.mail_ssl = None
10770b5 @markrcote Added unit tests.
markrcote authored
26
27 cfg = ConfigParser.ConfigParser()
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
28 if not cfg.read(self.cfgfile):
cd7c3c5 @bclary Bug 1161631 - Autophone - part 2 - revamp logging, r=gbrown.
bclary authored
29 logger.info('No email configuration file found. No emails will be sent.')
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
30 return
31
10770b5 @markrcote Added unit tests.
markrcote authored
32 try:
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
33 self.from_address = cfg.get('report', 'from')
10770b5 @markrcote Added unit tests.
markrcote authored
34 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
cd7c3c5 @bclary Bug 1161631 - Autophone - part 2 - revamp logging, r=gbrown.
bclary authored
35 logger.error('No "from" option defined in "report" section '
c544d65 @bclary bug 1037077 - clean up mailer, part 2, r=mcote.
bclary authored
36 'of file "%s".\n' % self.cfgfile)
10770b5 @markrcote Added unit tests.
markrcote authored
37 return
38
39 try:
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
40 self.mail_dest = [x.strip() for x in cfg.get('email', 'dest').split(',')]
10770b5 @markrcote Added unit tests.
markrcote authored
41 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
cd7c3c5 @bclary Bug 1161631 - Autophone - part 2 - revamp logging, r=gbrown.
bclary authored
42 logger.error('No "dest" option defined in "email" section '
c544d65 @bclary bug 1037077 - clean up mailer, part 2, r=mcote.
bclary authored
43 'of file "%s".\n' % self.cfgfile)
44 return
10770b5 @markrcote Added unit tests.
markrcote authored
45
46 try:
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
47 self.mail_username = cfg.get('email', 'username')
10770b5 @markrcote Added unit tests.
markrcote authored
48 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
49 self.mail_username = None
10770b5 @markrcote Added unit tests.
markrcote authored
50
51 try:
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
52 self.mail_password = cfg.get('email', 'password')
10770b5 @markrcote Added unit tests.
markrcote authored
53 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
54 self.mail_password = None
10770b5 @markrcote Added unit tests.
markrcote authored
55
56 try:
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
57 self.mail_server = cfg.get('email', 'server')
10770b5 @markrcote Added unit tests.
markrcote authored
58 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
514e8fe @bclary Bug 1172338 - Autophone - re-enable email notifications, r=self
bclary authored
59 self.mail_server = 'smtp.mozilla.org'
10770b5 @markrcote Added unit tests.
markrcote authored
60
61 try:
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
62 self.mail_port = cfg.getint('email', 'port')
10770b5 @markrcote Added unit tests.
markrcote authored
63 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
514e8fe @bclary Bug 1172338 - Autophone - re-enable email notifications, r=self
bclary authored
64 self.mail_port = 25
10770b5 @markrcote Added unit tests.
markrcote authored
65
66 try:
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
67 self.mail_ssl = cfg.getboolean('email', 'ssl')
10770b5 @markrcote Added unit tests.
markrcote authored
68 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
514e8fe @bclary Bug 1172338 - Autophone - re-enable email notifications, r=self
bclary authored
69 self.mail_ssl = False
10770b5 @markrcote Added unit tests.
markrcote authored
70
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
71
72 def send(self, subject, body):
c544d65 @bclary bug 1037077 - clean up mailer, part 2, r=mcote.
bclary authored
73 if not self.from_address or not self.mail_dest:
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
74 return
75
76 # encode string as ascii ignoring encoding errors
77 subject = subject.encode('ascii', errors='ignore')
78 body = body.encode('ascii', errors='ignore')
79
80 try:
81 sendemail(from_addr=self.from_address,
82 to_addrs=self.mail_dest,
83 subject='%s%s' % (self.subject_prefix, subject),
84 username=self.mail_username,
85 password=self.mail_password,
86 text_data=body,
87 server=self.mail_server,
88 port=self.mail_port,
89 use_ssl=self.mail_ssl)
90 except socket.error:
cd7c3c5 @bclary Bug 1161631 - Autophone - part 2 - revamp logging, r=gbrown.
bclary authored
91 logger.exception('Failed to send email notification: '
5d020e1 @bclary Bug 1037077 - Report run status to treeherder, r=mcote.
bclary authored
92 'subject: %s, body: %s' % (subject, body))
Something went wrong with that request. Please try again.