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(
text_template,
recipient,
to_recipients,
subject,
cc_recipients=[],
bcc_recipients=[],
html_template='',
sender='BornHack <info@bornhack.dk>',
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 <info@bornhack.dk>',
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:

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()
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.'}
)

View file

@ -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