From 14cb0eb2f0c88b78e15bb50e57979903e86cf9ee Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Thu, 3 Feb 2022 11:59:15 +0300 Subject: [PATCH 1/2] Add command that can send email with attachment --- .../commands/send_email_w_attachment.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 onadata/apps/main/management/commands/send_email_w_attachment.py diff --git a/onadata/apps/main/management/commands/send_email_w_attachment.py b/onadata/apps/main/management/commands/send_email_w_attachment.py new file mode 100644 index 0000000000..8e37443a5b --- /dev/null +++ b/onadata/apps/main/management/commands/send_email_w_attachment.py @@ -0,0 +1,49 @@ +from typing import List + +from django.core.management.base import BaseCommand +from django.core.mail import EmailMessage + + +def send_email_w_attachment( + attachment_path: str, recipients: List[str], + subject: str, body: str, from_email: str): + email = EmailMessage( + subject, + body, + from_email, + recipients) + email.attach_file(attachment_path) + email.send() + + +class Command(BaseCommand): + """ + Management command used to send an email with an attachment + """ + help = 'Send email with attachment' + + def add_arguments(self, parser): + parser.add_argument( + '-a', '--attachment', dest='attachment_path', + type=str, help="Full path to attachment") + parser.add_argument( + '-r', '--recipients', dest='recipients', + type=str, help="Comma-separated list of emails") + parser.add_argument( + '-s', '--subject', dest='subject', + type=str, help="Email subject") + parser.add_argument( + '-b', '--body', dest='body', + type=str, help="Email body") + parser.add_argument( + '-f', '--from', dest='from_email', + type=str, default="noreply@ona.io", help="From email") + + def handle(self, *args, **options): + send_email_w_attachment( + options.get('attachment_path'), + options.get('recipients').split(','), + options.get('subject'), + options.get('body'), + options.get('from_email') + ) From 869fdd0ac9ecf64f508a243a16af2d454c9b87f0 Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Thu, 3 Feb 2022 12:45:21 +0300 Subject: [PATCH 2/2] Add docstrings --- .../main/management/commands/send_email_w_attachment.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/onadata/apps/main/management/commands/send_email_w_attachment.py b/onadata/apps/main/management/commands/send_email_w_attachment.py index 8e37443a5b..c14b992956 100644 --- a/onadata/apps/main/management/commands/send_email_w_attachment.py +++ b/onadata/apps/main/management/commands/send_email_w_attachment.py @@ -1,3 +1,7 @@ +""" +Management command for sending out emails to recipients +with an attachment +""" from typing import List from django.core.management.base import BaseCommand @@ -7,6 +11,9 @@ def send_email_w_attachment( attachment_path: str, recipients: List[str], subject: str, body: str, from_email: str): + """ + Send email with an attachment + """ email = EmailMessage( subject, body,