|
1 #!/usr/bin/env python |
|
2 # |
|
3 # Copyright 2007 Google Inc. |
|
4 # |
|
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 |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
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. |
|
16 # |
|
17 |
|
18 """Handler library for inbound Mail API. |
|
19 |
|
20 Contains handlers to help with receiving mail. |
|
21 |
|
22 InboundMailHandler: Has helper method for easily setting up |
|
23 email recievers. |
|
24 """ |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 from google.appengine.api import mail |
|
31 from google.appengine.ext import webapp |
|
32 |
|
33 |
|
34 MAIL_HANDLER_URL_PATTERN = '/_ah/mail/.+' |
|
35 |
|
36 |
|
37 class InboundMailHandler(webapp.RequestHandler): |
|
38 """Base class for inbound mail handlers. |
|
39 |
|
40 Example: |
|
41 |
|
42 # Sub-class overrides receive method. |
|
43 class HelloReceiver(InboundMailHandler): |
|
44 |
|
45 def receive(self, mail_message): |
|
46 logging.info('Received greeting from %s: %s' % (mail_message.sender, |
|
47 mail_message.body)) |
|
48 |
|
49 |
|
50 # Map mail handler to appliction. |
|
51 application = webapp.WSGIApplication([ |
|
52 HelloReceiver.mapping(), |
|
53 ]) |
|
54 """ |
|
55 |
|
56 def post(self): |
|
57 """Transforms body to email request.""" |
|
58 self.receive(mail.InboundEmailMessage(self.request.body)) |
|
59 |
|
60 def receive(self, mail_message): |
|
61 """Receive an email message. |
|
62 |
|
63 Override this method to implement an email receiver. |
|
64 |
|
65 Args: |
|
66 mail_message: InboundEmailMessage instance representing received |
|
67 email. |
|
68 """ |
|
69 pass |
|
70 |
|
71 @classmethod |
|
72 def mapping(cls): |
|
73 """Convenience method to map handler class to application. |
|
74 |
|
75 Returns: |
|
76 Mapping from email URL to inbound mail handler class. |
|
77 """ |
|
78 return MAIL_HANDLER_URL_PATTERN, cls |