envoie les emails via les templates jinja2 et données csv

This commit is contained in:
François Poulain 2020-08-25 08:42:17 +02:00
parent e7b20230ee
commit aa29bdc6b6

View File

@ -98,9 +98,9 @@ def read_templates(args):
return templates return templates
def read_join(args): def read_attachments(args):
r = [] r = []
for attachement in args.attachement: for attachement in args.attachement or []:
try: try:
with open(attachement, "rb") as fp: with open(attachement, "rb") as fp:
ctype, encoding = mimetypes.guess_type(attachement) ctype, encoding = mimetypes.guess_type(attachement)
@ -119,76 +119,28 @@ def read_join(args):
return r return r
def replace_values(bodytemplate, values): def send_message(templates, data, attachments, args):
body = bodytemplate dests = [data["to"]]
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)
msg = EmailMessage() msg = EmailMessage()
msg["Subject"] = subjectvalue msg["Subject"] = templates[0].render(**data).strip()
msg["From"] = fromvalue msg["From"] = data["from"]
msg["To"] = to msg["To"] = data["to"]
if replytovalue: if "reply-to" in data:
msg["Reply-To"] = replytovalue msg["Reply-To"] = data["reply-to"]
if ccvalue: if "cc" in data:
msg["Cc"] = ccvalue msg["Cc"] = data["cc"]
msg.set_content(body) 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: if attachments:
for attachment in attachments: for attachment in attachments:
msg.add_attachment(attachment[0], **attachment[1]) msg.add_attachment(attachment[0], **attachment[1])
@ -199,7 +151,7 @@ def send_message(message, to, attachments, args):
else: else:
print( print(
"Sending : %s, from %s to %s, dests : %s" "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: if not args.dry_run:
@ -217,32 +169,20 @@ if __name__ == "__main__":
# read the recipients file # read the recipients file
datas = read_recipients_datas(args) datas = read_recipients_datas(args)
# read the template of the mail # read the templates of the mail
templates = read_templates(args) templates = read_templates(args)
print(templates)
exit(0)
# optionnaly read the attachment of the mail # optionnaly read the attachment of the mail
attachment = None attachments = read_attachments(args)
if args.attachement:
attachments = read_join(args)
# counter to be able to pause each 10 mails send # counter to be able to pause each 10 mails send
counter = 0 counter = 0
# send mail to each recipient # send mail to each recipient
for set in sets: for data in datas:
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)
# send message # 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 # pause every 10 mails for 5 secs so that the MTA doesn't explode
counter = counter + 1 counter = counter + 1