|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2015 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 | +Sample Google App Engine application that sends mail using Mailgun. |
| 19 | +""" |
| 20 | +from urllib import urlencode |
| 21 | + |
| 22 | +import httplib2 |
| 23 | +import webapp2 |
| 24 | + |
| 25 | + |
| 26 | +# Your Mailgun Domain Name |
| 27 | +MAILGUN_DOMAIN_NAME = 'your-mailgun-domain-name' |
| 28 | +# Your Mailgun API key |
| 29 | +MAILGUN_API_KEY = 'your-mailgun-api-key' |
| 30 | + |
| 31 | + |
| 32 | +# [START simple_message] |
| 33 | +def send_simple_message(recipient): |
| 34 | + http = httplib2.Http() |
| 35 | + http.add_credentials('api', MAILGUN_API_KEY) |
| 36 | + |
| 37 | + url = 'https://api.mailgun.net/v3/{}/messages'.format(MAILGUN_DOMAIN_NAME) |
| 38 | + data = { |
| 39 | + 'from': 'Example Sender <mailgun@{}>'.format(MAILGUN_DOMAIN_NAME), |
| 40 | + 'to': recipient, |
| 41 | + 'subject': 'This is an example email from Mailgun', |
| 42 | + 'text': 'Test message from Mailgun' |
| 43 | + } |
| 44 | + |
| 45 | + resp, content = http.request(url, 'POST', urlencode(data)) |
| 46 | + |
| 47 | + if resp.status != 200: |
| 48 | + raise RuntimeError( |
| 49 | + 'Mailgun API error: {} {}'.format(resp.status, content)) |
| 50 | +# [END simple_message] |
| 51 | + |
| 52 | + |
| 53 | +# [START complex_message] |
| 54 | +def send_complex_message(recipient): |
| 55 | + http = httplib2.Http() |
| 56 | + http.add_credentials('api', MAILGUN_API_KEY) |
| 57 | + |
| 58 | + url = 'https://api.mailgun.net/v3/{}/messages'.format(MAILGUN_DOMAIN_NAME) |
| 59 | + data = { |
| 60 | + 'from': 'Example Sender <mailgun@{}>'.format(MAILGUN_DOMAIN_NAME), |
| 61 | + 'to': recipient, |
| 62 | + 'subject': 'This is an example email from Mailgun', |
| 63 | + 'text': 'Test message from Mailgun', |
| 64 | + 'html': '<html>HTML <strong>version</strong> of the body</html>' |
| 65 | + } |
| 66 | + |
| 67 | + resp, content = http.request(url, 'POST', urlencode(data)) |
| 68 | + |
| 69 | + if resp.status != 200: |
| 70 | + raise RuntimeError( |
| 71 | + 'Mailgun API error: {} {}'.format(resp.status, content)) |
| 72 | +# [END complex_message] |
| 73 | + |
| 74 | + |
| 75 | +class MainPage(webapp2.RequestHandler): |
| 76 | + def get(self): |
| 77 | + self.response.content_type = 'text/html' |
| 78 | + self.response.write(""" |
| 79 | +<!doctype html> |
| 80 | +<html><body> |
| 81 | +<form method="POST"> |
| 82 | +<input type="text" name="recipient" placeholder="Enter recipient email"> |
| 83 | +<input type="submit" name="submit" value="Send simple email"> |
| 84 | +<input type="submit" name="submit" value="Send complex email"> |
| 85 | +</form> |
| 86 | +</body></html> |
| 87 | +""") |
| 88 | + |
| 89 | + def post(self): |
| 90 | + recipient = self.request.get('recipient') |
| 91 | + action = self.request.get('submit') |
| 92 | + |
| 93 | + if action == 'Send simple email': |
| 94 | + send_simple_message(recipient) |
| 95 | + else: |
| 96 | + send_complex_message(recipient) |
| 97 | + |
| 98 | + self.response.write('Mail sent') |
| 99 | + |
| 100 | + |
| 101 | +app = webapp2.WSGIApplication([ |
| 102 | + ('/', MainPage) |
| 103 | +], debug=True) |
0 commit comments