add bcc and cc to email system
This commit is contained in:
parent
6a98ee3564
commit
c55c68beff
|
@ -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:
|
||||
|
|
19
src/utils/migrations/0002_remove_outgoingemail_recipient.py
Normal file
19
src/utils/migrations/0002_remove_outgoingemail_recipient.py
Normal 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',
|
||||
),
|
||||
]
|
31
src/utils/migrations/0003_auto_20170521_1932.py
Normal file
31
src/utils/migrations/0003_auto_20170521_1932.py
Normal 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),
|
||||
),
|
||||
]
|
|
@ -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.'}
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue