app/soc/logic/mail_dispatcher.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 24 Jul 2009 20:57:53 +0200
changeset 2677 7df4d0fcbb80
parent 2360 e389d26949db
permissions -rw-r--r--
Use proper in-line import in Request helper.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
"""Functions used to send email messages.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
The following are the possible fields of an email message:
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    21
  sender: The email address of the sender, the From address. This must be the
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
    email address of a registered administrator for the application, or the
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
    address of the current signed-in user. Administrators can be added to
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
    an application using the Administration Console. The current user's email
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
    address can be determined with the Users API.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    26
  to: A recipient's email address (a string) or a list of email addresses to
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
    appear on the To: line in the message header.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    28
  cc: A recipient's email address (a string) or a list of email addresses to
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
    appear on the Cc: line in the message header.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    30
  bcc: A recipient's email address (a string) or a list of email addresses to
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    31
    receive the message, but not appear in the message header ("blind carbon
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
    copy").
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    33
  reply_to: An email address to which a recipient should reply instead of the
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    34
    sender address, the Reply-To: field.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
  subject: The subject of the message, the Subject: line.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    36
  body: The plaintext body content of the message.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    37
  html: An HTML version of the body content, for recipients that
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
    prefer HTML email.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    39
  attachments: The file attachments for the message, as a list of two-value
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    40
    tuples, one tuple for each attachment. Each tuple contains a filename as
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    41
    the first element, and the file contents as the second element.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    42
    An attachment file must be one of the allowed file types, and the
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    43
    filename must end with an extension that corresponds with the type.
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    44
    For a list of allowed types and filename extensions, see Allowed
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    45
    Attachment Types.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    46
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    47
Usage:
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    49
  context = { 'sender': 'melange-noreply@example.com',
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    50
              'to': 'test@example.com',
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
              'subject': 'You have been invited to become a Host',
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    52
              'sender_name': 'Alice',
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    53
              'to_name': 'Melange Team',
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    54
              'role': 'Host',
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    55
              'group': 'Google Summer of Code 2009',
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    56
              'invitation_url': 'http://invitation-url'}
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    57
607
7e560d8cc035 Fixed typo in docstring.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 509
diff changeset
    58
  sendMailFromTemplate('soc/mail/invitation.html', context)
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    59
"""
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
__authors__ = [
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    63
  '"Pawel Solyga" <pawel.solyga@gmail.com',
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    64
  ]
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    65
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    66
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    67
from django.template import loader
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    68
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    69
from google.appengine.api import mail
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    70
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    71
from soc.logic import dicts
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    73
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    74
def sendMailFromTemplate(template, context):
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    75
  """Sends out an email using a Django template.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    76
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    77
  If 'html' is present in context dictionary it is overwritten with
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    78
  template HTML output.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    79
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    80
  Args:
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    81
    template: the template (or search list of templates) to use
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    82
    context: The context supplied to the template and email (dictionary)
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    83
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    84
  Raises:
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    85
    Error that corresponds with the first problem it finds iff the message
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    86
    is not properly initialized.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    87
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    88
    List of all possible errors:
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    89
      http://code.google.com/appengine/docs/mail/exceptions.html
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    90
  """
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    91
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    92
  # render the template and put in context with 'html' as key
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    93
  context['html'] = loader.render_to_string(template, dictionary=context)
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    94
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    95
  # filter out the unneeded values in context to keep sendMail happy
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    96
  sendMail(dicts.filter(context, mail.EmailMessage.PROPERTIES))
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    97
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    98
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    99
def sendMail(context):
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   100
  """Sends out an email using context to supply the needed information.
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   101
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   102
  Args:
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   103
    context : The context supplied to the email message (dictionary)
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   104
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   105
  Raises:
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   106
    Error that corresponds with the first problem it finds iff the message
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   107
    is not properly initialized.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   108
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   109
    List of all possible errors:
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   110
      http://code.google.com/appengine/docs/mail/exceptions.html
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   111
  """
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   112
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   113
  # construct the EmailMessage from the given context
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   114
  message = mail.EmailMessage(**context)
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   115
  message.check_initialized()
1449
9d2e46122b36 Fixed whitespace damadge and missing EOL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   116
1560
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1551
diff changeset
   117
  try:
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1551
diff changeset
   118
    # send the message
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1551
diff changeset
   119
    message.send()
2073
6eb9b4652c80 Style fixes and removal of unused imports in soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1655
diff changeset
   120
  except mail.Error, exception:
1560
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1551
diff changeset
   121
    import logging
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1551
diff changeset
   122
    logging.info(context)
1655
ed1df29f4fd9 Rename too short variable name in soc.logic.mail_dispatcher module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   123
    logging.exception(exception)
1545
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   124
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   125
def getDefaultMailSender():
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   126
  """Returns the sender that currently can be used to send emails.
2242
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   127
1545
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   128
  Returns:
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   129
    - A tuple containing (sender_name, sender_address)
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   130
    Consisting of:
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   131
    - If available the site name and noreply address from the site singleton
2242
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   132
    - Or the (public) name and email address of the current logged in User
1545
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   133
    - None if there is no address to return
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   134
  """
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   135
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   136
  import logging
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   137
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   138
  from soc.logic import accounts
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   139
  from soc.logic.models import user as user_logic
1545
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   140
  from soc.logic.models import site as site_logic
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   141
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   142
  # check if there is a noreply email address set
1545
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   143
  site_entity = site_logic.logic.getSingleton()
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   144
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   145
  if site_entity.noreply_email:
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   146
    return (site_entity.site_name, site_entity.noreply_email)
1545
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   147
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   148
  # use the email address of the current logged in user
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1560
diff changeset
   149
  account = accounts.getCurrentAccount(normalize=False)
1545
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   150
2242
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   151
  if not account:
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   152
    logging.warning('Non-Authenticated user triggered getDefaultMailSender '
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   153
                    'please set a no-reply address in Site settings')
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   154
    return None
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   155
2237
3cfaa7dfd9e0 Fixed a comment typo and possible bug in mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2073
diff changeset
   156
  # we need to retrieve account separately, as user_logic normalizes it
1560
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1551
diff changeset
   157
  # and the GAE admin API is case sensitive
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1551
diff changeset
   158
  user_entity = user_logic.logic.getForAccount(account)
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1551
diff changeset
   159
2360
e389d26949db Style and pylint fixes in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2242
diff changeset
   160
  # pylint: disable-msg=E1103
2242
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   161
  name = user_entity.name if user_entity else account.nickname()
1545
ce5d37a38091 Added getDefaultMailSender method to mail_dispatcher.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1449
diff changeset
   162
2242
0326d394dce5 getDefaultMailSender now properly handles users who are logged in but not registered.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2237
diff changeset
   163
  return (name, account.email())