From c4b97b55dc426c1ac0a9914e94d18871f8061c89 Mon Sep 17 00:00:00 2001 From: Son NK Date: Sun, 15 Mar 2020 18:39:59 +0100 Subject: [PATCH] Support download email file in browser --- app/s3.py | 23 ++++++++++++++++++- email_handler.py | 10 ++++---- .../automatic-disable-alias.html | 1 + 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/s3.py b/app/s3.py index 133f9433..dd09bbf1 100644 --- a/app/s3.py +++ b/app/s3.py @@ -34,7 +34,28 @@ def upload_from_bytesio(key: str, bs: BytesIO, content_type="string"): else: _session.resource("s3").Bucket(BUCKET).put_object( - Key=key, Body=bs, ContentType=content_type + Key=key, Body=bs, ContentType=content_type, + ) + + +def upload_email_from_bytesio(path: str, bs: BytesIO, filename): + bs.seek(0) + + if LOCAL_FILE_UPLOAD: + file_path = os.path.join(UPLOAD_DIR, path) + file_dir = os.path.dirname(file_path) + os.makedirs(file_dir, exist_ok=True) + with open(file_path, "wb") as f: + f.write(bs.read()) + + else: + _session.resource("s3").Bucket(BUCKET).put_object( + Key=path, + Body=bs, + # Support saving a remote file using Http header + # Also supports Safari. More info at + # https://github.com/eligrey/FileSaver.js/wiki/Saving-a-remote-file#using-http-header + ContentDisposition=f'attachment; filename="{filename}.eml";', ) diff --git a/email_handler.py b/email_handler.py index 1a69b6ed..b98143b4 100644 --- a/email_handler.py +++ b/email_handler.py @@ -30,6 +30,7 @@ It should contain the following info: """ +import uuid import time from email import encoders from email.message import Message @@ -540,14 +541,15 @@ def handle_bounce( disable_alias_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}" # Store the bounced email - random_name = random_string(50) + orig_msg = get_orig_message_from_bounce(msg) + # generate a name for the email + random_name = str(uuid.uuid4()) full_report_path = f"refused-emails/full-{random_name}.eml" - s3.upload_from_bytesio(full_report_path, BytesIO(msg.as_bytes())) + s3.upload_email_from_bytesio(full_report_path, BytesIO(msg.as_bytes()), random_name) file_path = f"refused-emails/{random_name}.eml" - orig_msg = get_orig_message_from_bounce(msg) - s3.upload_from_bytesio(file_path, BytesIO(orig_msg.as_bytes())) + s3.upload_email_from_bytesio(file_path, BytesIO(orig_msg.as_bytes()), random_name) refused_email = RefusedEmail.create( path=file_path, full_report_path=full_report_path, user_id=user.id diff --git a/templates/emails/transactional/automatic-disable-alias.html b/templates/emails/transactional/automatic-disable-alias.html index 2a040814..91845973 100644 --- a/templates/emails/transactional/automatic-disable-alias.html +++ b/templates/emails/transactional/automatic-disable-alias.html @@ -14,4 +14,5 @@ {{ render_text('Please let us know if you have any question.') }} {{ render_text('Thanks,
SimpleLogin Team.') }} + {{ raw_url(refused_email_url) }} {% endblock %}