suppression des variables globales
This commit is contained in:
parent
c147f844c5
commit
4ae4b7fe87
105
mailing.py
105
mailing.py
@ -28,74 +28,66 @@ from email.header import Header
|
|||||||
from email.message import EmailMessage
|
from email.message import EmailMessage
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
def parse_args():
|
||||||
description="""
|
parser = argparse.ArgumentParser(description="Simple mailing script.")
|
||||||
Simple mailing script.
|
ma = parser.add_argument_group(title="mandatory arguments", description=None)
|
||||||
"""
|
ma.add_argument(
|
||||||
)
|
"-t",
|
||||||
|
"--tofile",
|
||||||
ma = parser.add_argument_group(title="mandatory arguments", description=None)
|
metavar="TO.FILE",
|
||||||
ma.add_argument(
|
type=str,
|
||||||
"-t",
|
required=True,
|
||||||
"--tofile",
|
help="Sort of CSV file containing addresses of recipients.",
|
||||||
metavar="TO.FILE",
|
)
|
||||||
type=str,
|
ma.add_argument(
|
||||||
required=True,
|
"-b",
|
||||||
help="Sort of CSV file containing addresses of recipients.",
|
"--bodyfile",
|
||||||
)
|
metavar="BODY.FILE",
|
||||||
ma.add_argument(
|
type=str,
|
||||||
"-b",
|
required=True,
|
||||||
"--bodyfile",
|
help="Template of the mail to be sent",
|
||||||
metavar="BODY.FILE",
|
)
|
||||||
type=str,
|
parser.add_argument(
|
||||||
required=True,
|
"-d",
|
||||||
help="Template of the mail to be sent",
|
"--dry-run",
|
||||||
)
|
action="store_true",
|
||||||
|
help="Load data but don't send anything.",
|
||||||
parser.add_argument(
|
)
|
||||||
"-d", "--dry-run", action="store_true", help="Load data but don't send anything.",
|
parser.add_argument(
|
||||||
)
|
"-a",
|
||||||
|
"--attachement",
|
||||||
|
metavar="ATTACHED.FILE",
|
||||||
parser.add_argument(
|
type=str,
|
||||||
"-a",
|
nargs="?",
|
||||||
"--attachement",
|
help="Optionnal attachment.",
|
||||||
metavar="ATTACHED.FILE",
|
)
|
||||||
type=str,
|
return parser.parse_args()
|
||||||
nargs="?",
|
|
||||||
help="Optionnal attachment.",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
TOFILE = None
|
|
||||||
BODYFILE = None
|
|
||||||
JOINFILE = None
|
|
||||||
|
|
||||||
|
|
||||||
# read the recipients file where values are separated by | characters
|
# read the recipients file where values are separated by | characters
|
||||||
def read_recipients():
|
def read_recipients(args):
|
||||||
recipientfile = open(TOFILE)
|
recipientfile = open(args.tofile)
|
||||||
lines = recipientfile.readlines()
|
lines = recipientfile.readlines()
|
||||||
return [line[:-1].split("|") for line in lines]
|
return [line[:-1].split("|") for line in lines]
|
||||||
|
|
||||||
|
|
||||||
def read_body():
|
def read_body(args):
|
||||||
bodyfile = open(BODYFILE)
|
bodyfile = open(args.bodyfile)
|
||||||
body = ""
|
body = ""
|
||||||
for line in bodyfile.readlines():
|
for line in bodyfile.readlines():
|
||||||
body = body + line
|
body = body + line
|
||||||
return body
|
return body
|
||||||
|
|
||||||
|
|
||||||
def read_join():
|
def read_join(args):
|
||||||
try:
|
try:
|
||||||
with open(JOINFILE, "rb") as fp:
|
with open(args.attachement, "rb") as fp:
|
||||||
ctype, encoding = mimetypes.guess_type(JOINFILE)
|
ctype, encoding = mimetypes.guess_type(args.attachement)
|
||||||
if ctype is None or encoding is not None:
|
if ctype is None or encoding is not None:
|
||||||
ctype = "application/octet-stream"
|
ctype = "application/octet-stream"
|
||||||
maintype, subtype = ctype.split("/", 1)
|
maintype, subtype = ctype.split("/", 1)
|
||||||
metadata = {
|
metadata = {
|
||||||
"filename": JOINFILE,
|
"filename": args.attachement,
|
||||||
"maintype": maintype,
|
"maintype": maintype,
|
||||||
"subtype": subtype,
|
"subtype": subtype,
|
||||||
}
|
}
|
||||||
@ -203,21 +195,18 @@ def send_message(message, to, attachment, args):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parse_args()
|
||||||
TOFILE = args.tofile
|
|
||||||
BODYFILE = args.bodyfile
|
|
||||||
JOINFILE = args.attachement
|
|
||||||
|
|
||||||
# read the recipients file
|
# read the recipients file
|
||||||
sets = read_recipients()
|
sets = read_recipients(args)
|
||||||
|
|
||||||
# read the template of the mail
|
# read the template of the mail
|
||||||
bodytemplate = read_body()
|
bodytemplate = read_body(args)
|
||||||
|
|
||||||
# optionnaly read the attachment of the mail
|
# optionnaly read the attachment of the mail
|
||||||
attachment = None
|
attachment = None
|
||||||
if JOINFILE:
|
if args.attachement:
|
||||||
attachment = read_join()
|
attachment = 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
|
||||||
|
Loading…
Reference in New Issue
Block a user