diff --git a/src/utils/email.py b/src/utils/email.py index fbb90d78..6a458f39 100644 --- a/src/utils/email.py +++ b/src/utils/email.py @@ -11,15 +11,17 @@ logger = logging.getLogger("bornhack.%s" % __name__) def _send_email( text_template, - recipient, + to_recipients, subject, + cc_recipients=[], + bcc_recipients=[], html_template='', sender='BornHack ', attachment=None, attachment_filename='' ): - if not isinstance(recipient, list): - recipient = [recipient] + if not isinstance(to_recipients, list): + to_recipients = [to_recipients] try: # put the basic email together @@ -27,8 +29,9 @@ def _send_email( subject, text_template, sender, - recipient, - [settings.ARCHIVE_EMAIL] + to_recipients, + bcc_recipients + [settings.ARCHIVE_EMAIL], + cc_recipients ) # is there a html version of this email? @@ -57,9 +60,11 @@ def _send_email( def add_outgoing_email( text_template, - recipients, + to_recipients, formatdict, subject, + cc_recipients=[], + bcc_recipients=[], html_template='', sender='BornHack ', attachment=None, @@ -73,10 +78,10 @@ def add_outgoing_email( if html_template: html_template = render_to_string(html_template, formatdict) - if not isinstance(recipients, list): - recipients = [recipients] + if not isinstance(to_recipients, list): + to_recipients = [to_recipients] - for recipient in recipients: + for recipient in to_recipients: try: validate_email(recipient) except ValidationError: @@ -87,7 +92,9 @@ def add_outgoing_email( html_template=html_template, subject=subject, sender=sender, - recipient=recipients + to_recipients=to_recipients, + cc_recipients=cc_recipients, + bcc_recipients=bcc_recipients ) if attachment: diff --git a/src/utils/migrations/0002_remove_outgoingemail_recipient.py b/src/utils/migrations/0002_remove_outgoingemail_recipient.py new file mode 100644 index 00000000..65e857b9 --- /dev/null +++ b/src/utils/migrations/0002_remove_outgoingemail_recipient.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-05-21 16:08 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('utils', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='outgoingemail', + name='recipient', + ), + ] diff --git a/src/utils/migrations/0003_auto_20170521_1932.py b/src/utils/migrations/0003_auto_20170521_1932.py new file mode 100644 index 00000000..4867c105 --- /dev/null +++ b/src/utils/migrations/0003_auto_20170521_1932.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-05-21 17:32 +from __future__ import unicode_literals + +import django.contrib.postgres.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('utils', '0002_remove_outgoingemail_recipient'), + ] + + operations = [ + migrations.AddField( + model_name='outgoingemail', + name='bcc_recipients', + field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(blank=True, max_length=500), blank=True, null=True, size=None), + ), + migrations.AddField( + model_name='outgoingemail', + name='cc_recipients', + field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(blank=True, max_length=500), blank=True, null=True, size=None), + ), + migrations.AddField( + model_name='outgoingemail', + name='to_recipients', + field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(blank=True, max_length=500), blank=True, null=True, size=None), + ), + ] diff --git a/src/utils/models.py b/src/utils/models.py index 4710f4fc..0fe48d72 100644 --- a/src/utils/models.py +++ b/src/utils/models.py @@ -76,8 +76,28 @@ class OutgoingEmail(CreatedUpdatedModel): text_template = models.TextField() html_template = models.TextField(blank=True) sender = models.CharField(max_length=500) + to_recipients = ArrayField( + models.CharField(max_length=500, blank=True), + null=True, + blank=True + ) + cc_recipients = ArrayField( + models.CharField(max_length=500, blank=True), + null=True, + blank=True + ) + bcc_recipients = ArrayField( + models.CharField(max_length=500, blank=True), + null=True, + blank=True + ) attachment = models.FileField(blank=True) processed = models.BooleanField(default=False) - def __str__(self): - return 'Email {} for {}'.format(self.subject, self.recipient) + def clean(self): + if not self.to_recipients \ + and not self.bcc_recipients \ + and not self.cc_recipients: + raise ValidationError( + {'recipient': 'either to_recipient, bcc_recipient or cc_recipient required.'} + ) diff --git a/src/utils/outgoingemailworker.py b/src/utils/outgoingemailworker.py index 717f5b12..01355f7f 100644 --- a/src/utils/outgoingemailworker.py +++ b/src/utils/outgoingemailworker.py @@ -18,10 +18,6 @@ def do_work(): ) for email in not_processed_email: - if ',' in email.recipient: - recipient = email.recipient.split(',') - else: - recipient = [email.recipient] attachment = None attachment_filename = '' @@ -31,8 +27,10 @@ def do_work(): mail_send_success = _send_email( text_template=email.text_template, - recipient=recipient, + to_recipients=email.to_recipients, subject=email.subject, + cc_recipients=email.cc_recipients, + bcc_recipients=email.bcc_recipients, html_template=email.html_template, attachment=attachment, attachment_filename=attachment_filename