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 command that can send an email with an attachment #2193

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions onadata/apps/main/management/commands/send_email_w_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Management command for sending out emails to recipients
with an attachment
"""
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):
"""
Send email with an attachment
"""
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="[email protected]", 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')
)