Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#464 closed defect (invalid)

[patch] Modification of django.core.mail: more fields, using dicts to pass arguments

Reported by: anonymous Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: minor Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, mail.py allows specifying only few email headers. This enhancement would allow to specify fields such as Reply-To, Content-Type and charset. It also adds methods (send_mail_dict, send_mass_mail_dict) allowing to pass these values as a dictionary. It could be done by modifying existing methods and dictionary unpacking, but that would break backwards compatibility (if someone uses fail_silently as a positional argument). So, here's the patch:

Index: mail.py
===================================================================
--- mail.py	(revision 629)
+++ mail.py	(working copy)
@@ -9,15 +9,28 @@
     Easy wrapper for sending a single message to a recipient list. All members
     of the recipient list will see the other recipients in the 'To' field.
     """
-    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently)
+    return send_mass_mail([(subject, message, from_email, recipient_list)], fail_silently)
 
+def send_mail_dict(message_dict, fail_silently=False):
+    return send_mass_mail_dict([message_dict], fail_silently)
+
 def send_mass_mail(datatuple, fail_silently=False):
     """
     Given a datatuple of (subject, message, from_email, recipient_list), sends
     each message to each recipient list. Returns the number of e-mails sent.
+    If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
+    """ 
+    keys = ("subject", "message", "from_email", "recipient_list")
+    return send_mass_mail_dict([dict(zip(keys, item)) for item in datatuple])
 
-    If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
+def send_mass_mail_dict(message_dicts, fail_silently=False):
     """
+    Given a list of dictionaries containing email parameters, sends each message
+    to each recipient list. Returns the number of e-mails sent. Available parameters:
+    'from_email', 'recipient_list', 'message', 'subject', 'reply_email', 'content_type'
+    and 'charset'. Two first are mandatory. If from_email is None, the
+    DEFAULT_FROM_EMAIL setting is used.
+    """ 
     try:
         server = smtplib.SMTP(EMAIL_HOST)
     except:
@@ -25,14 +38,17 @@
             return
         raise
     num_sent = 0
-    for subject, message, from_email, recipient_list in datatuple:
+    for message_dict in message_dicts:
+        recipient_list = message_dict.get("recipient_list", None)
         if not recipient_list:
             continue
-        from_email = from_email or DEFAULT_FROM_EMAIL
-        msg = MIMEText(message)
-        msg['Subject'] = subject
-        msg['From'] = from_email
-        msg['To'] = ', '.join(recipient_list)
+        from_email = message_dict.get("from_email", None) or DEFAULT_FROM_EMAIL
+        msg = MIMEText(message_dict.get("message", ""), _charset=message_dict.get("charset", "us-ascii"))
+        msg["From"] = from_email
+        msg["To"] = ", ".join(recipient_list)
+        if message_dict.has_key("subject"): msg["Subject"] = message_dict["subject"]
+        if message_dict.has_key("reply_email"): msg["Reply-To"] = message_dict["reply_email"]
+        if message_dict.has_key("content_type"): msg.set_type(message_dict["content_type"])
         server.sendmail(from_email, recipient_list, msg.as_string())
         num_sent += 1
     server.quit()

Change History (4)

comment:1 by Adrian Holovaty, 19 years ago

Summary: Modification of django.core.mail: more fields, using dicts to pass arguments[patch] Modification of django.core.mail: more fields, using dicts to pass arguments

comment:2 by (none), 19 years ago

priority: lownormal

It will be glad to see this patch in django_src, need for change charset in mails.

comment:3 by Jacob, 19 years ago

Resolution: invalid
Status: newclosed

Please redo this patch as an attachment (using the guidlines at http://www.djangoproject.com/documentation/contributing/#patch-style).

comment:4 by Link, 18 years ago

Type: enhancementdefect
Note: See TracTickets for help on using tickets.
Back to Top