Skip to content

Commit 72abbd2

Browse files
Merge pull request GoogleCloudPlatform#126 from GoogleCloudPlatform/squashSendgrid
App Engine Flex Sendgrid sample.
2 parents cbdbdcf + b260a3c commit 72abbd2

File tree

11 files changed

+2556
-0
lines changed

11 files changed

+2556
-0
lines changed

appengine/flexible/sendgrid/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Sendgrid and Google App Engine Flexible Environment
2+
3+
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).
4+
5+
## Setup
6+
7+
Before running this sample:
8+
9+
1. You will need a [SendGrid account](http://sendgrid.com/partner/google).
10+
2. Update `SENDGRID_SENDER` and `SENDGRID_API_KEY` in `app.yaml` to match your
11+
SendGrid credentials. You can use your account's sandbox domain.
12+
13+
## Prerequisites
14+
15+
- Install [`composer`](https://getcomposer.org)
16+
- Install dependencies by running:
17+
18+
```sh
19+
composer install
20+
```
21+
22+
## Deploy to App Engine
23+
24+
**Prerequisites**
25+
26+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
27+
28+
**Run Locally**
29+
```sh
30+
export SENDGRID_API_KEY=your-sendgrid-api-key
31+
32+
php -S localhost:8000 -t .
33+
```
34+
35+
**Deploy with gcloud**
36+
```
37+
gcloud config set project YOUR_PROJECT_ID
38+
gcloud preview app deploy
39+
gcloud preview app browse
40+
```
41+
42+
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
43+
in your browser.

appengine/flexible/sendgrid/app.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
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+
use Silex\Application;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\Response;
20+
21+
// create the Silex application
22+
$app = new Application();
23+
24+
$app['sendgrid'] = function (Application $app) {
25+
return new SendGrid($app['sendgrid.api_key']);
26+
};
27+
28+
$app->get('/', function () use ($app) {
29+
return <<
30+
31+
32+
33+
34+
35+
36+
37+
EOF;
38+
});
39+
40+
$app->post('/', function (Request $request) use ($app) {
41+
$mail = new SendGrid\Mail(
42+
new SendGrid\Email(null, $app['sendgrid.sender']),
43+
'This is a test email',
44+
new SendGrid\Email(null, $request->get('recipient')),
45+
new SendGrid\Content('text/plain', 'Example text body.')
46+
);
47+
/** @var SendGrid $sendgrid */
48+
$sendgrid = $app['sendgrid'];
49+
$response = $sendgrid->client->mail()->send()->post($mail);
50+
if ($response->statusCode() < 200 || $response->statusCode() >= 300) {
51+
return new Response($response->body(), $response->statusCode());
52+
}
53+
return 'Email sent.';
54+
});
55+
56+
return $app;

appengine/flexible/sendgrid/app.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
runtime: php
2+
vm: true
3+
4+
runtime_config:
5+
document_root: .
6+
7+
env_variables:
8+
SENDGRID_API_KEY: your-sendgrid-api-key
9+
SENDGRID_SENDER: your-sendgrid-sender
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"sendgrid/sendgrid": "^5.0"
5+
},
6+
"require-dev": {
7+
"symfony/browser-kit": "^3.0",
8+
"google/cloud-tools": "^0.3.0",
9+
"paragonie/random_compat": "^2.0",
10+
"symfony/yaml": "^3.1"
11+
}
12+
}

0 commit comments

Comments
 (0)