Skip to content

Commit

Permalink
Add base64 encoded attachments to mails (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
wgresshoff authored Nov 29, 2023
1 parent 94bf0ee commit 0c58dea
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 7 deletions.
3 changes: 3 additions & 0 deletions invenio_mail/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@

MAIL_DEFAULT_REPLY_TO = None
"""Reply to mail address for e-mails."""

MAX_ATTACHMENT_SIZE = 1000000
"""Max size of an attachment in bytes."""
13 changes: 13 additions & 0 deletions invenio_mail/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2023 University of Münster.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Errors and exceptions for mail module."""


class AttachmentOversizeException(Exception):
"""Size of attachment is too big exception."""
2 changes: 2 additions & 0 deletions invenio_mail/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ def init_config(app):
for k in dir(config):
if k.startswith("MAIL_"):
app.config.setdefault(k, getattr(config, k))
if k.startswith("MAX_"):
app.config.setdefault(k, getattr(config, k))
41 changes: 40 additions & 1 deletion invenio_mail/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2023 University of Münster.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Background tasks for mail module."""

from __future__ import absolute_import, print_function

from base64 import b64decode

from celery import shared_task
from flask import current_app
from flask_mail import Message

from .errors import AttachmentOversizeException


def send_email_with_attachments(data, attachments):
"""Celery task for sending mails with attachments."""
for attachment in attachments["attachments"]:
if len(attachment["base64"]) > current_app.config["MAX_ATTACHMENT_SIZE"]:
raise AttachmentOversizeException

return _send_email_with_attachments.apply_async(
kwargs={"data": data, "attachments": attachments}
)


@shared_task
def send_email(data):
Expand All @@ -27,7 +42,31 @@ def send_email(data):
`custom serializer <http://docs.celeryproject.org/en/latest/
userguide/calling.html#serializers>`__
can be created if attachments are really needed.
This version adds attachments by putting them into a base64 encoded string.
This is not an optimal solution, it might get problematic if they are too
large to handle by the messaging queue, so check for a maximum size beforehand.
"""
msg = Message()
msg.__dict__.update(data)

current_app.extensions["mail"].send(msg)


@shared_task
def _send_email_with_attachments(data, attachments):
msg = Message()
msg.__dict__.update(data)

if attachments is not None:
for attachment in attachments["attachments"]:
rawdata = b64decode(attachment.get("base64"))
content_type = "application/octet-stream"
if "content_type" in attachment:
content_type = attachment.get("content_type")
disposition = None
if "disposition" in attachment:
disposition = attachment.get("disposition")
msg.attach(content_type=content_type, data=rawdata, disposition=disposition)

current_app.extensions["mail"].send(msg)
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def email_task_app(request):
CELERY_CACHE_BACKEND="memory",
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
MAIL_SUPPRESS_SEND=True,
MAX_ATTACHMENT_SIZE=30,
)
FlaskCeleryExt(app)

Expand Down
64 changes: 58 additions & 6 deletions tests/test_invenio_mail_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2023 University of Münster.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -11,12 +12,8 @@

from __future__ import absolute_import, print_function

import os

import pkg_resources
from flask_mail import Attachment

from invenio_mail.tasks import send_email
from invenio_mail.errors import AttachmentOversizeException
from invenio_mail.tasks import send_email, send_email_with_attachments


def test_send_message_outbox(email_task_app):
Expand Down Expand Up @@ -70,3 +67,58 @@ def test_send_message_with_date(email_task_app):

result_stream = email_task_app.extensions["invenio-mail"].stream
assert result_stream.getvalue().find("Date: Tue, 23 Feb 2016") != -1


def test_send_message_stream_with_attachment(email_task_app):
"""Test sending a message with attachment using Task module."""
with email_task_app.app_context():
with email_task_app.extensions["mail"].record_messages() as outbox:
msg = {
"subject": "Test2",
"sender": "[email protected]",
"recipients": ["[email protected]"],
}
attachments = {
"attachments": [
{
"base64": "RWluIGVpbmZhY2hlciBTdHJpbmcK",
"disposition": "filename.bin",
},
]
}
send_email_with_attachments(msg, attachments)

result_stream = email_task_app.extensions["invenio-mail"].stream
assert (
result_stream.getvalue().find("Content-Type: application/octet-stream")
!= -1
)
assert (
result_stream.getvalue().find("Content-Disposition: filename.bin;")
!= -1
)
assert result_stream.getvalue().find("RWluIGVpbmZhY2hlciBTdHJpbmcK") != -1


def test_send_message_stream_with_oversize_attachment(email_task_app):
"""Test sending a message with oversize attachment."""
with email_task_app.app_context():
with email_task_app.extensions["mail"].record_messages() as outbox:
msg = {
"subject": "Test2",
"sender": "[email protected]",
"recipients": ["[email protected]"],
}
attachments = {
"attachments": [
{
"base64": "RGllcyBpc3QgZGFzIEhhdXMgdm9tIE5pa29sYXVzCg==",
"disposition": "filename.bin",
},
]
}
try:
send_email_with_attachments(msg, attachments)
assert False
except AttachmentOversizeException:
assert True

0 comments on commit 0c58dea

Please sign in to comment.