Support download email file in browser

This commit is contained in:
Son NK 2020-03-15 18:39:59 +01:00 committed by Son NK
parent 9cf5b886cc
commit c4b97b55dc
3 changed files with 29 additions and 5 deletions

View file

@ -34,7 +34,28 @@ def upload_from_bytesio(key: str, bs: BytesIO, content_type="string"):
else: else:
_session.resource("s3").Bucket(BUCKET).put_object( _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";',
) )

View file

@ -30,6 +30,7 @@ It should contain the following info:
""" """
import uuid
import time import time
from email import encoders from email import encoders
from email.message import Message from email.message import Message
@ -540,14 +541,15 @@ def handle_bounce(
disable_alias_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}" disable_alias_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}"
# Store the bounced email # 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" 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" file_path = f"refused-emails/{random_name}.eml"
orig_msg = get_orig_message_from_bounce(msg) s3.upload_email_from_bytesio(file_path, BytesIO(orig_msg.as_bytes()), random_name)
s3.upload_from_bytesio(file_path, BytesIO(orig_msg.as_bytes()))
refused_email = RefusedEmail.create( refused_email = RefusedEmail.create(
path=file_path, full_report_path=full_report_path, user_id=user.id path=file_path, full_report_path=full_report_path, user_id=user.id

View file

@ -14,4 +14,5 @@
{{ render_text('Please let us know if you have any question.') }} {{ render_text('Please let us know if you have any question.') }}
{{ render_text('Thanks, <br />SimpleLogin Team.') }} {{ render_text('Thanks, <br />SimpleLogin Team.') }}
{{ raw_url(refused_email_url) }}
{% endblock %} {% endblock %}