add bcc and cc to email system

This commit is contained in:
Stephan Telling 2017-05-21 20:13:49 +02:00
parent 6a98ee3564
commit c55c68beff
No known key found for this signature in database
GPG key ID: D4892289F36ADA9B
5 changed files with 92 additions and 17 deletions

View file

@ -11,15 +11,17 @@ logger = logging.getLogger("bornhack.%s" % __name__)
def _send_email( def _send_email(
text_template, text_template,
recipient, to_recipients,
subject, subject,
cc_recipients=[],
bcc_recipients=[],
html_template='', html_template='',
sender='BornHack <info@bornhack.dk>', sender='BornHack <info@bornhack.dk>',
attachment=None, attachment=None,
attachment_filename='' attachment_filename=''
): ):
if not isinstance(recipient, list): if not isinstance(to_recipients, list):
recipient = [recipient] to_recipients = [to_recipients]
try: try:
# put the basic email together # put the basic email together
@ -27,8 +29,9 @@ def _send_email(
subject, subject,
text_template, text_template,
sender, sender,
recipient, to_recipients,
[settings.ARCHIVE_EMAIL] bcc_recipients + [settings.ARCHIVE_EMAIL],
cc_recipients
) )
# is there a html version of this email? # is there a html version of this email?
@ -57,9 +60,11 @@ def _send_email(
def add_outgoing_email( def add_outgoing_email(
text_template, text_template,
recipients, to_recipients,
formatdict, formatdict,
subject, subject,
cc_recipients=[],
bcc_recipients=[],
html_template='', html_template='',
sender='BornHack <info@bornhack.dk>', sender='BornHack <info@bornhack.dk>',
attachment=None, attachment=None,
@ -73,10 +78,10 @@ def add_outgoing_email(
if html_template: if html_template:
html_template = render_to_string(html_template, formatdict) html_template = render_to_string(html_template, formatdict)
if not isinstance(recipients, list): if not isinstance(to_recipients, list):
recipients = [recipients] to_recipients = [to_recipients]
for recipient in recipients: for recipient in to_recipients:
try: try:
validate_email(recipient) validate_email(recipient)
except ValidationError: except ValidationError:
@ -87,7 +92,9 @@ def add_outgoing_email(
html_template=html_template, html_template=html_template,
subject=subject, subject=subject,
sender=sender, sender=sender,
recipient=recipients to_recipients=to_recipients,
cc_recipients=cc_recipients,
bcc_recipients=bcc_recipients
) )
if attachment: if attachment:

View file

@ -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',
),
]

View file

@ -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),
),
]

View file

@ -76,8 +76,28 @@ class OutgoingEmail(CreatedUpdatedModel):
text_template = models.TextField() text_template = models.TextField()
html_template = models.TextField(blank=True) html_template = models.TextField(blank=True)
sender = models.CharField(max_length=500) 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) attachment = models.FileField(blank=True)
processed = models.BooleanField(default=False) processed = models.BooleanField(default=False)
def __str__(self): def clean(self):
return 'Email {} for {}'.format(self.subject, self.recipient) 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.'}
)

View file

@ -18,10 +18,6 @@ def do_work():
) )
for email in not_processed_email: for email in not_processed_email:
if ',' in email.recipient:
recipient = email.recipient.split(',')
else:
recipient = [email.recipient]
attachment = None attachment = None
attachment_filename = '' attachment_filename = ''
@ -31,8 +27,10 @@ def do_work():
mail_send_success = _send_email( mail_send_success = _send_email(
text_template=email.text_template, text_template=email.text_template,
recipient=recipient, to_recipients=email.to_recipients,
subject=email.subject, subject=email.subject,
cc_recipients=email.cc_recipients,
bcc_recipients=email.bcc_recipients,
html_template=email.html_template, html_template=email.html_template,
attachment=attachment, attachment=attachment,
attachment_filename=attachment_filename attachment_filename=attachment_filename