Sending Emails with SendGrid

You can use SendGrid to power your emails on Google App Engine. SendGrid can improve your deliverability and provide transparency into what actually happens to those emails your app sends. You can see statistics on opens, clicks, unsubscribes, spam reports and more with the SendGrid interface or its API.

Pricing

Google App Engine customers can send 12,000 emails every month for free by signing up for the SendGrid Email API plan through the Google Cloud Platform Console. Note that Google is compensated for customers who sign up for a paid account.

SendGrid libraries

You can send email with SendGrid through an SMTP relay or using a Web API.

To integrate SendGrid with your App Engine project, use the SendGrid client libraries.

Setting up

To use SendGrid to send an email:

  1. Select or create a new GCP project in the Google Cloud Platform Console and then ensure an App Engine application exists and billing is enabled:

    Go to App Engine

    The Dashboard opens if an App Engine application already exists in your project and billing is enabled. Otherwise, follow the prompts for choosing a region and enabling billing.
  2. Sign up for SendGrid Email API plan
  3. Add your SendGrid settings to the environment variables section in app.yaml.

    For example, for the sample code below add:

    env_variables:
        SENDGRID_API_KEY: your-sendgrid-api-key
        SENDGRID_SENDER: your-sendgrid-sender

  4. Add the SendGrid Python library to your application's requirements.txt. For example:
    Flask==0.12.2
    sendgrid==5.3.0
    gunicorn==19.7.1
    

Example

You can create a SendGrid instance and use it to send mail. The following sample code shows how to send an email and specifies some error handling:

@app.route('/send/email', methods=['POST'])
def send_email():
    to = request.form.get('to')
    if not to:
        return ('Please provide an email address in the "to" query string '
                'parameter.'), 400

    sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)

    to_email = mail.Email(to)
    from_email = mail.Email(SENDGRID_SENDER)
    subject = 'This is a test email'
    content = mail.Content('text/plain', 'Example message.')
    message = mail.Mail(from_email, subject, to_email, content)

    response = sg.client.mail.send.post(request_body=message.get())

    if response.status_code != 202:
        return 'An error occurred: {}'.format(response.body), 500

    return 'Email sent.'

Add your own account details, and then edit the email address and other message content.

For more email settings and examples, see the SendGrid-Python library.

Testing and Deploying

Before running your app locally, you need to set the environment variables on your local machine using the command line. For example, for the sample code above:

export SENDGRID_API_KEY= your-sendgrid-api-key
export SENDGRID_SENDER= your-sendgrid-sender-email-address

You can run the application locally to test the callbacks and SMS transmission. For example:

python main.py

After testingyour application, deploy your project to App Engine:

gcloud app deploy

Getting real-time information

In addition to sending email, SendGrid can receive email or make sense of the email you’ve already sent using webhooks.

Event API

Once you start sending email from your application, you can view statistics collected by SendGrid to assess your email program. You can use the Event API to see this data. For example, whenever a recipient opens or clicks an email, SendGrid can send a descriptive JSON to your Google App Engine app that can react to the event or store the data for future use.

The Event API documentation shows how to set up the webhook, outlines the nine event types and shows the fields included in event callbacks.

Inbound Parse API

SendGrid can receive email. The Inbound Parse API can be used for interactive applications, such as automating support tickets.

The Parse API is a webhook that sends data to your application when something new is available. In this case, the webhook is called whenever a new email arrives at the domain you've associated with incoming email.

Emails are sent to the application structured as JSON, with sender, recipients, subject, and body as different fields. Attachments of up to 20MB are allowed.

The Parse API documentation has more details, including additional fields sent with every email, as well as instructions for DNS setup and usage.

Was this page helpful? Let us know how we did:

Send feedback about...

App Engine flexible environment for Python docs