Skip to content

Commit 0f6b3cf

Browse files
committed
Merge pull request GoogleCloudPlatform#46 from GoogleCloudPlatform/add-mailjet
adds mailjet, fixes a few mailgun issues
2 parents 4097334 + 6589807 commit 0f6b3cf

File tree

13 files changed

+1216
-13
lines changed

13 files changed

+1216
-13
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ script:
6464
- composer install
6565
- phpunit test
6666
- popd
67+
# run mailjet tests
68+
- pushd appengine/standard/mailjet
69+
- composer install
70+
- phpunit test
71+
- popd
6772
# run cloudsql tests
6873
- pushd appengine/standard/cloudsql
6974
- composer install

appengine/standard/mailgun/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ This sample application demonstrates how to use [Mailgun with Google App Engine]
77
Before running this sample:
88

99
1. You will need a [Mailgun account](http://www.mailgun.com/google).
10-
2. Update the `MAILGUN_DOMAIN_NAME` and `MAILGUN_API_KEY` constants in `index.php`.
11-
You can use your account's sandbox domain.
10+
2. Update `MAILGUN_DOMAIN` and `MAILGUN_API_KEY` in `index.php` to match your
11+
Mailgun credentials. You can use your account's sandbox domain.
1212

1313
## Prerequisites
1414

appengine/standard/mailgun/app.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
$app = new Application();
2424

2525
$app->get('/', function () use ($app) {
26-
if ($app['mailgun.domain'] == 'YOUR_DOMAIN_NAME') {
27-
return 'set your mailgun domain and API key in web/index.php';
26+
if ($app['mailgun.domain'] == 'MAILGUN_DOMAIN_NAME') {
27+
return 'set your mailgun domain and API key in index.php';
2828
}
2929
return <<
3030

appengine/standard/mailgun/functions.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
22

33
use Mailgun\Mailgun;
4-
use Http\Adapter\Guzzle6\Client;
54

65
# [START simple_message]
76
function sendSimpleMessage($recipient, $mailgunDomain, $mailgunApiKey)
87
{
98
// Instantiate the client.
10-
$httpClient = new Client();
9+
$httpClient = new Http\Adapter\Guzzle6\Client();
1110
$mailgunClient = new Mailgun($mailgunApiKey, $httpClient);
1211

1312
// Make the call to the client.
@@ -24,7 +23,7 @@ function sendSimpleMessage($recipient, $mailgunDomain, $mailgunApiKey)
2423
function sendComplexMessage($recipient, $mailgunDomain, $mailgunApiKey, $cc = '[email protected]', $bcc = '[email protected]')
2524
{
2625
// Instantiate the client.
27-
$httpClient = new Client();
26+
$httpClient = new Http\Adapter\Guzzle6\Client();
2827
$mailgunClient = new Mailgun($mailgunApiKey, $httpClient);
2928
$fileAttachment = __DIR__ . '/attachment.txt';
3029

appengine/standard/mailgun/index.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717

1818
// Install composer dependencies with "composer install"
1919
// @see http://getcomposer.org for more information.
20-
require __DIR__ . '/../vendor/autoload.php';
20+
require __DIR__ . '/vendor/autoload.php';
2121

22-
$app = require __DIR__ . '/../src/app.php';
22+
$app = require __DIR__ . '/app.php';
2323

2424
// set your Mailgun domain name and API key
25-
$app['mailgun.domain'] = 'YOUR_DOMAIN_NAME';;
26-
$app['mailgun.api_key'] = 'YOUR_API_KEY';
25+
$app['mailgun.domain'] = 'MAILGUN_DOMAIN_NAME';;
26+
$app['mailgun.api_key'] = 'MAILGUN_API_KEY';
2727

2828
// Run the app!
2929
// use "gcloud preview app deploy" or run "php -S localhost:8000"
30-
// and browse to "mailgun.php"
3130
$app['debug'] = true;
3231
$app->run();

appengine/standard/mailgun/test/mailgunTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function createApplication()
3535
$mailgunApiKey = getenv('MAILGUN_APIKEY');
3636

3737
if (empty($mailgunDomain) || empty($mailgunApiKey)) {
38-
$this->markTestSkipped('set the GOOGLE_PROJECT_ID, MAILGUN_DOMAIN and MAILGUN_APIKEY environment variables');
38+
$this->markTestSkipped('set the MAILGUN_DOMAIN and MAILGUN_APIKEY environment variables');
3939
}
4040

4141
$app['mailgun.domain'] = $mailgunDomain;

appengine/standard/mailjet/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Mailjet & Google App Engine
2+
3+
This sample application demonstrates how to use [Mailjet with Google App Engine](https://cloud.google.com/appengine/docs/php/mail/).
4+
5+
## Setup
6+
7+
Before running this sample:
8+
9+
1. You will need a [Mailjet account](http://www.mailjet.com).
10+
2. Update `MAILJET_API_KEY` and `MAILJET_SECRET` in `index.php` to match your
11+
Mailjet credentials.
12+
13+
## Prerequisites
14+
15+
- Install [`composer`](https://getcomposer.org)
16+
- Install dependencies by running:
17+
18+
```sh
19+
composer install
20+
```
21+
22+
## Run locally
23+
24+
you can run locally using PHP's built-in web server:
25+
26+
```sh
27+
cd php-docs-samples/appengine/standard/mailjet
28+
php -S localhost:8080
29+
```
30+
31+
Now you can view the app running at [http://localhost:8080](http://localhost:8080)
32+
in your browser.
33+
34+
## Deploy to App Engine
35+
36+
**Prerequisites**
37+
38+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
39+
40+
**Deploy with gcloud**
41+
42+
```
43+
gcloud config set project YOUR_PROJECT_ID
44+
gcloud preview app deploy
45+
gcloud preview app browse
46+
```
47+
48+
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
49+
in your browser.

appengine/standard/mailjet/app.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
18+
use Silex\Application;
19+
20+
// create the Silex application
21+
$app = new Application();
22+
23+
$app->get('/', function () use ($app) {
24+
/** @var Mailjet\Client $mailjet */
25+
$mailjet = $app['mailjet'];
26+
return <<
27+
28+
29+
30+
31+
32+
33+
34+
EOF;
35+
});
36+
37+
$app->post('/send', function () use ($app) {
38+
/** @var Symfony\Component\HttpFoundation\Request $request */
39+
$request = $app['request'];
40+
/** @var Mailjet\Client $mailjet */
41+
$mailjet = $app['mailjet'];
42+
$recipient = $request->get('recipient');
43+
44+
# [START send_email]
45+
$body = [
46+
'FromEmail' => "[email protected]",
47+
'FromName' => "Testing Mailjet",
48+
'Subject' => "Your email flight plan!",
49+
'Text-part' => "Dear passenger, welcome to Mailjet! May the delivery force be with you!",
50+
'Html-part' => "

Dear passenger, welcome to Mailjet!


May the delivery force be with you!
"
,
51+
'Recipients' => [
52+
[
53+
'Email' => $recipient,
54+
]
55+
]
56+
];
57+
58+
// trigger the API call
59+
$response = $mailjet->post(Mailjet\Resources::$Email, ['body' => $body]);
60+
if ($response->success()) {
61+
// if the call succed, data will go here
62+
return sprintf(
63+
'
%s
'
,
64+
json_encode($response->getData(), JSON_PRETTY_PRINT)
65+
);
66+
}
67+
68+
return 'Error: ' . print_r($response->getStatus(), true);
69+
# [END send_email]
70+
});
71+
72+
$app['mailjet'] = function () use ($app) {
73+
if ($app['mailjet.api_key'] == 'MAILJET_API_KEY') {
74+
return 'set your mailjet api key and secret in index.php';
75+
}
76+
$mailjetApiKey = $app['mailjet.api_key'];
77+
$mailjetSecret = $app['mailjet.secret'];
78+
79+
# [START mailjet_client]
80+
$mailjet = new Mailjet\Client($mailjetApiKey, $mailjetSecret);
81+
# [END mailjet_client]
82+
83+
return $mailjet;
84+
};
85+
86+
return $app;

appengine/standard/mailjet/app.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: php55
2+
threadsafe: yes
3+
api_version: 1
4+
5+
handlers:
6+
- url: .*
7+
script: index.php
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"mailjet/mailjet-apiv3-php": "^1.1"
5+
},
6+
"require-dev": {
7+
"symfony/browser-kit": "^3.0"
8+
}
9+
}

0 commit comments

Comments
 (0)