Created 04/11/2020 at 06:02PM

Billing report

Have you ever wondered how to build your own Mail API, similar to Mailgun or Mailjet?
Setting up a Mail API on the Google App Engine (GAE) might be the answer.
GAE offers a mail library as part of its PaaS solution out-of-the-box.

The library may be imported via:

from google.appengine.api import mail

In order to build a RESTful API out of this, we will use the Flask web framework and define the API endpoint rules. Let’s define 4 arguments that our API Endpoint accepts via POST requests, namely the sender address (from), the receiver addresses (to), the subject and the HTML body:

from flask import Flask, request
from flask.json import jsonify

from google.appengine.api import mail

app = Flask(__name__)

# define API endpoint /send
@app.route('/send', methods=['POST']) # GET requests will be blocked
def send():
    req_data = request.get_json()
    sender_address = req_data['from']
    to = req_data['to']
    subject = req_data['subject']
    body = req_data['html']
    mail.send_mail(sender=sender_address, to=to, subject=subject, body='', html=body)
    return 'Email sent.',200

Since the App Engine Mail API is currently only available within the App Engine Standard Python 2.7 SDK, we have to define the App Engine configuration file as follows:

runtime: python27
api_version: 1
threadsafe: true


- url: /_ah/queue/deferred
  script: google.appengine.ext.deferred.deferred.application
  login: admin 
- url: .*

Also, since we are dependent on Flask as our API web framework, we have to define a requirements.txt file:


Please note, App Engine Standard with the Python 2.7 SDK doesn’t automatically recognize the requirements, hence we have to create a lib folder containing the required libraries via:

pip install -r requirements.txt -t lib

These libraries are then included into our Flask App via an file:

from google.appengine.ext import vendor


Once all this is done, your folder structure on your GCE VM from which you’re deploying the Flask App should look like this:

Tree structure

Now change into your App directory and then run the following command to deploy your Flask App to GAE:

gcloud app deploy app.yaml

Congratulations! You just deployed your Mail API on GAE. To test out your API, send a POST request to your API endpoint. For example, the following Python code would send some training job results to your Mail API and notify you: (swap out the email address and your project_id)

import json
import requests

# Model training results
accuracy = 0.99
job_id = 'test_job_12345'

# Define email as Python dict for JSON post request
dict_payload = {"from": "John Doe <>",
                "to": "John Doe <>",
                "subject": "Model training results",
                "html": "Accuracy: {} <br> Job_ID: {}".format(accuracy,job_id)}

# Convert Python dict to JSON object
json_payload = json.loads(json.dumps(dict_payload))

# Send email with KPI's"",json=json_payload)

Finally, check your email address whether it worked. :)

Email report