diff --git a/README.md b/README.md index e4962a6..c288d0a 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ On a Debian-based host running at least Debian Stretch, you will need the following packages: - git (recommended for getting the source) - python3 +- python3-jinja2 ### Manual installation diff --git a/mailing.py b/mailing.py index e913c52..cc463af 100755 --- a/mailing.py +++ b/mailing.py @@ -26,6 +26,7 @@ import argparse, csv, sys, re import smtplib, time, mimetypes from email.header import Header from email.message import EmailMessage +from jinja2 import Environment, FileSystemLoader, select_autoescape def parse_args(): @@ -40,11 +41,12 @@ def parse_args(): ) ma.add_argument( "-b", - "--bodyfile", - metavar="BODY.FILE", + "--basename", type=str, required=True, - help="Template of the mail to be sent", + help="Templates basename to be used. The known extensions are: " + ".subject for subject, .txt for plain body " + "and optionally .html for html alternative.", ) parser.add_argument( "-d", @@ -82,12 +84,19 @@ def read_recipients_datas(args): return full_datas -def read_body(args): - bodyfile = open(args.bodyfile) - body = "" - for line in bodyfile.readlines(): - body = body + line - return body +def read_templates(args): + env = Environment( + loader=FileSystemLoader("."), autoescape=select_autoescape(["html"]) + ) + templates = [ + env.get_template("{}.subject".format(args.basename)), + env.get_template("{}.txt".format(args.basename)), + ] + try: + templates.append(env.get_template("{}.html".format(args.basename))) + except Exception: + pass + return templates def read_join(args): @@ -218,11 +227,11 @@ if __name__ == "__main__": # read the recipients file datas = read_recipients_datas(args) - print(datas) - exit(0) # read the template of the mail - bodytemplate = read_body(args) + templates = read_templates(args) + print(templates) + exit(0) # optionnaly read the attachment of the mail attachment = None