Skip to content

Commit

Permalink
Merge pull request #301 from GoogleCloudPlatform/mergeMail
Browse files Browse the repository at this point in the history
Add mail samples.
  • Loading branch information
SurferJeffAtGoogle committed Apr 28, 2016
2 parents 9a08ede + cac95a7 commit ad51b4d
Show file tree
Hide file tree
Showing 18 changed files with 655 additions and 0 deletions.
Empty file added appengine/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions appengine/mail/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## App Engine Email Docs Snippets

This sample application demonstrates different ways to send and receive email
on App Engine


<!-- auto-doc-link --><!-- end-auto-doc-link -->
47 changes: 47 additions & 0 deletions appengine/mail/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
runtime: python27
api_version: 1
threadsafe: yes

# [START bounce_service]
# [START mail_service]
inbound_services:
- mail
# [END mail_service]
- mail_bounce
# [END bounce_service]

handlers:
- url: /user/.+
script: user_signup.app
- url: /send_mail
script: send_mail.app
- url: /send_message
script: send_message.app
# [START handle_incoming_email]
- url: /_ah/mail/.+
script: handle_incoming_email.app
login: admin
# [END handle_incoming_email]
# [START handle_all_email]
- url: /_ah/mail/owner@.*your_app_id\.appspotmail\.com
script: handle_owner.app
login: admin
- url: /_ah/mail/support@.*your_app_id\.appspotmail\.com
script: handle_support.app
login: admin
- url: /_ah/mail/.+
script: handle_catchall.app
login: admin
# [END handle_all_email]
# [START handle_bounced_email]
- url: /_ah/bounce
script: handle_bounced_email.app
login: admin
# [END handle_bounced_email]
- url: /attachment
script: attachment.app
- url: /header
script: header.app
- url: /
static_files: index.html
upload: index.html
50 changes: 50 additions & 0 deletions appengine/mail/attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.api import app_identity
from google.appengine.api import mail
import webapp2


# [START send_attachment]
class AttachmentHandler(webapp2.RequestHandler):
def post(self):
f = self.request.POST['file']
mail.send_mail(sender='{}@appspot.gserviceaccount.com'.format(
app_identity.get_application_id()),
to="Albert Johnson <Albert.Johnson@example.com>",
subject="The doc you requested",
body="""
Attached is the document file you requested.
The example.com Team
""",
attachments=[(f.filename, f.file.read())])
# [END send_attachment]
self.response.content_type = 'text/plain'
self.response.write('Sent {} to Albert.'.format(f.filename))

def get(self):
self.response.content_type = 'text/html'
self.response.write("""<html><body>
<form method="post" enctype="multipart/form-data">
Send a file to Albert:<br />
<input type="file" name="file"><br /><br />
<input type="submit" name="submit" value="Submit">
</form></body></html""")


app = webapp2.WSGIApplication([
('/attachment', AttachmentHandler),
], debug=True)
26 changes: 26 additions & 0 deletions appengine/mail/attachment_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import attachment
import webtest


def test_send_mail(testbed):
testbed.init_mail_stub()
testbed.init_app_identity_stub()
app = webtest.TestApp(attachment.app)
response = app.post('/attachment', upload_files=[
('file', 'hello.txt', 'Good day!')])
assert response.status_int == 200
assert 'Sent hello.txt to Albert.' in response.body
29 changes: 29 additions & 0 deletions appengine/mail/handle_bounced_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from google.appengine.ext.webapp.mail_handlers import BounceNotificationHandler
import webapp2


# [START bounce_handler]
class LogBounceHandler(BounceNotificationHandler):
def receive(self, bounce_message):
logging.info('Received bounce post ... [%s]', self.request)
logging.info('Bounce original: %s', bounce_message.original)
logging.info('Bounce notification: %s', bounce_message.notification)
# [END bounce_handler]

app = webapp2.WSGIApplication([LogBounceHandler.mapping()], debug=True)
24 changes: 24 additions & 0 deletions appengine/mail/handle_bounced_email_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.ext.webapp.mail_handlers import BounceNotification

import handle_bounced_email


def test_handle_bounced_email(testbed):
handler = handle_bounced_email.LogBounceHandler()
handler.request = 'request'
bounced_message = BounceNotification({})
handler.receive(bounced_message)
41 changes: 41 additions & 0 deletions appengine/mail/handle_incoming_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START log_sender_handler]
import logging

from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
import webapp2


class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
logging.info("Received a message from: " + mail_message.sender)
# [END log_sender_handler]
# [START bodies]
plaintext_bodies = mail_message.bodies('text/plain')
html_bodies = mail_message.bodies('text/html')

for content_type, body in html_bodies:
decoded_html = body.decode()
# ...
# [END bodies]
logging.info("Html body of length %d.", len(decoded_html))
for content_type, body in plaintext_bodies:
plaintext = body.decode()
logging.info("Plain text body of length %d.", len(plaintext))

# [START app]
app = webapp2.WSGIApplication([LogSenderHandler.mapping()], debug=True)
# [END app]
28 changes: 28 additions & 0 deletions appengine/mail/handle_incoming_email_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.api import mail

import handle_incoming_email


def test_handle_bounced_email(testbed):
handler = handle_incoming_email.LogSenderHandler()
handler.request = 'request'
message = mail.EmailMessage(
sender='support@example.com',
subject='Your account has been approved')
message.to = 'Albert Johnson <Albert.Johnson@example.com>'
message.body = 'Dear Albert.'
handler.receive(message)
55 changes: 55 additions & 0 deletions appengine/mail/header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.api import app_identity
from google.appengine.api import mail
import webapp2


def send_example_mail(sender_address, email_thread_id):
# [BEGIN send_mail]
mail.send_mail(sender=sender_address,
to="Albert Johnson <Albert.Johnson@example.com>",
subject="An example email",
body="""
The email references a given email thread id.
The example.com Team
""",
headers={"References": email_thread_id})
# [SEND send_mail]


class SendMailHandler(webapp2.RequestHandler):
def get(self):
self.response.content_type = 'text/html'
self.response.write("""<html><body><form method="POST">
Enter an email thread id: <input name="thread_id">
<input type=submit>
</form></body></html>""")

def post(self):
print repr(self.request.POST)
id = self.request.POST['thread_id']
send_example_mail('{}@appspot.gserviceaccount.com'.format(
app_identity.get_application_id()), id)
self.response.content_type = 'text/plain'
self.response.write(
'Sent an email to Albert with Reference header set to {}.'
.format(id))


app = webapp2.WSGIApplication([
('/header', SendMailHandler),
], debug=True)
26 changes: 26 additions & 0 deletions appengine/mail/header_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import header
import webtest


def test_send_mail(testbed):
testbed.init_mail_stub()
testbed.init_app_identity_stub()
app = webtest.TestApp(header.app)
response = app.post('/header', 'thread_id=42')
assert response.status_int == 200
assert ('Sent an email to Albert with Reference header set to 42.'
in response.body)
29 changes: 29 additions & 0 deletions appengine/mail/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<!--
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Google App Engine Mail Samples</title>
</head>
<body>
<p><a href="send_mail">Send email.</a></p>
<p><a href="send_message">Send email with a message object.</a></p>
<p><a href="user/signup">Confirm a user's email address.</a></p>
<p><a href="attachment">Send email with attachments.</a></p>
<p><a href="header">Send email with headers.</a></p>
</body>
</html>
Loading

0 comments on commit ad51b4d

Please sign in to comment.