-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailer.py
92 lines (77 loc) · 2.82 KB
/
Emailer.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
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
# coding: utf-8
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
import csv
import time
import datetime
import logging
from retry import retry
import yaml
import os
import itertools
import base64
import inflection
import csv, codecs, cStringIO
from ConfigUtils import *
class Emailer():
'''
util class to email stuff to people.
'''
def __init__(self, configItems):
self._config_dir = configItems['config_dir']
self._email_config_file = configItems['email_config_file']
self._emailConfigs = ConfigUtils.setConfigs(self._config_dir, self._email_config_file)
self._server = None
self._server_port = None
self._sender = None
self._password = None
self._bcc = None
self.setConfigs()
self._recipients = self.getRecipients(self._emailConfigs)
@staticmethod
def getRecipients(emailConfigs):
if 'recipients'in emailConfigs.keys():
return emailConfigs['recipients']
return None
def setConfigs(self):
self._server = self._emailConfigs['server_addr']
self._server_port = self._emailConfigs['server_port']
self._sender = self._emailConfigs['sender_addr']
self._bcc = self._emailConfigs['bcc']
if 'sender_password' in self._emailConfigs.keys():
self._password = base64.b64decode(self._emailConfigs['sender_password'])
def sendEmails(self, subject_line, msgBody, fname_attachment=None, fname_attachment_fullpath=None, recipients=None):
fromaddr = self._sender
if(not(recipients)):
recipients = self._recipients
toaddr = recipients
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = recipients
msg['Subject'] = subject_line
msg['Bcc'] = self._bcc
body = msgBody
msg.attach(MIMEText(body, 'html'))
#Optional Email Attachment:
if(not(fname_attachment is None and fname_attachment_fullpath is None)):
filename = fname_attachment
attachment = open(fname_attachment_fullpath, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
#normal emails, no attachment
server = smtplib.SMTP(self._server, self._server_port)
##comment these lines out when using the sfgov email server
#server.starttls()
#server.login(fromaddr, self._password)
######
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
if __name__ == "__main__":
main()