3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Functions used to send email messages.
19 The following are the possible fields of an email message:
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
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
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
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
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
31 receive the message, but not appear in the message header ("blind carbon
33 reply_to: An email address to which a recipient should reply instead of the
34 sender address, the Reply-To: field.
35 subject: The subject of the message, the Subject: line.
36 body: The plaintext body content of the message.
37 html: An HTML version of the body content, for recipients that
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
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
43 filename must end with an extension that corresponds with the type.
44 For a list of allowed types and filename extensions, see Allowed
49 context = { 'sender': 'melange-noreply@example.com',
50 'to': 'test@example.com',
51 'subject': 'You have been invited to become a Host',
52 'sender_name': 'Alice',
53 'to_name': 'Melange Team',
55 'group': 'Google Summer of Code 2009',
56 'invitation_url': 'http://invitation-url'}
58 sendMailFromTemplate('soc/mail/invitation.html', context)
62 '"Lennard de Rijk" <ljvderijk@gmail.com>',
63 '"Pawel Solyga" <pawel.solyga@gmail.com',
67 from django
.template
import loader
69 from google
.appengine
.api
import mail
71 from soc
.logic
import dicts
74 def sendMailFromTemplate(template
, context
):
75 """Sends out an email using a Django template.
77 If 'html' is present in context dictionary it is overwritten with
81 template: the template (or search list of templates) to use
82 context: The context supplied to the template and email (dictionary)
85 Error that corresponds with the first problem it finds iff the message
86 is not properly initialized.
88 List of all possible errors:
89 http://code.google.com/appengine/docs/mail/exceptions.html
92 # render the template and put in context with 'html' as key
93 context
['html'] = loader
.render_to_string(template
, dictionary
=context
)
95 # filter out the unneeded values in context to keep sendMail happy
96 sendMail(dicts
.filter(context
, mail
.EmailMessage
.PROPERTIES
))
99 def sendMail(context
):
100 """Sends out an email using context to supply the needed information.
103 context : The context supplied to the email message (dictionary)
106 Error that corresponds with the first problem it finds iff the message
107 is not properly initialized.
109 List of all possible errors:
110 http://code.google.com/appengine/docs/mail/exceptions.html
113 # construct the EmailMessage from the given context
114 message
= mail
.EmailMessage(**context
)
115 message
.check_initialized()
120 except mail
.Error
, exception
:
122 logging
.info(context
)
123 logging
.exception(exception
)
125 def getDefaultMailSender():
126 """Returns the sender that currently can be used to send emails.
129 - A tuple containing (sender_name, sender_address)
131 - If available the site name and noreply address from the site singleton
132 - Or the public name and email address of the current logged in User
133 - None if there is no address to return
138 from soc
.logic
import accounts
139 from soc
.logic
.models
import user
as user_logic
140 from soc
.logic
.models
import site
as site_logic
142 # check if there is a noreply email address set
143 site_entity
= site_logic
.logic
.getSingleton()
145 if site_entity
.noreply_email
:
146 return (site_entity
.site_name
, site_entity
.noreply_email
)
148 # use the email address of the current logged in user
149 account
= accounts
.getCurrentAccount(normalize
=False)
151 # we need to retrieve account seperately, as user_logic normalizes it
152 # and the GAE admin API is case sensitive
153 user_entity
= user_logic
.logic
.getForAccount(account
)
156 logging
.warning('Non-Authenticated user triggered getDefaultMailSender')
159 return (user_entity
.name
, account
.email())