ajoute un nombre arbitraire de PJ

This commit is contained in:
François Poulain 2020-08-24 20:03:54 +02:00
parent 166f4298f9
commit 03d2a29311

View File

@ -57,12 +57,7 @@ def parse_args():
"-v", "--verbose", action="store_true",
)
parser.add_argument(
"-a",
"--attachement",
metavar="ATTACHED.FILE",
type=str,
nargs="?",
help="Optionnal attachment.",
"-a", "--attachement", type=str, nargs="+", help="Optionnal attachments.",
)
return parser.parse_args()
@ -83,21 +78,24 @@ def read_body(args):
def read_join(args):
try:
with open(args.attachement, "rb") as fp:
ctype, encoding = mimetypes.guess_type(args.attachement)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
metadata = {
"filename": args.attachement,
"maintype": maintype,
"subtype": subtype,
}
return (fp.read(), metadata)
except Exception as e:
print("read error: %s" % e)
exit(1)
r = []
for attachement in args.attachement:
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
def replace_values(bodytemplate, values):
@ -130,7 +128,7 @@ def qencode_subject(message):
return message.replace(subjectstr, qsubjectstr, 1)
def send_message(message, to, attachment, args):
def send_message(message, to, attachments, args):
lines = message.split("\n")
@ -180,8 +178,9 @@ def send_message(message, to, attachment, args):
if ccvalue:
msg["Cc"] = ccvalue
msg.set_content(body)
if attachment:
msg.add_attachment(attachment[0], **attachment[1])
if attachments:
for attachment in attachments:
msg.add_attachment(attachment[0], **attachment[1])
if args.verbose:
print(msg)
@ -213,7 +212,7 @@ if __name__ == "__main__":
# optionnaly read the attachment of the mail
attachment = None
if args.attachement:
attachment = read_join(args)
attachments = read_join(args)
# counter to be able to pause each 10 mails send
counter = 0
@ -230,7 +229,7 @@ if __name__ == "__main__":
body = replace_values(bodytemplate, values)
# send message
send_message(body, recipient, attachment, args)
send_message(body, recipient, attachments, args)
# pause every 10 mails for 5 secs so that the MTA doesn't explode
counter = counter + 1