From c3a27efd4d0b6407e6c41d5b81f6328e9ed4087a Mon Sep 17 00:00:00 2001 From: Shengchen Kan Date: Tue, 10 Sep 2024 21:56:36 +0800 Subject: [PATCH] Fix bug for send_report about attachment --- send_report.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/send_report.py b/send_report.py index 89c59ec..074e07d 100755 --- a/send_report.py +++ b/send_report.py @@ -9,24 +9,22 @@ from pretty_html_table import build_table from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart +from email.mime.application import MIMEApplication def file_to_html_str(path): with open(path, 'r') as fp: content = fp.read().replace('\n','
') return content -def add_attachment(msg, path): - ctype, encoding = mimetypes.guess_type(path) - if ctype is None or encoding is not None: - # No guess could be made, or the file is encoded (compressed), so - # use a generic bag-of-bits type. - ctype = 'application/octet-stream' - maintype, subtype = ctype.split('/', 1) - with open(path, 'rb') as fp: - msg.add_attachment(fp.read(), - maintype=maintype, - subtype=subtype, - filename=os.path.basename(path)) +def add_attachment(msg, f): + with open(f, "rb") as fil: + part = MIMEApplication( + fil.read(), + Name=os.path.basename(f) + ) + # After the file is closed + part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(f) + msg.attach(part) def main(): parser = ArgumentParser( @@ -59,7 +57,7 @@ def main(): # Add attachments for attachment in args.attachments: assert os.path.isfile(attachment), f'{attachment} is not a file' - add_attachment(msg, path) + add_attachment(msg, attachment) all_body_content = '' # Display the CSVs