forked from mozilla/autophone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.py
64 lines (51 loc) · 2.34 KB
/
mailer.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import logging
import ConfigParser
from sendemail import sendemail
class Mailer(object):
logger = logging.getLogger('autophone.mailer')
def __init__(self, cfgfile, subject_prefix=''):
self.cfgfile = cfgfile
self.subject_prefix = subject_prefix
def send(self, subject, body):
# encode string as ascii ignoring encoding errors
subject = subject.encode('ascii', errors='ignore')
body = body.encode('ascii', errors='ignore')
cfg = ConfigParser.ConfigParser()
cfg.read(self.cfgfile)
try:
from_address = cfg.get('report', 'from')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
self.logger.error('No "from" option defined in "report" section of file "%s".\n' % self.cfgfile)
return
try:
mail_dest = [x.strip() for x in cfg.get('email', 'dest').split(',')]
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
mail_dest = []
try:
mail_username = cfg.get('email', 'username')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
mail_username = None
try:
mail_password = cfg.get('email', 'password')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
mail_password = None
try:
mail_server = cfg.get('email', 'server')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
mail_server = 'mail.mozilla.com'
try:
mail_port = cfg.getint('email', 'port')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
mail_port = 465
try:
mail_ssl = cfg.getboolean('email', 'ssl')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
mail_ssl = True
sendemail(from_addr=from_address, to_addrs=mail_dest,
subject='%s%s' % (self.subject_prefix, subject),
username=mail_username, password=mail_password,
text_data=body, server=mail_server, port=mail_port,
use_ssl=mail_ssl)