1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-09 22:38:19 +02:00
django-mailbox/django_mailbox/management/commands/sendmail.py

27 lines
1.2 KiB
Python
Raw Normal View History

2018-05-23 15:56:12 +03:00
from django.core.management.base import BaseCommand
from django_mailbox.models import Mailbox
class Command(BaseCommand):
help = 'Sends an e-mail to specified recipient'
def add_arguments(self, parser):
2018-05-23 16:49:26 +03:00
parser.add_argument('from', type=int, help='ID (pk) for the mailbox you want to use to send an e-mail')
parser.add_argument('to', type=str, help='E-mail address of recipient')
parser.add_argument('subject', type=str, help='Subject of letter')
parser.add_argument('body', type=str, help='Message')
2018-05-23 15:56:12 +03:00
def handle(self, *args, **options):
from_email = options.get('from')
to = options.get('to')
subject = options.get('subject')
body = options.get('body')
mailbox = Mailbox.objects.get(pk=from_email)
self.stdout.write(self.style.WARNING("-"*36 + "\nFrom: %s \nTo: %s \nSubject: %s \nMessage: %s \n" %
(mailbox.from_email,
2018-05-23 16:49:26 +03:00
to, subject, body) + '-'*36))
if mailbox.send_mail(subject, to, body):
2018-05-23 15:56:12 +03:00
self.stdout.write(self.style.SUCCESS('The letter is sent.'))