|
| 1 | +# Copyright 2016 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from google.appengine.api import app_identity |
| 15 | +from google.appengine.api import mail |
| 16 | +import webapp2 |
| 17 | + |
| 18 | + |
| 19 | +# [START send_attachment] |
| 20 | +class AttachmentHandler(webapp2.RequestHandler): |
| 21 | + def post(self): |
| 22 | + f = self.request.POST['file'] |
| 23 | + mail. send_mail( sender=( '%[email protected]' % |
| 24 | + app_identity.get_application_id()), |
| 25 | + to="Albert Johnson <[email protected]>", |
| 26 | + subject="The doc you requested", |
| 27 | + body="""Attached is the document file you requested. |
| 28 | +
|
| 29 | +The example.com Team |
| 30 | +""", |
| 31 | + attachments=[(f.filename, f.file.read())]) |
| 32 | +# [END send_attachment] |
| 33 | + self.response.content_type = 'text/plain' |
| 34 | + self.response.write('Sent %s to Albert.' % f.filename) |
| 35 | + |
| 36 | + def get(self): |
| 37 | + self.response.content_type = 'text/html' |
| 38 | + self.response.write("""<html><body> |
| 39 | + <form method="post" enctype="multipart/form-data"> |
| 40 | + Send a file to Albert:<br /> |
| 41 | + <input type="file" name="file"><br /><br /> |
| 42 | + <input type="submit" name="submit" value="Submit"> |
| 43 | + </form></body></html""") |
| 44 | + |
| 45 | + |
| 46 | +app = webapp2.WSGIApplication([ |
| 47 | + ('/attachment', AttachmentHandler), |
| 48 | +], debug=True) |
0 commit comments