Browse Source

Mailer documented and polished some. #321

Rafał Pitoń 11 years ago
parent
commit
dcb00710bb

+ 29 - 0
docs/developers/mails.rst

@@ -1,3 +1,32 @@
 =============
 Sending Mails
 =============
+
+
+Misago provides its own API for sending e-mails to forum users that extends standard `Django mailer <https://docs.djangoproject.com/en/dev/topics/email/>`_.
+
+This API lives in :py:mod:`misago.core.mail` and has two functions:
+
+
+mail_user
+---------
+
+.. function:: mail_user(request, recipient, subject, template, context=None)
+
+Build e-mail message using supplied template name and (optionally context), then send it to user. Template name shouldn't contain file extension, as Misago will automatically append ``.html`` for html content and ``.txt`` for plaintext content for sent message. Message templates will have access to same request context as other templates, additional context you've provided and two extra context values: ``recipient`` and ``sender``.
+
+* ``request:`` HttpRequest object instance.
+* ``recipient:`` User model instance.
+* ``subject:`` A string.
+* ``template:`` A string.
+* ``context:`` The optional dictionary with extra context values that should be available for message templates.
+
+
+mail_users
+----------
+
+.. function:: mail_users(request, recipients, subject, template, context=None)
+
+Same as above, but instead of sending message to one recipient, it sends it to many recipients at same time. Keep on mind this may be memory intensitive as this function creates one Mail object instance for every recipient specified, so you may want to split recipients into smaller groups as you are sending them emails.
+
+* ``recipients:`` Iterable of User models.

+ 3 - 2
misago/conf/defaults.py

@@ -94,6 +94,7 @@ USE_TZ = True
 TIME_ZONE = 'UTC'
 
 
-# Misago Mailer
+# How many e-mails should be sent in single step.
+# This is used for conserving memory usage when mailing many users at same time
 
-MISAGO_MAILER_BATCH_SIZE = 30
+MISAGO_MAILER_BATCH_SIZE = 20

+ 1 - 7
misago/core/mail.py

@@ -25,19 +25,13 @@ def mail_user(request, recipient, subject, template, context=None):
     message.send()
 
 
-def mail_users(request, recipients, subject, template, context=None, batch=None):
-    batch = batch or settings.MISAGO_MAILER_BATCH_SIZE
+def mail_users(request, recipients, subject, template, context=None):
     messages = []
 
     for recipient in recipients:
         messages.append(
             _build_mail(request, recipient, subject, template, context))
 
-        if batch and len(messages) >= batch:
-            connection = djmail.get_connection()
-            connection.send_messages(messages)
-            messages = []
-
     if messages:
         connection = djmail.get_connection()
         connection.send_messages(messages)

+ 3 - 0
misago/templates/misago/email/base.html

@@ -0,0 +1,3 @@
+<h1>{% block title %}{% endblock %}</h1>
+<hr>
+{% block content %}{% endblock %}

+ 4 - 0
misago/templates/misago/email/base.txt

@@ -0,0 +1,4 @@
+{% block title %}{% endblock %}
+================================================
+
+{% block content %}{% endblock %}