-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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. #301
Add mail samples. #301
Changes from all commits
436ad4e
965794c
b08b35c
ebcfc25
1d26eae
cac95a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 --> |
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: | ||
# [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 |
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 | ||
""", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe move Attached to the next line? Looks really weird There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
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) |
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 |
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
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) |
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) |
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] |
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) |
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) |
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) |
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline between license and imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.