refactor le style avec black
This commit is contained in:
parent
f2dcc790da
commit
1064e517be
135
mailing.py
135
mailing.py
@ -7,12 +7,12 @@
|
|||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation; either version 2 of the License, or
|
# the Free Software Foundation; either version 2 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# This program is distributed in the hope that it will be useful, but
|
# This program is distributed in the hope that it will be useful, but
|
||||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
# General Public License for more details.
|
# General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program; if not, write to the Free Software
|
# along with this program; if not, write to the Free Software
|
||||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
||||||
@ -31,13 +31,17 @@ from email.message import EmailMessage
|
|||||||
# displays usage of the program, then quit
|
# displays usage of the program, then quit
|
||||||
#
|
#
|
||||||
def usage(returncode):
|
def usage(returncode):
|
||||||
print("""SYNTAX:
|
print(
|
||||||
""", sys.argv[0], """ -t tofile -b bodyfile [-p attachedfile]
|
"""SYNTAX:
|
||||||
|
""",
|
||||||
|
sys.argv[0],
|
||||||
|
""" -t tofile -b bodyfile [-p attachedfile]
|
||||||
|
|
||||||
tofile : sort of CSV file containing addresses of recipients
|
tofile : sort of CSV file containing addresses of recipients
|
||||||
bodyfile : template of the mail to be sent
|
bodyfile : template of the mail to be sent
|
||||||
attachedfile : optionnal attachment
|
attachedfile : optionnal attachment
|
||||||
""")
|
""",
|
||||||
|
)
|
||||||
sys.exit(returncode)
|
sys.exit(returncode)
|
||||||
|
|
||||||
|
|
||||||
@ -47,44 +51,46 @@ 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():
|
||||||
recipientfile = open(TOFILE)
|
recipientfile = open(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():
|
||||||
bodyfile = open(BODYFILE)
|
bodyfile = open(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():
|
||||||
try:
|
try:
|
||||||
with open(JOINFILE, 'rb') as fp:
|
with open(JOINFILE, "rb") as fp:
|
||||||
ctype, encoding = mimetypes.guess_type(JOINFILE)
|
ctype, encoding = mimetypes.guess_type(JOINFILE)
|
||||||
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": JOINFILE,
|
||||||
'maintype': maintype,
|
"maintype": maintype,
|
||||||
'subtype': subtype,
|
"subtype": subtype,
|
||||||
}
|
}
|
||||||
return (fp.read(), metadata)
|
return (fp.read(), metadata)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("read error: %s" % e)
|
print("read error: %s" % e)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
def replace_values(bodytemplate, values) :
|
|
||||||
|
def replace_values(bodytemplate, values):
|
||||||
body = bodytemplate
|
body = bodytemplate
|
||||||
|
|
||||||
for i in range(len(values)) :
|
for i in range(len(values)):
|
||||||
i = i+1
|
i = i + 1
|
||||||
pattern = "${{%02d}}" % i
|
pattern = "${{%02d}}" % i
|
||||||
|
|
||||||
body = body.replace(pattern, values[i-1])
|
body = body.replace(pattern, values[i - 1])
|
||||||
|
|
||||||
return body
|
return body
|
||||||
|
|
||||||
@ -96,17 +102,20 @@ CCPATTERN = re.compile(r"^Cc: *(.*)")
|
|||||||
BCCPATTERN = re.compile(r"^Bcc: *(.*)")
|
BCCPATTERN = re.compile(r"^Bcc: *(.*)")
|
||||||
REPLYTOPATTERN = re.compile(r"^Reply-To: *(.*)")
|
REPLYTOPATTERN = re.compile(r"^Reply-To: *(.*)")
|
||||||
|
|
||||||
def qencode_subject (message):
|
|
||||||
subjectmatches = re.search (r"^Subject: *(.*)$", message, re.MULTILINE)
|
def qencode_subject(message):
|
||||||
if (subjectmatches == None): return message
|
subjectmatches = re.search(r"^Subject: *(.*)$", message, re.MULTILINE)
|
||||||
|
if subjectmatches == None:
|
||||||
|
return message
|
||||||
subjectstr = subjectmatches.group(1)
|
subjectstr = subjectmatches.group(1)
|
||||||
h = Header(subjectstr, 'utf-8')
|
h = Header(subjectstr, "utf-8")
|
||||||
qsubjectstr=h.encode()
|
qsubjectstr = h.encode()
|
||||||
return message.replace (subjectstr, qsubjectstr, 1)
|
return message.replace(subjectstr, qsubjectstr, 1)
|
||||||
|
|
||||||
def send_message(message, to, attachment) :
|
|
||||||
|
|
||||||
lines = message.split('\n')
|
def send_message(message, to, attachment):
|
||||||
|
|
||||||
|
lines = message.split("\n")
|
||||||
|
|
||||||
# identify headers in the template
|
# identify headers in the template
|
||||||
fromvalue = None
|
fromvalue = None
|
||||||
@ -114,10 +123,10 @@ def send_message(message, to, attachment) :
|
|||||||
ccvalue = None
|
ccvalue = None
|
||||||
bccvalue = None
|
bccvalue = None
|
||||||
replytovalue = None
|
replytovalue = None
|
||||||
|
|
||||||
for index, line in enumerate(lines) :
|
for index, line in enumerate(lines):
|
||||||
if not line :
|
if not line:
|
||||||
body = '\n'.join(lines[index+1:])
|
body = "\n".join(lines[index + 1 :])
|
||||||
break
|
break
|
||||||
|
|
||||||
frommatches = FROMPATTERN.match(line)
|
frommatches = FROMPATTERN.match(line)
|
||||||
@ -126,62 +135,65 @@ def send_message(message, to, attachment) :
|
|||||||
bccmatches = BCCPATTERN.match(line)
|
bccmatches = BCCPATTERN.match(line)
|
||||||
replytomatches = REPLYTOPATTERN.match(line)
|
replytomatches = REPLYTOPATTERN.match(line)
|
||||||
|
|
||||||
if frommatches :
|
if frommatches:
|
||||||
fromvalue = frommatches.group(1)
|
fromvalue = frommatches.group(1)
|
||||||
if subjectmatches :
|
if subjectmatches:
|
||||||
subjectvalue = subjectmatches.group(1)
|
subjectvalue = subjectmatches.group(1)
|
||||||
if ccmatches :
|
if ccmatches:
|
||||||
ccvalue = ccmatches.group(1)
|
ccvalue = ccmatches.group(1)
|
||||||
if bccmatches :
|
if bccmatches:
|
||||||
bccvalue = bccmatches.group(1)
|
bccvalue = bccmatches.group(1)
|
||||||
if replytomatches :
|
if replytomatches:
|
||||||
replytovalue = replytomatches.group(1)
|
replytovalue = replytomatches.group(1)
|
||||||
|
|
||||||
# lists all addresses to which the mail will be sent
|
# lists all addresses to which the mail will be sent
|
||||||
dests = [to]
|
dests = [to]
|
||||||
|
|
||||||
if ccvalue :
|
if ccvalue:
|
||||||
dests.append(ccvalue)
|
dests.append(ccvalue)
|
||||||
if bccvalue :
|
if bccvalue:
|
||||||
dests.append(bccvalue)
|
dests.append(bccvalue)
|
||||||
|
|
||||||
msg = EmailMessage()
|
msg = EmailMessage()
|
||||||
msg['Subject'] = subjectvalue
|
msg["Subject"] = subjectvalue
|
||||||
msg['From'] = fromvalue
|
msg["From"] = fromvalue
|
||||||
msg['To'] = to
|
msg["To"] = to
|
||||||
if replytovalue:
|
if replytovalue:
|
||||||
msg['Reply-To'] = replytovalue
|
msg["Reply-To"] = replytovalue
|
||||||
if ccvalue :
|
if ccvalue:
|
||||||
msg['Cc'] = ccvalue
|
msg["Cc"] = ccvalue
|
||||||
msg.set_content(body)
|
msg.set_content(body)
|
||||||
if attachment:
|
if attachment:
|
||||||
msg.add_attachment(attachment[0], **attachment[1])
|
msg.add_attachment(attachment[0], **attachment[1])
|
||||||
|
|
||||||
print("Sending : %s, from %s to %s, dests : %s" % (subjectvalue, fromvalue, dests[0], repr(dests)))
|
print(
|
||||||
|
"Sending : %s, from %s to %s, dests : %s"
|
||||||
|
% (subjectvalue, fromvalue, dests[0], repr(dests))
|
||||||
|
)
|
||||||
|
|
||||||
# sending the mail to its recipients using the local mailer via SMTP
|
# sending the mail to its recipients using the local mailer via SMTP
|
||||||
server = smtplib.SMTP('localhost','25')
|
server = smtplib.SMTP("localhost", "25")
|
||||||
server.set_debuglevel(1)
|
server.set_debuglevel(1)
|
||||||
server.send_message(msg)
|
server.send_message(msg)
|
||||||
server.quit()
|
server.quit()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
if __name__ == '__main__' :
|
|
||||||
|
|
||||||
|
|
||||||
# handle options of the program
|
# handle options of the program
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 't:b:p:')
|
opts, args = getopt.getopt(sys.argv[1:], "t:b:p:")
|
||||||
except getopt.error as msg:
|
except getopt.error as msg:
|
||||||
print("getopt error: %s" % msg)
|
print("getopt error: %s" % msg)
|
||||||
usage(1)
|
usage(1)
|
||||||
|
|
||||||
for name, value in opts:
|
for name, value in opts:
|
||||||
if name == '-t': TOFILE = value
|
if name == "-t":
|
||||||
elif name == '-b': BODYFILE = value
|
TOFILE = value
|
||||||
elif name == '-p': JOINFILE = value
|
elif name == "-b":
|
||||||
|
BODYFILE = value
|
||||||
|
elif name == "-p":
|
||||||
|
JOINFILE = value
|
||||||
else:
|
else:
|
||||||
print("argument: ", name, " invalid")
|
print("argument: ", name, " invalid")
|
||||||
usage(1)
|
usage(1)
|
||||||
@ -204,8 +216,8 @@ if __name__ == '__main__' :
|
|||||||
counter = 0
|
counter = 0
|
||||||
|
|
||||||
# send mail to each recipient
|
# send mail to each recipient
|
||||||
for set in sets :
|
for set in sets:
|
||||||
|
|
||||||
values = set
|
values = set
|
||||||
|
|
||||||
# the recipient is always in first column
|
# the recipient is always in first column
|
||||||
@ -219,8 +231,7 @@ if __name__ == '__main__' :
|
|||||||
|
|
||||||
# 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
|
||||||
if counter >= 10 :
|
if counter >= 10:
|
||||||
counter = 0
|
counter = 0
|
||||||
print("suspending execution for 5 secs")
|
print("suspending execution for 5 secs")
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user