2020-08-24 18:07:13 +02:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
# (C) Olivier Berger 2001-2002
|
|
|
|
# (C) François Poulain 2019
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import sys, getopt, re
|
|
|
|
import smtplib, time, mimetypes
|
|
|
|
from email.header import Header
|
|
|
|
from email.message import EmailMessage
|
|
|
|
|
|
|
|
#
|
|
|
|
# displays usage of the program, then quit
|
|
|
|
#
|
|
|
|
def usage(returncode):
|
2020-08-24 18:15:45 +02:00
|
|
|
print(
|
|
|
|
"""SYNTAX:
|
|
|
|
""",
|
|
|
|
sys.argv[0],
|
|
|
|
""" -t tofile -b bodyfile [-p attachedfile]
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
tofile : sort of CSV file containing addresses of recipients
|
|
|
|
bodyfile : template of the mail to be sent
|
|
|
|
attachedfile : optionnal attachment
|
2020-08-24 18:15:45 +02:00
|
|
|
""",
|
|
|
|
)
|
2020-08-24 18:07:13 +02:00
|
|
|
sys.exit(returncode)
|
|
|
|
|
|
|
|
|
|
|
|
TOFILE = None
|
|
|
|
BODYFILE = None
|
|
|
|
JOINFILE = None
|
|
|
|
|
|
|
|
|
|
|
|
# read the recipients file where values are separated by | characters
|
2020-08-24 18:15:45 +02:00
|
|
|
def read_recipients():
|
2020-08-24 18:07:13 +02:00
|
|
|
recipientfile = open(TOFILE)
|
|
|
|
lines = recipientfile.readlines()
|
2020-08-24 18:15:45 +02:00
|
|
|
return [line[:-1].split("|") for line in lines]
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
def read_body():
|
2020-08-24 18:07:13 +02:00
|
|
|
bodyfile = open(BODYFILE)
|
|
|
|
body = ""
|
2020-08-24 18:15:45 +02:00
|
|
|
for line in bodyfile.readlines():
|
2020-08-24 18:07:13 +02:00
|
|
|
body = body + line
|
|
|
|
return body
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
|
|
|
|
def read_join():
|
2020-08-24 18:07:13 +02:00
|
|
|
try:
|
2020-08-24 18:15:45 +02:00
|
|
|
with open(JOINFILE, "rb") as fp:
|
2020-08-24 18:07:13 +02:00
|
|
|
ctype, encoding = mimetypes.guess_type(JOINFILE)
|
|
|
|
if ctype is None or encoding is not None:
|
2020-08-24 18:15:45 +02:00
|
|
|
ctype = "application/octet-stream"
|
|
|
|
maintype, subtype = ctype.split("/", 1)
|
2020-08-24 18:07:13 +02:00
|
|
|
metadata = {
|
2020-08-24 18:15:45 +02:00
|
|
|
"filename": JOINFILE,
|
|
|
|
"maintype": maintype,
|
|
|
|
"subtype": subtype,
|
|
|
|
}
|
2020-08-24 18:07:13 +02:00
|
|
|
return (fp.read(), metadata)
|
|
|
|
except Exception as e:
|
|
|
|
print("read error: %s" % e)
|
|
|
|
exit(1)
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
|
|
|
|
def replace_values(bodytemplate, values):
|
2020-08-24 18:07:13 +02:00
|
|
|
body = bodytemplate
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
for i in range(len(values)):
|
|
|
|
i = i + 1
|
2020-08-24 18:07:13 +02:00
|
|
|
pattern = "${{%02d}}" % i
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
body = body.replace(pattern, values[i - 1])
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
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: *(.*)")
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
|
|
|
|
def qencode_subject(message):
|
|
|
|
subjectmatches = re.search(r"^Subject: *(.*)$", message, re.MULTILINE)
|
|
|
|
if subjectmatches == None:
|
|
|
|
return message
|
2020-08-24 18:07:13 +02:00
|
|
|
subjectstr = subjectmatches.group(1)
|
2020-08-24 18:15:45 +02:00
|
|
|
h = Header(subjectstr, "utf-8")
|
|
|
|
qsubjectstr = h.encode()
|
|
|
|
return message.replace(subjectstr, qsubjectstr, 1)
|
|
|
|
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
def send_message(message, to, attachment):
|
2020-08-24 18:07:13 +02:00
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
lines = message.split("\n")
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
# identify headers in the template
|
|
|
|
fromvalue = None
|
|
|
|
subjectvalue = None
|
|
|
|
ccvalue = None
|
|
|
|
bccvalue = None
|
|
|
|
replytovalue = None
|
2020-08-24 18:15:45 +02:00
|
|
|
|
|
|
|
for index, line in enumerate(lines):
|
|
|
|
if not line:
|
|
|
|
body = "\n".join(lines[index + 1 :])
|
2020-08-24 18:07:13 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
frommatches = FROMPATTERN.match(line)
|
|
|
|
subjectmatches = SUBJECTPATTERN.match(line)
|
|
|
|
ccmatches = CCPATTERN.match(line)
|
|
|
|
bccmatches = BCCPATTERN.match(line)
|
|
|
|
replytomatches = REPLYTOPATTERN.match(line)
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
if frommatches:
|
2020-08-24 18:07:13 +02:00
|
|
|
fromvalue = frommatches.group(1)
|
2020-08-24 18:15:45 +02:00
|
|
|
if subjectmatches:
|
2020-08-24 18:07:13 +02:00
|
|
|
subjectvalue = subjectmatches.group(1)
|
2020-08-24 18:15:45 +02:00
|
|
|
if ccmatches:
|
2020-08-24 18:07:13 +02:00
|
|
|
ccvalue = ccmatches.group(1)
|
2020-08-24 18:15:45 +02:00
|
|
|
if bccmatches:
|
2020-08-24 18:07:13 +02:00
|
|
|
bccvalue = bccmatches.group(1)
|
2020-08-24 18:15:45 +02:00
|
|
|
if replytomatches:
|
2020-08-24 18:07:13 +02:00
|
|
|
replytovalue = replytomatches.group(1)
|
|
|
|
|
|
|
|
# lists all addresses to which the mail will be sent
|
|
|
|
dests = [to]
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
if ccvalue:
|
2020-08-24 18:07:13 +02:00
|
|
|
dests.append(ccvalue)
|
2020-08-24 18:15:45 +02:00
|
|
|
if bccvalue:
|
2020-08-24 18:07:13 +02:00
|
|
|
dests.append(bccvalue)
|
|
|
|
|
|
|
|
msg = EmailMessage()
|
2020-08-24 18:15:45 +02:00
|
|
|
msg["Subject"] = subjectvalue
|
|
|
|
msg["From"] = fromvalue
|
|
|
|
msg["To"] = to
|
2020-08-24 18:07:13 +02:00
|
|
|
if replytovalue:
|
2020-08-24 18:15:45 +02:00
|
|
|
msg["Reply-To"] = replytovalue
|
|
|
|
if ccvalue:
|
|
|
|
msg["Cc"] = ccvalue
|
2020-08-24 18:07:13 +02:00
|
|
|
msg.set_content(body)
|
|
|
|
if attachment:
|
|
|
|
msg.add_attachment(attachment[0], **attachment[1])
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
print(
|
|
|
|
"Sending : %s, from %s to %s, dests : %s"
|
|
|
|
% (subjectvalue, fromvalue, dests[0], repr(dests))
|
|
|
|
)
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
# sending the mail to its recipients using the local mailer via SMTP
|
2020-08-24 18:15:45 +02:00
|
|
|
server = smtplib.SMTP("localhost", "25")
|
2020-08-24 18:07:13 +02:00
|
|
|
server.set_debuglevel(1)
|
|
|
|
server.send_message(msg)
|
|
|
|
server.quit()
|
|
|
|
|
|
|
|
|
2020-08-24 18:15:45 +02:00
|
|
|
if __name__ == "__main__":
|
2020-08-24 18:07:13 +02:00
|
|
|
|
|
|
|
# handle options of the program
|
2020-08-24 18:15:45 +02:00
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "t:b:p:")
|
2020-08-24 18:07:13 +02:00
|
|
|
except getopt.error as msg:
|
|
|
|
print("getopt error: %s" % msg)
|
|
|
|
usage(1)
|
2020-08-24 18:15:45 +02:00
|
|
|
|
2020-08-24 18:07:13 +02:00
|
|
|
for name, value in opts:
|
2020-08-24 18:15:45 +02:00
|
|
|
if name == "-t":
|
|
|
|
TOFILE = value
|
|
|
|
elif name == "-b":
|
|
|
|
BODYFILE = value
|
|
|
|
elif name == "-p":
|
|
|
|
JOINFILE = value
|
2020-08-24 18:07:13 +02:00
|
|
|
else:
|
|
|
|
print("argument: ", name, " invalid")
|
|
|
|
usage(1)
|
|
|
|
|
|
|
|
if not TOFILE or not BODYFILE:
|
|
|
|
usage(1)
|
|
|
|
|
|
|
|
# read the recipients file
|
|
|
|
sets = read_recipients()
|
|
|
|
|
|
|
|
# read the template of the mail
|
|
|
|
bodytemplate = read_body()
|
|
|
|
|
|
|
|
# optionnaly read the attachment of the mail
|
|
|
|
attachment = None
|
|
|
|
if JOINFILE:
|
|
|
|
attachment = read_join()
|
|
|
|
|
|
|
|
# counter to be able to pause each 10 mails send
|
|
|
|
counter = 0
|
|
|
|
|
|
|
|
# send mail to each recipient
|
2020-08-24 18:15:45 +02:00
|
|
|
for set in sets:
|
|
|
|
|
2020-08-24 18:07:13 +02:00
|
|
|
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(body, recipient, attachment)
|
|
|
|
|
|
|
|
# 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)
|