|
| 1 | +<?php |
| 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 <<<EOF |
| 28 | +<!doctype html> |
| 29 | +<html><body> |
| 30 | +<form method="POST"> |
| 31 | +<input type="text" name="recipient" placeholder="Enter recipient email"> |
| 32 | +<input type="submit" name="submit"> |
| 33 | +</form> |
| 34 | +</body></html> |
| 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] |
0 commit comments