Skip to content

Commit fe70215

Browse files
authored
Merge pull request #15 from jayvdb/outbox-backend
Add OutboxEmailBackend
2 parents dcd6819 + 8af4176 commit fe70215

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

django_mail_admin/backends.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import logging
12
import threading
23

4+
from django.core.mail.backends.base import BaseEmailBackend
35
from django.core.mail.backends.smtp import EmailBackend
46

5-
from .models import Outbox
7+
from .mail import create
8+
from .models import Outbox, create_attachments
9+
from .utils import PRIORITY
10+
11+
logger = logging.getLogger(__name__)
612

713

814
class CustomEmailBackend(EmailBackend):
@@ -28,3 +34,35 @@ def __init__(self, host=None, port=None, username=None, password=None,
2834
self.ssl_certfile = configuration.email_ssl_certfile if ssl_certfile is None else ssl_certfile
2935
self.connection = None
3036
self._lock = threading.RLock()
37+
38+
39+
class OutboxEmailBackend(BaseEmailBackend):
40+
def send_messages(self, email_messages):
41+
for msg in email_messages:
42+
try:
43+
email = create(
44+
sender=msg.from_email,
45+
recipients=msg.to,
46+
cc=msg.cc,
47+
bcc=msg.bcc,
48+
subject=msg.subject,
49+
message=msg.body,
50+
headers=msg.extra_headers,
51+
priority=PRIORITY.medium,
52+
)
53+
alternatives = getattr(msg, 'alternatives', [])
54+
for content, mimetype in alternatives:
55+
if mimetype == 'text/html':
56+
email.html_message = content
57+
email.save()
58+
59+
if msg.attachments:
60+
attachments = create_attachments(msg.attachments)
61+
email.attachments.add(*attachments)
62+
63+
except Exception:
64+
if not self.fail_silently:
65+
raise
66+
logger.exception('Email queue failed')
67+
68+
return len(email_messages)

tests/test_backends.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
from django.core.mail import send_mail
12
from django.core.mail.backends.base import BaseEmailBackend
23
from django.test import TestCase
34
from django.test.utils import override_settings
45
from django_mail_admin.mail import send
56
from django_mail_admin.settings import get_backend
67
from django_mail_admin.backends import CustomEmailBackend
7-
from django_mail_admin.models import Outbox
8+
from django_mail_admin.models import Outbox, OutgoingEmail
89

910

1011
class ErrorRaisingBackend(BaseEmailBackend):
@@ -42,3 +43,14 @@ def test_custom_email_backend(self):
4243
outbox.delete()
4344
with self.assertRaises(ValueError):
4445
backend2 = CustomEmailBackend()
46+
47+
@override_settings(DJANGO_MAIL_ADMIN={}, EMAIL_BACKEND='django_mail_admin.backends.OutboxEmailBackend')
48+
def test_outbox_email_backend(self):
49+
count_before = len(OutgoingEmail.objects.all())
50+
sent_count = send_mail(
51+
subject='Test subject', message='message.',
52+
from_email='from@example.com', recipient_list=['to@example.com'],
53+
fail_silently=False)
54+
self.assertEqual(sent_count, 1)
55+
count_after = len(OutgoingEmail.objects.all())
56+
self.assertEqual(count_after, count_before + 1)

0 commit comments

Comments
 (0)