2020-08-24 18:07:13 +02:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
# (C) Olivier Berger 2001-2002
|
2020-08-24 21:40:26 +02:00
|
|
|
# (C) François Poulain 2019-2020
|
2020-08-24 18:07:13 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2020-08-24 18:15:45 +02:00
|
|
|
#
|
2020-08-24 18:07:13 +02:00
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
2020-08-24 18:15:45 +02:00
|
|
|
#
|
2020-08-24 18:07:13 +02:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
# 02111-1307, USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
# This program is used to mail a message to several recipients, thus
|
|
|
|
# providing customization for each recipient.
|
|
|
|
|
2020-08-24 22:36:13 +02:00
|
|
|
import argparse, csv, sys
|
2020-08-24 18:07:13 +02:00
|
|
|
import smtplib, time, mimetypes
|
|
|
|
from email.message import EmailMessage
|
2020-08-24 21:06:21 +02:00
|
|
|
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-24 18:34:32 +02:00
|
|
|
|
2020-08-24 19:05:59 +02:00
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(description="Simple mailing script.")
|
|
|
|
ma = parser.add_argument_group(title="mandatory arguments", description=None)
|
|
|
|
ma.add_argument(
|
|
|
|
"-t",
|
|
|
|
"--tofile",
|
|
|
|
type=str,
|
|
|
|
required=True,
|
2020-08-24 21:05:22 +02:00
|
|
|
help="CSV file containing recipients data.",
|
2020-08-24 19:05:59 +02:00
|
|
|
)
|
|
|
|
ma.add_argument(
|
|
|
|
"-b",
|
2020-08-24 21:06:21 +02:00
|
|
|
"--basename",
|
2020-08-24 19:05:59 +02:00
|
|
|
type=str,
|
|
|
|
required=True,
|
2020-08-24 21:06:21 +02:00
|
|
|
help="Templates basename to be used. The known extensions are: "
|
|
|
|
".subject for subject, .txt for plain body "
|
|
|
|
"and optionally .html for html alternative.",
|
2020-08-24 19:05:59 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--dry-run",
|
|
|
|
action="store_true",
|
|
|
|
help="Load data but don't send anything.",
|
|
|
|
)
|
2020-08-24 20:02:27 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"-v", "--verbose", action="store_true",
|
|
|
|
)
|
2020-08-25 09:26:21 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"-vv", "--very-verbose", action="store_true",
|
|
|
|
)
|
2020-08-24 19:05:59 +02:00
|
|
|
parser.add_argument(
|
2020-08-24 20:03:54 +02:00
|
|
|
"-a", "--attachement", type=str, nargs="+", help="Optionnal attachments.",
|
2020-08-24 19:05:59 +02:00
|
|
|
)
|
|
|
|
return parser.parse_args()
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
|
2020-08-24 21:05:22 +02:00
|
|
|
def read_recipients_datas(args):
|
|
|
|
with open(args.tofile) as csv_file:
|
|
|
|
csv_soup = csv.reader(csv_file)
|
|
|
|
first = [(s.split(":", 1)) for s in csv_soup.__next__()]
|
|
|
|
header = [
|
|
|
|
(h[0].strip().lower(), h[1].strip() if len(h) == 2 else None) for h in first
|
|
|
|
]
|
|
|
|
datas = [[s.strip() for s in d] for d in csv_soup]
|
|
|
|
full_datas = []
|
|
|
|
for data in datas:
|
|
|
|
try:
|
|
|
|
full_data = {
|
|
|
|
h[0]: data[i] if i < len(data) or not h[1] else h[1]
|
|
|
|
for i, h in enumerate(header)
|
|
|
|
}
|
|
|
|
full_datas.append(full_data)
|
|
|
|
except Exception:
|
|
|
|
print("Ligne mal renseignée:", data, file=sys.stderr)
|
|
|
|
return full_datas
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
|
2020-08-24 21:06:21 +02:00
|
|
|
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
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
|
2020-08-25 08:42:17 +02:00
|
|
|
def read_attachments(args):
|
2020-08-24 20:03:54 +02:00
|
|
|
r = []
|
2020-08-25 08:42:17 +02:00
|
|
|
for attachement in args.attachement or []:
|
2020-08-24 20:03:54 +02:00
|
|
|
try:
|
|
|
|
with open(attachement, "rb") as fp:
|
|
|
|
ctype, encoding = mimetypes.guess_type(attachement)
|
|
|
|
if ctype is None or encoding is not None:
|
|
|
|
ctype = "application/octet-stream"
|
|
|
|
maintype, subtype = ctype.split("/", 1)
|
|
|
|
metadata = {
|
|
|
|
"filename": attachement,
|
|
|
|
"maintype": maintype,
|
|
|
|
"subtype": subtype,
|
|
|
|
}
|
|
|
|
r.append((fp.read(), metadata))
|
|
|
|
except Exception as e:
|
|
|
|
print("read error: %s" % e)
|
|
|
|
exit(1)
|
|
|
|
return r
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
|
2020-08-25 08:42:17 +02:00
|
|
|
def send_message(templates, data, attachments, args):
|
|
|
|
dests = [data["to"]]
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
msg = EmailMessage()
|
2020-08-25 08:42:17 +02:00
|
|
|
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)
|
|
|
|
|
2020-08-24 20:03:54 +02:00
|
|
|
if attachments:
|
|
|
|
for attachment in attachments:
|
|
|
|
msg.add_attachment(attachment[0], **attachment[1])
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-24 20:02:27 +02:00
|
|
|
if args.verbose:
|
|
|
|
print(msg)
|
|
|
|
print("-" * 80)
|
|
|
|
else:
|
|
|
|
print(
|
|
|
|
"Sending : %s, from %s to %s, dests : %s"
|
2020-08-25 08:42:17 +02:00
|
|
|
% (msg["Subject"], data["from"], dests[0], repr(dests))
|
2020-08-24 20:02:27 +02:00
|
|
|
)
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-24 18:56:58 +02:00
|
|
|
if not args.dry_run:
|
|
|
|
# sending the mail to its recipients using the local mailer via SMTP
|
|
|
|
server = smtplib.SMTP("localhost", "25")
|
2020-08-25 09:26:21 +02:00
|
|
|
if args.very_verbose:
|
|
|
|
server.set_debuglevel(1)
|
2020-08-24 18:56:58 +02:00
|
|
|
server.send_message(msg)
|
|
|
|
server.quit()
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
if __name__ == "__main__":
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-24 19:05:59 +02:00
|
|
|
args = parse_args()
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
# read the recipients file
|
2020-08-24 21:05:22 +02:00
|
|
|
datas = read_recipients_datas(args)
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-25 08:42:17 +02:00
|
|
|
# read the templates of the mail
|
2020-08-24 21:06:21 +02:00
|
|
|
templates = read_templates(args)
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
# optionnaly read the attachment of the mail
|
2020-08-25 08:42:17 +02:00
|
|
|
attachments = read_attachments(args)
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
# counter to be able to pause each 10 mails send
|
|
|
|
counter = 0
|
|
|
|
|
|
|
|
# send mail to each recipient
|
2020-08-25 08:42:17 +02:00
|
|
|
for data in datas:
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
# send message
|
2020-08-25 08:42:17 +02:00
|
|
|
send_message(templates, data, attachments, args)
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
# pause every 10 mails for 5 secs so that the MTA doesn't explode
|
|
|
|
counter = counter + 1
|
2020-08-24 18:15:45 +02:00
|
|
|
if counter >= 10:
|
2020-08-24 18:07:13 +02:00
|
|
|
counter = 0
|
|
|
|
print("suspending execution for 5 secs")
|
|
|
|
time.sleep(5)
|