Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mail samples. #299

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -->
60 changes: 60 additions & 0 deletions appengine/mail/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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.
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
48 changes: 48 additions & 0 deletions appengine/mail/attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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=('%s@appspot.gserviceaccount.com' %
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 %s to Albert.' % 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)
25 changes: 25 additions & 0 deletions appengine/mail/attachment_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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
28 changes: 28 additions & 0 deletions appengine/mail/handle_bounced_email.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.
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)
23 changes: 23 additions & 0 deletions appengine/mail/handle_bounced_email_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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)
16 changes: 16 additions & 0 deletions appengine/mail/handle_catchall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

# app.yaml defines that all email is handled by handle_incoming_email.py,
# so nothing to do here.
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]
27 changes: 27 additions & 0 deletions appengine/mail/handle_incoming_email_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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)
16 changes: 16 additions & 0 deletions appengine/mail/handle_owner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

# app.yaml defines that all email is handled by handle_incoming_email.py,
# so nothing to do here.
16 changes: 16 additions & 0 deletions appengine/mail/handle_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

# app.yaml defines that all email is handled by handle_incoming_email.py,
# so nothing to do here.
54 changes: 54 additions & 0 deletions appengine/mail/header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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('%s@appspot.gserviceaccount.com' %
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 %s.' % id)


app = webapp2.WSGIApplication([
('/header', SendMailHandler),
], debug=True)
25 changes: 25 additions & 0 deletions appengine/mail/header_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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)
Loading