diff --git a/docs/source/installation.rst b/docs/source/installation.rst index a0b25c6..6685ba4 100644 --- a/docs/source/installation.rst +++ b/docs/source/installation.rst @@ -103,3 +103,7 @@ against various storages such as S3 Boto. To force Django's File System storage:: MAILQUEUE_STORAGE = True + +To change the Attachment dir:: + + MAILQUEUE_ATTACHMENT_DIR = 'mailqueue-attachments' diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 9f2efba..a651719 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -12,6 +12,7 @@ Create a new MailerMessage() object:: new_message = MailerMessage() new_message.subject = "My Subject" new_message.to_address = "someone@example.com" + new_message.cc_address = "carboncopy@yo.com" new_message.bcc_address = "myblindcarboncopy@yo.com" new_message.from_address = "hello@example.com" new_message.content = "Mail content" @@ -57,13 +58,14 @@ You can add a reply to header to your emails be setting:: Sending to Multiple Recipients ------------------------------ -To include more than one BCC in your email, just separate the addresses with a comma:: +To include more than one CC/BCC in your email, just separate the addresses with a comma:: + message.cc_address = "one@mail.com, two@mail.com, three@mail.com" message.bcc_address = "one@mail.com, two@mail.com, three@mail.com" As of version 2.2.0 multiple recipients may be included in the `to_address` field as well:: - message.bcc_address = "one@mail.com, two@mail.com, three@mail.com" + message.to_address = "one@mail.com, two@mail.com, three@mail.com" Using the Management Command diff --git a/example/mail_example/views.py b/example/mail_example/views.py index bc9886e..c687fc5 100644 --- a/example/mail_example/views.py +++ b/example/mail_example/views.py @@ -7,6 +7,7 @@ def create_mail_message(request): new_message = MailerMessage() new_message.subject = "My Subject" new_message.to_address = "someone@example.com" + new_message.cc_address = "carboncopy@yo.com" new_message.bcc_address = "myblindcarboncopy@yo.com" new_message.from_address = "hello@example.com" new_message.content = "Mail content" diff --git a/mailqueue/admin.py b/mailqueue/admin.py index f5f54cf..f10201f 100644 --- a/mailqueue/admin.py +++ b/mailqueue/admin.py @@ -11,7 +11,7 @@ class AttachmentInline(admin.TabularInline): class MailerAdmin(admin.ModelAdmin): list_display = ('created', 'subject', 'to_address', 'app', 'sent', 'last_attempt', 'reply_to') - search_fields = ['to_address', 'subject', 'app', 'bcc_address', 'reply_to'] + search_fields = ['to_address', 'subject', 'app', 'cc_address', 'bcc_address', 'reply_to'] actions = ['send_failed'] inlines = [AttachmentInline] diff --git a/mailqueue/migrations/0005_cc_address_created.py b/mailqueue/migrations/0005_cc_address_created.py new file mode 100644 index 0000000..4335ef7 --- /dev/null +++ b/mailqueue/migrations/0005_cc_address_created.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('mailqueue', '0004_mailermessage_created'), + ] + + operations = [ + migrations.AddField( + model_name='mailermessage', + name='cc_address', + field=models.TextField(verbose_name='CC', blank=True), + preserve_default=True, + ), + ] diff --git a/mailqueue/models.py b/mailqueue/models.py index 09dea20..6344d70 100644 --- a/mailqueue/models.py +++ b/mailqueue/models.py @@ -42,6 +42,7 @@ class MailerMessage(models.Model): editable=False, null=True) subject = models.CharField(_('Subject'), max_length=250, blank=True) to_address = models.TextField(_('To')) + cc_address = models.TextField(_('CC'), blank=True) bcc_address = models.TextField(_('BCC'), blank=True) from_address = models.EmailField(_('From'), max_length=250) reply_to = models.TextField(_('Reply to'), max_length=250, blank=True, null=True) @@ -107,6 +108,7 @@ def _send(self): msg.attach_alternative(html_content, "text/html") msg.to = [email.strip() for email in self.to_address.split(',') if email.strip()] + msg.cc = [email.strip() for email in self.cc_address.split(',') if email.strip()] msg.bcc = [email.strip() for email in self.bcc_address.split(',') if email.strip()] # Add any additional attachments diff --git a/mailqueue/tests/test_messages.py b/mailqueue/tests/test_messages.py index 947b3fe..cfc2964 100644 --- a/mailqueue/tests/test_messages.py +++ b/mailqueue/tests/test_messages.py @@ -51,15 +51,22 @@ def test_ugly_addresses(self): Jane@mail.co.uk, john@mail.co.uk, , julie@mail.co.uk, ''' + cc_addresses = ''' + , Lou@mail.co.uk, + lisa@mail.co.uk, , lori@mail.co.uk, + , + ''' bcc_addresses = ''' , Lou@mail.co.uk, lisa@mail.co.uk, , lori@mail.co.uk, , ''' - MailFactory.create(to_address=addresses, bcc_address=bcc_addresses) + MailFactory.create(to_address=addresses, cc_address=cc_addresses, bcc_address=bcc_addresses) self.assertEqual(mail.outbox[0].to, ["Jane@mail.co.uk", "john@mail.co.uk", "julie@mail.co.uk"]) + self.assertEqual(mail.outbox[0].cc, ["Lou@mail.co.uk", "lisa@mail.co.uk", + "lori@mail.co.uk"]) self.assertEqual(mail.outbox[0].bcc, ["Lou@mail.co.uk", "lisa@mail.co.uk", "lori@mail.co.uk"]) @@ -69,6 +76,14 @@ def test_multiple_to(self): self.assertEqual(mail.outbox[0].to, ["Jane@mail.co.uk", "john@mail.co.uk", "julie@mail.co.uk"]) + def test_single_cc(self): + MailFactory.create(cc_address="cc@mail.co.uk") + self.assertEqual(mail.outbox[0].cc, ["cc@mail.co.uk"]) + + def test_multiple_cc(self): + MailFactory.create(cc_address="cc_one@mail.co.uk, cc_two@mail.co.uk") + self.assertEqual(mail.outbox[0].cc, ["cc_one@mail.co.uk", "cc_two@mail.co.uk"]) + def test_single_bcc(self): MailFactory.create(bcc_address="bcc@mail.co.uk") self.assertEqual(mail.outbox[0].bcc, ["bcc@mail.co.uk"]) diff --git a/mailqueue/utils.py b/mailqueue/utils.py index b2535d0..0dc5c56 100644 --- a/mailqueue/utils.py +++ b/mailqueue/utils.py @@ -2,6 +2,8 @@ from django.core.files.storage import FileSystemStorage from django.utils.crypto import get_random_string +MAILQUEUE_ATTACHMENT_DIR = getattr(settings, 'MAILQUEUE_ATTACHMENT_DIR', 'mailqueue-attachments') + class MailerStorage(FileSystemStorage): def __init__(self, location=None): @@ -27,4 +29,4 @@ def upload_to(instance, filename): filename = filename.split('/')[-1] # Because instead of filesystem, email message # can have multiple attachments with the same filename - return 'mailqueue-attahcments/{0}_{1}'.format(get_random_string(length=24), filename) + return '{0}/{1}_{2}'.format(MAILQUEUE_ATTACHMENT_DIR, get_random_string(length=24), filename) diff --git a/requirements.txt b/requirements.txt index c4e5c70..3850589 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ -pytest==2.6.4 -pytest-django==2.7.0 -pytest-flakes==0.2 +pytest==3.1.2 +pytest-django==3.1.2 +pytest-flakes==2.0.0 pytest-pep8==1.0.6 factory_boy==2.8.1