|
21 | 21 | // create the Silex application
|
22 | 22 | $app = new Application();
|
23 | 23 |
|
24 |
| -$app['sendgrid'] = function (Application $app) { |
25 |
| - return new SendGrid($app['sendgrid.api_key']); |
26 |
| -}; |
27 |
| - |
28 | 24 | $app->get('/', function () use ($app) {
|
29 | 25 | return <<
|
30 | 26 |
|
|
38 | 34 | });
|
39 | 35 |
|
40 | 36 | $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']; |
| 37 | + $sendgridSender = $app['sendgrid.sender']; |
| 38 | + $sendgridApiKey = $app['sendgrid.api_key']; |
| 39 | + $sendgridRecipient = $request->get('recipient'); |
| 40 | + # [START send_mail] |
| 41 | + // $sendgridApiKey = 'YOUR_SENDGRID_API_KEY'; |
| 42 | + // $sendgridSender = '[email protected]'; |
| 43 | + // $sendgridRecipient = '[email protected]'; |
| 44 | + $sender = new SendGrid\Email(null, $sendgridSender); |
| 45 | + $recipient = new SendGrid\Email(null, $sendgridRecipient); |
| 46 | + $subject = 'This is a test email'; |
| 47 | + $body = new SendGrid\Content('text/plain', 'Example text body.'); |
| 48 | + $mail = new SendGrid\Mail($sender, $subject, $recipient, $body); |
| 49 | + // send the email |
| 50 | + $sendgrid = new SendGrid($sendgridApiKey); |
49 | 51 | $response = $sendgrid->client->mail()->send()->post($mail);
|
| 52 | + # [END send_mail] |
50 | 53 | if ($response->statusCode() < 200 || $response->statusCode() >= 300) {
|
51 | 54 | return new Response($response->body(), $response->statusCode());
|
52 | 55 | }
|
|
0 commit comments