8
8
import sys
9
9
from tempfile import NamedTemporaryFile
10
10
import time
11
- from typing import Any , Generator , Optional
11
+ from typing import Any , Iterable , Optional , Dict , List
12
12
import zipfile
13
13
from base64 import b64decode
14
14
from collections import OrderedDict
@@ -50,13 +50,13 @@ class Mail:
50
50
to : str
51
51
path : str
52
52
count : int
53
- template_data : dict [str ,Any ]
53
+ template_data : Dict [str , Any ]
54
54
55
55
56
56
class SMTPBatchOutputBot (Bot ):
57
57
# configurable parameters
58
58
additional_grouping_keys : Optional [list ] = [] # refers to the event directly
59
- templating : Optional [dict [str , bool ]] = {'subject' : False , 'body' : False , 'attachment' : False }
59
+ templating : Optional [Dict [str , bool ]] = {'subject' : False , 'body' : False , 'attachment' : False }
60
60
alternative_mails : Optional [str ] = None
61
61
bcc : Optional [list ] = None
62
62
email_from : str = ""
@@ -126,8 +126,6 @@ def init(self):
126
126
raise MissingDependencyError ('envelope' , '>=2.0.0' )
127
127
if jinja2 is None :
128
128
self .logger .warning ("No jinja2 installed. Thus, the templating is deactivated." )
129
- if self .additional_grouping_keys and len (self .additional_grouping_keys ) > 0 :
130
- self .additional_grouping_keys_trans = {self .fieldnames_translation .get (k , k ) for k in self .additional_grouping_keys }
131
129
self .set_cache ()
132
130
self .key = f"{ self ._Bot__bot_id } :"
133
131
@@ -251,7 +249,7 @@ def set_tester(self, force=True):
251
249
print ("\n What e-mail should I use?" )
252
250
self .testing_to = input ()
253
251
254
- def send_mails_to_tester (self , mails ):
252
+ def send_mails_to_tester (self , mails : List [ Mail ] ):
255
253
"""
256
254
These mails are going to tester's address. Then prints out their count.
257
255
:param mails: list
@@ -260,7 +258,7 @@ def send_mails_to_tester(self, mails):
260
258
count = sum ([1 for mail in mails if self .build_mail (mail , send = True , override_to = self .testing_to )])
261
259
print (f"{ count } × mail sent to: { self .testing_to } \n " )
262
260
263
- def prepare_mails (self ) -> Generator [Mail ]:
261
+ def prepare_mails (self ) -> Iterable [Mail ]:
264
262
""" Generates Mail objects """
265
263
266
264
for mail_record in self .cache .redis .keys (f"{ self .key } *" )[slice (self .limit_results )]:
@@ -332,14 +330,16 @@ def prepare_mails(self) -> Generator[Mail]:
332
330
333
331
# collect all data which must be the same for all events of the
334
332
# bucket and thus can be used for templating
335
- template_data = {}
336
- # only collect if templating is enabled (save the memory otherwise)
333
+ template_keys = [ 'source.abuse_contact' ]
334
+ # only collect if templating is enabled (save the memory otherwise)+
337
335
if jinja2 and self .templating and any (self .templating .values ()):
338
- template_data = {
339
- k .replace ("." , "_" ): lines [0 ][k ]
340
- for k in ["source.abuse_contact" ] + self .additional_grouping_keys
341
- if k in rows_output [0 ]
342
- }
336
+ template_keys .extend (self .additional_grouping_keys )
337
+
338
+ template_data = {
339
+ k .replace ("." , "_" ): lines [0 ][k ]
340
+ for k in template_keys
341
+ if k in lines [0 ]
342
+ }
343
343
344
344
email_to = template_data ["source_abuse_contact" ]
345
345
filename = f'{ time .strftime ("%y%m%d" )} _{ count } _events'
@@ -361,7 +361,7 @@ def prepare_mails(self) -> Generator[Mail]:
361
361
self .build_mail (mail , send = False )
362
362
yield mail
363
363
364
- def build_mail (self , mail , send = False , override_to = None ):
364
+ def build_mail (self , mail : Mail , send = False , override_to = None ):
365
365
""" creates a MIME message
366
366
:param mail: Mail object
367
367
:param send: True to send through SMTP, False for just printing the information
@@ -377,6 +377,8 @@ def build_mail(self, mail, send=False, override_to=None):
377
377
email_to = mail .to
378
378
email_from = self .email_from
379
379
380
+ template_data = mail .template_data
381
+
380
382
text = self .mail_contents
381
383
if jinja2 and self .templating and self .templating .get ('body' , False ):
382
384
jinja_tmpl = jinja_env .from_string (text )
0 commit comments