Skip to content

Commit e7e6525

Browse files
authored
adds sendgrid sample for PHP 55 (GoogleCloudPlatform#914)
1 parent d2f0568 commit e7e6525

File tree

8 files changed

+249
-0
lines changed

8 files changed

+249
-0
lines changed

appengine/php55/sendgrid/README.md

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

appengine/php55/sendgrid/app.php

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

appengine/php55/sendgrid/app.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
runtime: php55
2+
threadsafe: yes
3+
api_version: 1
4+
5+
handlers:
6+
- url: .*
7+
script: index.php
8+
9+
# [START gae_sendgrid_yaml]
10+
env_variables:
11+
SENDGRID_APIKEY: your-sendgrid-api-key
12+
SENDGRID_SENDER: your-sendgrid-sender
13+
# [END gae_sendgrid_yaml]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"sendgrid/sendgrid": "5.0.4"
5+
},
6+
"require-dev": {
7+
"paragonie/random_compat": "^2.0",
8+
"symfony/yaml": "^3.1"
9+
}
10+
}

appengine/php55/sendgrid/index.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
/**
3+
* Copyright 2016 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+
// Install composer dependencies with "composer install"
19+
// @see http://getcomposer.org for more information.
20+
require __DIR__ . '/vendor/autoload.php';
21+
22+
$app = require __DIR__ . '/app.php';
23+
24+
$app['sendgrid.sender'] = getenv('SENDGRID_SENDER');
25+
$app['sendgrid.api_key'] = getenv('SENDGRID_APIKEY');
26+
27+
// Run the app!
28+
// use "gcloud app deploy" or run "php -S localhost:8000"
29+
$app['debug'] = true;
30+
$app->run();

appengine/php55/sendgrid/php.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extension=curl.so
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../../../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="PHP App Engine Standard for PHP 5.5 Sendgrid test">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<file>app.phpfile>
29+
whitelist>
30+
filter>
31+
phpunit>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* Copyright 2016 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+
namespace Google\Cloud\Samples\sendgrid\test;
18+
19+
use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
20+
use Google\Cloud\TestUtils\FileUtil;
21+
use Symfony\Component\Yaml\Yaml;
22+
23+
class DeployTest extends \PHPUnit_Framework_TestCase
24+
{
25+
use AppEngineDeploymentTrait;
26+
27+
public static function beforeDeploy()
28+
{
29+
$tmpDir = FileUtil::cloneDirectoryIntoTmp(__DIR__ . '/..');
30+
self::$gcloudWrapper->setDir($tmpDir);
31+
chdir($tmpDir);
32+
33+
$appYaml = Yaml::parse(file_get_contents('app.yaml'));
34+
$appYaml['env_variables']['SENDGRID_APIKEY'] =
35+
getenv('SENDGRID_APIKEY');
36+
$appYaml['env_variables']['SENDGRID_SENDER'] =
37+
getenv('SENDGRID_SENDER');
38+
file_put_contents('app.yaml', Yaml::dump($appYaml));
39+
}
40+
41+
public function testIndex()
42+
{
43+
// Access the modules app top page.
44+
$resp = $this->client->get('/');
45+
$this->assertEquals('200', $resp->getStatusCode(),
46+
'top page status code');
47+
}
48+
49+
public function testSendMessage()
50+
{
51+
$resp = $this->client->request('POST', '/', [
52+
'form_params' => [
53+
'recipient' => '[email protected]',
54+
]
55+
]);
56+
57+
$this->assertEquals('200', $resp->getStatusCode(),
58+
'send message status code');
59+
}
60+
}

0 commit comments

Comments
 (0)