1+ import logging
12import threading
23
4+ from django .core .mail .backends .base import BaseEmailBackend
35from 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
814class 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 )
0 commit comments