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