diff --git a/mailing.py b/mailing.py index cfda686..405dfc1 100755 --- a/mailing.py +++ b/mailing.py @@ -98,9 +98,9 @@ def read_templates(args): return templates -def read_join(args): +def read_attachments(args): r = [] - for attachement in args.attachement: + for attachement in args.attachement or []: try: with open(attachement, "rb") as fp: ctype, encoding = mimetypes.guess_type(attachement) @@ -119,76 +119,28 @@ def read_join(args): return r -def replace_values(bodytemplate, values): - body = bodytemplate - - for i in range(len(values)): - i = i + 1 - pattern = "${{%02d}}" % i - - body = body.replace(pattern, values[i - 1]) - - return body - - -# patterns for header of the mail -FROMPATTERN = re.compile(r"^From: *(.*)") -SUBJECTPATTERN = re.compile(r"^Subject: *(.*)") -CCPATTERN = re.compile(r"^Cc: *(.*)") -BCCPATTERN = re.compile(r"^Bcc: *(.*)") -REPLYTOPATTERN = re.compile(r"^Reply-To: *(.*)") - - -def send_message(message, to, attachments, args): - - lines = message.split("\n") - - # identify headers in the template - fromvalue = None - subjectvalue = None - ccvalue = None - bccvalue = None - replytovalue = None - - for index, line in enumerate(lines): - if not line: - body = "\n".join(lines[index + 1 :]) - break - - frommatches = FROMPATTERN.match(line) - subjectmatches = SUBJECTPATTERN.match(line) - ccmatches = CCPATTERN.match(line) - bccmatches = BCCPATTERN.match(line) - replytomatches = REPLYTOPATTERN.match(line) - - if frommatches: - fromvalue = frommatches.group(1) - if subjectmatches: - subjectvalue = subjectmatches.group(1) - if ccmatches: - ccvalue = ccmatches.group(1) - if bccmatches: - bccvalue = bccmatches.group(1) - if replytomatches: - replytovalue = replytomatches.group(1) - - # lists all addresses to which the mail will be sent - dests = [to] - - if ccvalue: - dests.append(ccvalue) - if bccvalue: - dests.append(bccvalue) +def send_message(templates, data, attachments, args): + dests = [data["to"]] msg = EmailMessage() - msg["Subject"] = subjectvalue - msg["From"] = fromvalue - msg["To"] = to - if replytovalue: - msg["Reply-To"] = replytovalue - if ccvalue: - msg["Cc"] = ccvalue - msg.set_content(body) + msg["Subject"] = templates[0].render(**data).strip() + msg["From"] = data["from"] + msg["To"] = data["to"] + if "reply-to" in data: + msg["Reply-To"] = data["reply-to"] + if "cc" in data: + msg["Cc"] = data["cc"] + dests.append(data["cc"]) + if "bcc" in data: + msg["Bcc"] = data["bcc"] + dests.append(data["bcc"]) + + msg.set_content(templates[1].render(**data)) + + if len(templates) > 2: + alt = templates[2].render(**data) + msg.add_alternative(alt) + if attachments: for attachment in attachments: msg.add_attachment(attachment[0], **attachment[1]) @@ -199,7 +151,7 @@ def send_message(message, to, attachments, args): else: print( "Sending : %s, from %s to %s, dests : %s" - % (subjectvalue, fromvalue, dests[0], repr(dests)) + % (msg["Subject"], data["from"], dests[0], repr(dests)) ) if not args.dry_run: @@ -217,32 +169,20 @@ if __name__ == "__main__": # read the recipients file datas = read_recipients_datas(args) - # read the template of the mail + # read the templates of the mail templates = read_templates(args) - print(templates) - exit(0) # optionnaly read the attachment of the mail - attachment = None - if args.attachement: - attachments = read_join(args) + attachments = read_attachments(args) # counter to be able to pause each 10 mails send counter = 0 # send mail to each recipient - for set in sets: - - values = set - - # the recipient is always in first column - recipient = values[0] - - # replace substitution variables in the template for each recipient - body = replace_values(bodytemplate, values) + for data in datas: # send message - send_message(body, recipient, attachments, args) + send_message(templates, data, attachments, args) # pause every 10 mails for 5 secs so that the MTA doesn't explode counter = counter + 1