app/soc/logic/mail_dispatcher.py
changeset 1449 9d2e46122b36
parent 1308 35b75ffcbb37
child 1545 ce5d37a38091
equal deleted inserted replaced
1448:abb676cb0094 1449:9d2e46122b36
    16 
    16 
    17 """Functions used to send email messages.
    17 """Functions used to send email messages.
    18 
    18 
    19 The following are the possible fields of an email message:
    19 The following are the possible fields of an email message:
    20 
    20 
    21   sender: The email address of the sender, the From address. This must be the 
    21   sender: The email address of the sender, the From address. This must be the
    22     email address of a registered administrator for the application, or the
    22     email address of a registered administrator for the application, or the
    23     address of the current signed-in user. Administrators can be added to
    23     address of the current signed-in user. Administrators can be added to
    24     an application using the Administration Console. The current user's email
    24     an application using the Administration Console. The current user's email
    25     address can be determined with the Users API.
    25     address can be determined with the Users API.
    26   to: A recipient's email address (a string) or a list of email addresses to 
    26   to: A recipient's email address (a string) or a list of email addresses to
    27     appear on the To: line in the message header.
    27     appear on the To: line in the message header.
    28   cc: A recipient's email address (a string) or a list of email addresses to 
    28   cc: A recipient's email address (a string) or a list of email addresses to
    29     appear on the Cc: line in the message header.
    29     appear on the Cc: line in the message header.
    30   bcc: A recipient's email address (a string) or a list of email addresses to 
    30   bcc: A recipient's email address (a string) or a list of email addresses to
    31     receive the message, but not appear in the message header ("blind carbon 
    31     receive the message, but not appear in the message header ("blind carbon
    32     copy").
    32     copy").
    33   reply_to: An email address to which a recipient should reply instead of the 
    33   reply_to: An email address to which a recipient should reply instead of the
    34     sender address, the Reply-To: field.
    34     sender address, the Reply-To: field.
    35   subject: The subject of the message, the Subject: line.
    35   subject: The subject of the message, the Subject: line.
    36   body: The plaintext body content of the message.
    36   body: The plaintext body content of the message.
    37   html: An HTML version of the body content, for recipients that 
    37   html: An HTML version of the body content, for recipients that
    38     prefer HTML email.
    38     prefer HTML email.
    39   attachments: The file attachments for the message, as a list of two-value 
    39   attachments: The file attachments for the message, as a list of two-value
    40     tuples, one tuple for each attachment. Each tuple contains a filename as 
    40     tuples, one tuple for each attachment. Each tuple contains a filename as
    41     the first element, and the file contents as the second element.
    41     the first element, and the file contents as the second element.
    42     An attachment file must be one of the allowed file types, and the 
    42     An attachment file must be one of the allowed file types, and the
    43     filename must end with an extension that corresponds with the type. 
    43     filename must end with an extension that corresponds with the type.
    44     For a list of allowed types and filename extensions, see Allowed 
    44     For a list of allowed types and filename extensions, see Allowed
    45     Attachment Types.
    45     Attachment Types.
    46 
    46 
    47 Usage:
    47 Usage:
    48 
    48 
    49   context = { 'sender': 'melange-noreply@foo.com',
    49   context = { 'sender': 'melange-noreply@example.com',
    50               'to': 'bob@bar.com',
    50               'to': 'test@example.com',
    51               'subject': 'You have been invited to become a Host',
    51               'subject': 'You have been invited to become a Host',
    52               'sender_name': 'Alice',
    52               'sender_name': 'Alice',
    53               'to_name': 'Melange Team',
    53               'to_name': 'Melange Team',
    54               'role': 'Host',
    54               'role': 'Host',
    55               'group': 'Google Summer of Code 2009',
    55               'group': 'Google Summer of Code 2009',
    71 from soc.logic import dicts
    71 from soc.logic import dicts
    72 
    72 
    73 
    73 
    74 def sendMailFromTemplate(template, context):
    74 def sendMailFromTemplate(template, context):
    75   """Sends out an email using a Django template.
    75   """Sends out an email using a Django template.
    76   
    76 
    77   If 'html' is present in context dictionary it is overwritten with
    77   If 'html' is present in context dictionary it is overwritten with
    78   template HTML output.
    78   template HTML output.
    79   
    79 
    80   Args:
    80   Args:
    81     template: the template (or search list of templates) to use
    81     template: the template (or search list of templates) to use
    82     context: The context supplied to the template and email (dictionary)
    82     context: The context supplied to the template and email (dictionary)
    83   
    83 
    84   Raises:
    84   Raises:
    85     Error that corresponds with the first problem it finds iff the message 
    85     Error that corresponds with the first problem it finds iff the message
    86     is not properly initialized.
    86     is not properly initialized.
    87 
    87 
    88     List of all possible errors:
    88     List of all possible errors:
    89       http://code.google.com/appengine/docs/mail/exceptions.html
    89       http://code.google.com/appengine/docs/mail/exceptions.html
    90   """
    90   """
       
    91 
    91   # render the template and put in context with 'html' as key
    92   # render the template and put in context with 'html' as key
    92   context['html'] = loader.render_to_string(template, dictionary=context)
    93   context['html'] = loader.render_to_string(template, dictionary=context)
    93 
    94 
    94   # filter out the unneeded values in context to keep sendMail happy
    95   # filter out the unneeded values in context to keep sendMail happy
    95   sendMail(dicts.filter(context, mail.EmailMessage.PROPERTIES))
    96   sendMail(dicts.filter(context, mail.EmailMessage.PROPERTIES))
    96 
    97 
    97 
    98 
    98 def sendMail(context):
    99 def sendMail(context):
    99   """Sends out an email using context to supply the needed information.
   100   """Sends out an email using context to supply the needed information.
   100   
   101 
   101   Args:
   102   Args:
   102     context : The context supplied to the email message (dictionary)
   103     context : The context supplied to the email message (dictionary)
   103 
   104 
   104   Raises:
   105   Raises:
   105     Error that corresponds with the first problem it finds iff the message 
   106     Error that corresponds with the first problem it finds iff the message
   106     is not properly initialized.
   107     is not properly initialized.
   107 
   108 
   108     List of all possible errors:
   109     List of all possible errors:
   109       http://code.google.com/appengine/docs/mail/exceptions.html
   110       http://code.google.com/appengine/docs/mail/exceptions.html
   110   """
   111   """
   111   
   112 
   112   # construct the EmailMessage from the given context
   113   # construct the EmailMessage from the given context
   113   message = mail.EmailMessage(**context)
   114   message = mail.EmailMessage(**context)
   114   
       
   115   message.check_initialized()
   115   message.check_initialized()
   116   
   116 
   117   # send the message
   117   # send the message
   118   message.send()
   118   message.send()