Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to send base64 encoded attachments to invenio-mail. #61

Merged
merged 3 commits into from
Nov 29, 2023

Conversation

wgresshoff
Copy link
Contributor

❤️ Thank you for your contribution!

Description

It is now possible to send emails with attachments by invenio-mail. They have to be base64 encoded, so they get decoded and attached to the mail.

Checklist

Ticks in all boxes and 🟢 on all GitHub actions status checks are required to merge:

Third-party code

If you've added third-party code (copy/pasted or new dependencies), please reach out to an architect.

Reminder

By using GitHub, you have already agreed to the GitHub’s Terms of Service including that:

  1. You license your contribution under the same terms as the current repository’s license.
  2. You agree that you have the right to license your contribution under the current repository’s license.

invenio_mail/tasks.py Outdated Show resolved Hide resolved

if "attachments" in data:
for attachment in data.get("attachments"):
rawdata = b64decode(attachment.get("base64"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about limiting the max size?

Copy link
Contributor Author

@wgresshoff wgresshoff Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure about this: this code is called after the attachment has been pulled from the message queue, so I felt it is too late to do the check here. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after discussions (and sorry if we did not think about that earlier), maybe a better solution would:

  1. create a new func that accept attachments
  2. the new method will check the size
  3. if valid, it will call an async task
def send_email_attachments(data, attachments):
    """Nice docstring..."""
    for att in attachments:
        ... if att size > config["MAX_ATTACHMENT_SIZE"]:
            fail
    ... update data dict and add attachments ...
    _send_email_attachments.apply_async(data)


@shared_task
def _send_email_attachments
    msg = Message()
    msg.__dict__.update(data)
    msg.attach(content_type=content_type, data=rawdata, disposition=disposition)
    ...

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable, but I'm not quite sure I completely understand your suggestion. The first function is not called via celery but directly? And by calling apply_async a task will be generated? Ok, that would eliminate my objection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check for size limit and a test to see if it works.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable, but I'm not quite sure I completely understand your suggestion. The first function is not called via celery but directly? And by calling apply_async a task will be generated? Ok, that would eliminate my objection.

You implemented it exactly as I was thinking 🥳


if "attachments" in data:
for attachment in data.get("attachments"):
rawdata = b64decode(attachment.get("base64"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable, but I'm not quite sure I completely understand your suggestion. The first function is not called via celery but directly? And by calling apply_async a task will be generated? Ok, that would eliminate my objection.

You implemented it exactly as I was thinking 🥳

@ntarocco
Copy link
Contributor

Thanks for your contribution!

@ntarocco ntarocco merged commit 0c58dea into inveniosoftware:master Nov 29, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Released | Done 🚀
Development

Successfully merging this pull request may close these issues.

2 participants