|
| 1 | +<?php |
| 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 <<<EOF |
| 27 | +<!doctype html> |
| 28 | +<html><body> |
| 29 | +<form method="POST" target="/send"> |
| 30 | +<input type="text" name="recipient" placeholder="Enter recipient email"> |
| 31 | +<input type="submit" name="submit" value="Send email"> |
| 32 | +</form> |
| 33 | +</body></html> |
| 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' => "<h3>Dear passenger, welcome to Mailjet!</h3><br/>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 | + '<pre>%s</pre>', |
| 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 <code>index.php</code>'; |
| 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; |
0 commit comments