Skip to content
Merged
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
43 changes: 43 additions & 0 deletions appengine/flexible/sendgrid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Sendgrid and Google App Engine Flexible Environment

This sample application demonstrates how to use [Sendgrid with Google App Engine Flexible Environment](https://cloud.google.com/appengine/docs/flexible/php/sending-emails-with-sendgrid).

## Setup

Before running this sample:

1. You will need a [SendGrid account](http://sendgrid.com/partner/google).
2. Update `SENDGRID_SENDER` and `SENDGRID_API_KEY` in `app.yaml` to match your
SendGrid credentials. You can use your account's sandbox domain.

## Prerequisites

- Install [`composer`](https://getcomposer.org)
- Install dependencies by running:

```sh
composer install
```

## Deploy to App Engine

**Prerequisites**

- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).

**Run Locally**
```sh
export SENDGRID_API_KEY=your-sendgrid-api-key
export [email protected]
php -S localhost:8000 -t .
```

**Deploy with gcloud**
```
gcloud config set project YOUR_PROJECT_ID
gcloud preview app deploy
gcloud preview app browse
```

The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
in your browser.
56 changes: 56 additions & 0 deletions appengine/flexible/sendgrid/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright 2015 Google Inc.
*
* 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.
*/
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// create the Silex application
$app = new Application();

$app['sendgrid'] = function (Application $app) {
return new SendGrid($app['sendgrid.api_key']);
};

$app->get('/', function () use ($app) {
return <<<EOF
<!doctype html>
<html><body>
<form method="POST">
<input type="text" name="recipient" placeholder="Enter recipient email">
<input type="submit" name="submit">
</form>
</body></html>
EOF;
});

$app->post('/', function (Request $request) use ($app) {
$mail = new SendGrid\Mail(
new SendGrid\Email(null, $app['sendgrid.sender']),
'This is a test email',
new SendGrid\Email(null, $request->get('recipient')),
new SendGrid\Content('text/plain', 'Example text body.')
);
/** @var SendGrid $sendgrid */
$sendgrid = $app['sendgrid'];
$response = $sendgrid->client->mail()->send()->post($mail);
if ($response->statusCode() < 200 || $response->statusCode() >= 300) {
return new Response($response->body(), $response->statusCode());
}
return 'Email sent.';
});

return $app;
9 changes: 9 additions & 0 deletions appengine/flexible/sendgrid/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
runtime: php
vm: true

runtime_config:
document_root: .

env_variables:
SENDGRID_API_KEY: your-sendgrid-api-key
SENDGRID_SENDER: your-sendgrid-sender
12 changes: 12 additions & 0 deletions appengine/flexible/sendgrid/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"require": {
"silex/silex": "^1.3",
"sendgrid/sendgrid": "^5.0"
},
"require-dev": {
"symfony/browser-kit": "^3.0",
"google/cloud-tools": "^0.3.0",
"paragonie/random_compat": "^2.0",
"symfony/yaml": "^3.1"
}
}
Loading