-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmassmail.py
More file actions
104 lines (82 loc) · 3.39 KB
/
massmail.py
File metadata and controls
104 lines (82 loc) · 3.39 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
##
## massmail.py
##
## Created on: Oct 21, 2012
## Author: martinez
##
import configparser
import re
from sendmail import send_mail
import logging
logger = logging.getLogger('pymm')
class MassMail:
"""MassMail"""
@staticmethod
def init_cfg_file(cfgfile, note=''):
"""Initialize a new config file with default values"""
config = configparser.ConfigParser()
config['DEFAULT'] = { 'username' : 'user',
'senderadress' : '[email protected]',
'mailhost' : 'host.url.com',
'port' : 25,
'subject' : 'mail subject',
'message' : 'message text {} {}',
'note' : note }
config['RECIPIENTS'] = { 'recipients': ['[email protected] fmtstr1 fmtstr2 fileattachment1 fileattachment1',
'[email protected] fmtstr1 fmtstr2 fileattachment1',
'...'] }
with open(cfgfile, 'w') as configfile:
config.write(configfile)
logger.info('Initialized a plain new configuration %s.' % cfgfile)
return
logger.error('ERROR: Something went wrong writing plain new configuration %s.'
% cfgfile)
sys.exit(1)
def __init__(self, cfgfile):
self.cfgfile = cfgfile
config = configparser.ConfigParser()
config.read(cfgfile)
username = config['DEFAULT']['username']
senderadress = config['DEFAULT']['senderadress']
mailhost = config['DEFAULT']['mailhost']
port = config['DEFAULT']['port']
subject = config['DEFAULT']['subject']
note = config['DEFAULT']['note']
# determine number of required fmtstr's in message
message = config['DEFAULT']['message']
numfmtstrs = len(re.findall('{}',message))
recipients = eval(config['RECIPIENTS']['recipients'])
logger.debug('username: {}\n'
'senderadress: {}\n'
'mailhost: {}\n'
'port: {}\n'
'subject: {}\n'
'note: {}\n'
'message: {}\n'
'numfmtstrs: {}\n'
'recipients: {}'.format(username,senderadress,mailhost,port,
subject,note,message,numfmtstrs,
recipients))
for recipient in recipients:
data = recipient.strip().split()
try:
mail_dest = data[0]
fmtstrs = data[1:numfmtstrs+1]
attachments = data[numfmtstrs+1:]
logger.debug('mail_dest: {}\n'
'fmtstrs: {}\n'
'attachments: {}'.format(mail_dest,fmtstrs,attachments))
if(len(fmtstrs) != numfmtstrs):
raise IndexError
fmtmessage = message.format(*fmtstrs)
logger.debug('fmtmessage: {}'.format(fmtmessage))
send_mail(senderadress, mail_dest, subject, fmtmessage,
files=attachments, server=mailhost, user=username,
password=None, port=port, note=note)
except IndexError as e:
logger.warning('Skipping wrongly formated data {}: {}\n{}'.format(data,fmtstrs,e))
continue
except Exception as e:
logger.warning('Error sending {}: {}'.format(data,e))
continue
logger.info('Mail to {} successfully send.'.format(mail_dest))