{"id":14252,"date":"2021-07-29T10:32:31","date_gmt":"2021-07-29T01:32:31","guid":{"rendered":"https:\/\/sendgrid.kke.co.jp\/blog\/?p=14252"},"modified":"2022-06-16T11:41:37","modified_gmt":"2022-06-16T02:41:37","slug":"laravel%e3%81%a8twilio-sendgrid%e3%81%a7%e5%95%8f%e5%90%88%e3%81%9b%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/sendgrid.kke.co.jp\/blog\/?p=14252","title":{"rendered":"Laravel\u3068Twilio SendGrid\u3067\u554f\u5408\u305b\u30d5\u30a9\u30fc\u30e0\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2021\/07\/20210729_blog.png\" alt=\"Laravel\u3068Twilio SendGrid\u3067\u554f\u5408\u305b\u30d5\u30a9\u30fc\u30e0\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\" width=\"100%\" class=\"aligncenter size-full wp-image-15380\" srcset=\"https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2021\/07\/20210729_blog.png 650w, https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2021\/07\/20210729_blog-300x185.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/p>\n<blockquote>\n<p>\u3053\u306e\u8a18\u4e8b\u306f <a href=\"https:\/\/www.twilio.com\/blog\/create-email-contact-form-php-laravel-twilio-sendgrid\" target=\"_blank\">Create a Contact Form in PHP with Laravel and Twilio SendGrid<\/a> \u306e\u6284\u8a33\u3067\u3059\u3002<\/p>\n<\/blockquote>\n<p>\u72ec\u81ea\u306e\u30e1\u30fc\u30eb\u30b5\u30fc\u30d3\u30b9\u3092\u30bc\u30ed\u304b\u3089\u4f5c\u3063\u3066\u904b\u7528\u3059\u308b\u5834\u5408\u3001\u30b9\u30d1\u30e0\u5224\u5b9a\u3084\u30e6\u30fc\u30b6\u304c\u5897\u52a0\u3057\u305f\u5834\u5408\u306e\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0\u306a\u3069\u69d8\u3005\u306a\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3053\u3067\u3001<a href=\"https:\/\/sendgrid.kke.co.jp\/\" rel=\"noopener\" target=\"_blank\">Twilio SendGrid<\/a>\u306e\u3088\u3046\u306aAPI\u3092\u5229\u7528\u3059\u308b\u3068\u3001\u5230\u9054\u7387\u3084\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0\u306e\u554f\u984c\u306b\u5bb9\u6613\u306b\u5bfe\u51e6\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u8a18\u4e8b\u306f\u3001\u30e6\u30fc\u30b6\u306b\u30cb\u30e5\u30fc\u30b9\u30ec\u30bf\u30fc\u3092\u914d\u4fe1\u3057\u305f\u308a\u3001\u30b7\u30b9\u30c6\u30e0\u7ba1\u7406\u8005\u306b\u901a\u77e5\u30e1\u30fc\u30eb\u3092\u9001\u3063\u305f\u308a\u3001\u30e1\u30fc\u30eb\u306e\u9001\u4fe1\u7d50\u679c\u3092\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3057\u305f\u308a\u3059\u308b\u65b9\u6cd5\u306b\u8208\u5473\u304c\u3042\u308b\u65b9\u306b\u304a\u3059\u3059\u3081\u3067\u3059\u3002\u69d8\u3005\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3067\u30a8\u30f3\u30c9\u30e6\u30fc\u30b6\u5b9b\u306e\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u305f\u3081\u306b\u3001Twilio SendGrid\u3068<a href=\"https:\/\/laravel.com\/\" rel=\"noopener\" target=\"_blank\">Laravel<\/a>\u3092\u4f7f\u3063\u3066\u7c21\u5358\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002\u6700\u5f8c\u307e\u3067\u9032\u3081\u308b\u3068\u3001Laravel\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u7d44\u307f\u8fbc\u3080\u30e1\u30fc\u30eb\u30d5\u30a9\u30fc\u30e0\u304c\u5b8c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2021\/07\/210729_02.png\" alt=\"Laravel\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u7d44\u307f\u8fbc\u3080\u30e1\u30fc\u30eb\u30d5\u30a9\u30fc\u30e0\" width=\"100%\" class=\"aligncenter size-full wp-image-14254\" srcset=\"https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2021\/07\/210729_02.png 800w, https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2021\/07\/210729_02-300x202.png 300w, https:\/\/sendgrid.kke.co.jp\/blog\/wp\/wp-content\/uploads\/2021\/07\/210729_02-768x517.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<h3>\u5fc5\u8981\u306a\u3082\u306e<\/h3>\n<p>\u3053\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3067\u5fc5\u8981\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u30d1\u30c3\u30b1\u30fc\u30b8\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<table border>\n<tr>\n<td>\n\u30fbPHP 7.1+<br \/>\n\u30fb<a href=\"https:\/\/getcomposer.org\/\" rel=\"noopener\" target=\"_blank\">Composer<\/a>\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<br \/>\n\u30fb<a href=\"https:\/\/laravel.com\/\" rel=\"noopener\" target=\"_blank\">Laravel<\/a>\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<br \/>\n\u30fb<a href=\"https:\/\/sendgrid.kke.co.jp\/app?p=signup.index\" rel=\"noopener\" target=\"_blank\">SendGrid<\/a>\u30a2\u30ab\u30a6\u30f3\u30c8\n<\/td>\n<\/tr>\n<\/table>\n<p>Laravel\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u3089\u3001Laravel CLI\u3067\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u65b0\u898f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$ laravel new project_name\r\n<\/pre>\n<p>\u3082\u3057\u304f\u306f\u3001Composer\u306e <span style=\"background-color:#d3d3d3;\">create-project<\/span> \u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3082OK\u3067\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$ composer create-project --prefer-dist laravel\/laravel project_name\r\n<\/pre>\n<p>\u6b21\u306bSendGrid\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u4f9d\u5b58\u95a2\u4fc2\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306bComposer\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$ composer require sendgrid\/sendgrid\r\n<\/pre>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5b8c\u4e86\u3057\u305f\u3089\u3001 <span style=\"background-color:#d3d3d3;\">.env<\/span> \u30d5\u30a1\u30a4\u30eb\u306b\u30e1\u30fc\u30eb\u9001\u4fe1\u6642\u306e\u8a8d\u8a3c\u60c5\u5831\u3068\u3057\u3066\u4f7f\u3046API\u30ad\u30fc\u3092\u767b\u9332\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nSENDGRID_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXX\r\n<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>API\u30ad\u30fc\u306f<a href=\"https:\/\/app.sendgrid.com\/settings\/api_keys\" rel=\"noopener\" target=\"_blank\">\u3053\u3061\u3089<\/a>\u3067\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3053\u3053\u307e\u3067\u8a2d\u5b9a\u3067\u304d\u305f\u3089\u3001\u30e1\u30fc\u30eb\u9001\u4fe1\u6a5f\u80fd\u3092\u5b9f\u88c5\u3059\u308bEmailController\u3068\u3044\u3046\u540d\u524d\u306e<a href=\"https:\/\/laravel.com\/docs\/6.x\/controllers\" rel=\"noopener\" target=\"_blank\">controller<\/a>\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$ php artisan make:controller EmailController\r\n<\/pre>\n<p>Laravel\u306e\u5b9f\u88c5\u306b\u3042\u307e\u308a\u6163\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001\u3059\u3079\u3066\u306econtroller\u3092 <span style=\"background-color:#d3d3d3;\">app\/Http\/Controllers<\/span> \u76f4\u4e0b\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f5c\u6210\u3057\u305fcontroller\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nnamespace App\\Http\\Controllers;\r\n\r\nuse Illuminate\\Http\\Request;\r\nuse SendGrid;\r\n\r\nclass EmailController extends Controller\r\n{\r\n    \/\/\r\n}\r\n<\/pre>\n<h3>view\u306e\u4f5c\u6210<\/h3>\n<p>\u5b9f\u88c5\u3057\u305f\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u7d50\u679c\u3092\u78ba\u8a8d\u3059\u308b\u305f\u3081<a href=\"https:\/\/laravel.com\/docs\/6.x\/views\" rel=\"noopener\" target=\"_blank\">view<\/a>\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<span style=\"background-color:#d3d3d3;\">resources\/views\/<\/span> \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b <span style=\"background-color:#d3d3d3;\">send-email.blade.php<\/span> \u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!doctype html&gt;\r\n&lt;html lang=&quot;{{ str_replace('_', '-', app()-&gt;getLocale()) }}&quot;&gt;\r\n  &lt;head&gt;\r\n    &lt;!-- Required meta tags --&gt;\r\n    &lt;meta charset=&quot;utf-8&quot;&gt;\r\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;\r\n\r\n    &lt;!-- Bootstrap CSS --&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/css\/bootstrap.min.css&quot; integrity=&quot;sha384-ggOyR0iXCbMQv3Xipma34MD+dH\/1fQ784\/j6cY\/iJTQUOhcWr7x9JvoRxT2MZw1T&quot; crossorigin=&quot;anonymous&quot;&gt;\r\n\r\n    &lt;title&gt;Laravel + SendGrid&lt;\/title&gt;\r\n    &lt;style&gt;\r\n        form {\r\n            background-color: #fff;\r\n            width: 1000px;\r\n            float: center;\r\n            margin: auto;\r\n            margin-top: 100px;\r\n        }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;form method=&quot;post&quot; action=&quot;{{ route('send-email') }}&quot;&gt;\r\n        @csrf\r\n        @if ($errors-&gt;any())\r\n            &lt;div class=&quot;alert alert-danger&quot;&gt;\r\n                &lt;ul&gt;\r\n                    @foreach ($errors-&gt;all() as $error)\r\n                    &lt;li&gt;{{ $error }}&lt;\/li&gt;\r\n                    @endforeach\r\n                &lt;\/ul&gt;\r\n            &lt;\/div&gt;\r\n        @endif\r\n        &lt;div class=&quot;form-group row col-8&quot;&gt;\r\n            &lt;div class=&quot;form-group col-12&quot;&gt;\r\n                &lt;label for=&quot;email&quot;&gt;Email Address(es)&lt;\/label&gt;\r\n                &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;email&quot; id=&quot;email&quot; placeholder=&quot;Example: johndoe@email.com,JohnDoe@email.com, &quot; required&gt;\r\n                &lt;small id=&quot;email&quot; class=&quot;form-text text-muted&quot;&gt;Enter the comma separated email addresses&lt;\/small&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;form-group col-12&quot;&gt;\r\n                &lt;label for=&quot;subject&quot;&gt;Subject&lt;\/label&gt;\r\n                &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;subject&quot; id=&quot;subject&quot; placeholder=&quot;Email Subject&quot; required&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;form-group col-12&quot;&gt;\r\n                &lt;div class=&quot;form-group&quot;&gt;\r\n                    &lt;label for=&quot;emailBody&quot;&gt;Email Body&lt;\/label&gt;\r\n                    &lt;textarea class=&quot;form-control&quot; id=&quot;emailBody&quot; name=&quot;body&quot; rows=&quot;3&quot;&gt;&lt;\/textarea&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/div&gt;\r\n            &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;\/button&gt;\r\n        &lt;\/div&gt;\r\n    &lt;\/form&gt;\r\n\r\n    &lt;!-- Optional JavaScript --&gt;\r\n    &lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&gt;\r\n    &lt;script src=&quot;https:\/\/code.jquery.com\/jquery-3.3.1.slim.min.js&quot; integrity=&quot;sha384-q8i\/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.14.7\/umd\/popper.min.js&quot; integrity=&quot;sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/js\/bootstrap.min.js&quot; integrity=&quot;sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf\/nJGzIxFDsf4x0xIM+B07jRM&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h3>\u30e1\u30fc\u30eb\u9001\u4fe1\u51e6\u7406\u306e\u30c8\u30ea\u30ac<\/h3>\n<p>view\u3092\u4f5c\u6210\u3057\u305f\u3089\u3001\u6b21\u306b\u4efb\u610f\u306e\u30a2\u30c9\u30ec\u30b9\u5b9b\u3066\u306b\u30e1\u30fc\u30eb\u9001\u4fe1\u3059\u308b\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002 <span style=\"background-color:#d3d3d3;\">EmailController.php<\/span> \u306b\u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\npublic function sendEmail(Request $request) {\r\n    $this-&gt;validate($request, &#x5B;\r\n        'email' =&gt; 'required',\r\n        'subject' =&gt; 'required',\r\n        'body' =&gt; 'required',\r\n    ]);\r\n\r\n    $emails = $request-&gt;email;\r\n    $subject = $request-&gt;subject;\r\n    $body = $request-&gt;body;\r\n\r\n    $senderEmail = &quot;SENDER_EMAIL&quot;;\r\n    $senderName = &quot;SENDER_NAME&quot;;\r\n\r\n    \/** An array to store the status codes for all emails to have a record of all successful emails *\/\r\n    $emailReports = &#x5B;];\r\n\r\n    \/** Exploding the string to get the email addresses individually *\/\r\n    $addressesArray = explode(',', $emails);\r\n\r\n    for ($i = 0; $i &lt; count($addressesArray); $i++) {\r\n        $email = new SendGrid\\Mail\\Mail();\r\n        $email-&gt;setFrom($senderEmail, $senderName);\r\n        $email-&gt;setSubject($subject);\r\n        $email-&gt;addTo($addressesArray&#x5B;$i]);\r\n        $email-&gt;addContent(&quot;text\/plain&quot;, $body);\r\n        $sendgrid = new SendGrid(getenv('SENDGRID_API_KEY'));\r\n        try {\r\n            $response = $sendgrid-&gt;send($email);\r\n            \/** Push the email address and status code into the $emailReports array *\/\r\n            array_push($emailReports, $addressesArray&#x5B;$i] . &quot; =&gt; &quot; . $response-&gt;statusCode());\r\n        } catch (Exception $e) {\r\n            echo 'Caught exception: ',  $e-&gt;getMessage(), &quot;\\n&quot;;\r\n        }\r\n    }\r\n    return $emailReports;\r\n}\r\n<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u30b3\u30fc\u30c9\u5185\u306e\u201dSENDER_EMAIL\u201d\u3068\u201dSENDER_NAME\u201d\u306f\u9069\u5207\u306a\u3082\u306e\u306b\u7f6e\u304d\u63db\u3048\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u306f\u3001\u30e1\u30fc\u30eb\u9001\u4fe1\u7528controller\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059<a href=\"https:\/\/laravel.com\/docs\/5.8\/routing\" rel=\"noopener\" target=\"_blank\">\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0<\/a>\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u8a2d\u5b9a<\/h3>\n<p><span style=\"background-color:#d3d3d3;\">routes<\/span> \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e <span style=\"background-color:#d3d3d3;\">web.php<\/span> \u30d5\u30a1\u30a4\u30eb\u306b\u3001 <span style=\"background-color:#d3d3d3;\">sendEmail()<\/span> \u95a2\u6570\u306b\u51e6\u7406\u3092\u30ea\u30af\u30a8\u30b9\u30c8\u3057\u3001\u7d50\u679c\u3092view\u306b\u8fd4\u3059\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\nRoute::get('\/', 'EmailController@emailView')-&gt;name('email-view');\r\nRoute::post('\/send-email', 'EmailController@sendEmail')-&gt;name('send-email');\r\n<\/pre>\n<p>EmailController\u5168\u4f53\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\nuse Illuminate\\Http\\Request;\r\nuse SendGrid;\r\n\r\nclass EmailController extends Controller\r\n{\r\n\r\n    public function emailView() {\r\n        return view('send-email');\r\n    }\r\n\r\n    public function sendEmail(Request $request) {\r\n        $this-&gt;validate($request, &#x5B;\r\n            'email' =&gt; 'required',\r\n            'subject' =&gt; 'required',\r\n            'body' =&gt; 'required',\r\n        ]);\r\n\r\n        $emails = $request-&gt;email;\r\n        $subject = $request-&gt;subject;\r\n        $body = $request-&gt;body;\r\n\r\n        $senderEmail = &quot;SENDER_EMAIL&quot;;\r\n        $senderName = &quot;SENDER_NAME&quot;;\r\n\r\n        \/** An array to store the status codes for all emails to have a record of all successful emails *\/\r\n        $emailReports = &#x5B;];\r\n\r\n        \/** Exploding the string to get the email addresses individually *\/\r\n        $addressesArray = explode(',', $emails);\r\n\r\n        for ($i = 0; $i &lt; count($addressesArray); $i++) {\r\n            $email = new SendGrid\\Mail\\Mail();\r\n            $email-&gt;setFrom($senderEmail, $senderName);\r\n            $email-&gt;setSubject($subject);\r\n            $email-&gt;addTo($addressesArray&#x5B;$i]);\r\n            $email-&gt;addContent(&quot;text\/plain&quot;, $body);\r\n            $sendgrid = new SendGrid(getenv('SENDGRID_API_KEY'));\r\n            try {\r\n                $response = $sendgrid-&gt;send($email);\r\n                \/** Push the email address and status code into the $emailReports array *\/\r\n                array_push($emailReports, $addressesArray&#x5B;$i] . &quot; =&gt; &quot; . $response-&gt;statusCode());\r\n            } catch (Exception $e) {\r\n                echo 'Caught exception: ',  $e-&gt;getMessage(), &quot;\\n&quot;;\r\n            }\r\n        }\r\n        return $emailReports;\r\n    }\r\n}\r\n<\/pre>\n<h3>\u30c6\u30b9\u30c8<\/h3>\n<p>\u5b9f\u88c5\u304c\u5b8c\u4e86\u3057\u305f\u306e\u3067\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u52d5\u4f5c\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u30bf\u30fc\u30df\u30ca\u30eb\u3067\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3001\u30ed\u30fc\u30ab\u30eb\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$ php artisan serve\r\n<\/pre>\n<p>\u30d6\u30e9\u30a6\u30b6\u3067 <a href=\"http:\/\/localhost:8000\/\" rel=\"noopener\" target=\"_blank\">http:\/\/localhost:8000<\/a> \u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u30d5\u30a9\u30fc\u30e0\u306b\u5fc5\u8981\u4e8b\u9805\u3092\u5165\u529b\u3057\u3066submit( \u9001\u4fe1 )\u3057\u307e\u3059\u3002<\/p>\n<h3>\u7d42\u308f\u308a\u306b<\/h3>\n<p>\u672c\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u901a\u3057\u3066\u3001SendGrid\u3092\u4f7f\u3063\u305f\u30e1\u30fc\u30eb\u9001\u4fe1\u51e6\u7406\u3092\u7c21\u5358\u306b\u5b9f\u88c5\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u3053\u308c\u3092\u5fdc\u7528\u3059\u308b\u3068\u3001MySQL\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30e6\u30fc\u30b6\u60c5\u5831\u3092\u4fdd\u5b58\u3057\u305f\u308a\u3001\u72b6\u6cc1\u306b\u5fdc\u3058\u3066\u81ea\u52d5\u7684\u306b\u30e1\u30fc\u30eb\u9001\u4fe1\u3092\u30ea\u30c8\u30e9\u30a4\u3057\u305f\u308a\u3001\u30a8\u30f3\u30c9\u30e6\u30fc\u30b6\u306e\u30b0\u30eb\u30fc\u30d7\u306b\u5bfe\u3057\u3066\u4e00\u6589\u914d\u4fe1\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u304a\u8a66\u3057\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u8a18\u4e8b\u306f Create a Contact Form in PHP with Laravel and Twilio SendGrid \u306e\u6284\u8a33\u3067\u3059\u3002 \u72ec\u81ea\u306e\u30e1\u30fc\u30eb\u30b5\u30fc\u30d3\u30b9\u3092\u30bc\u30ed\u304b\u3089\u4f5c\u3063\u3066\u904b\u7528\u3059\u308b\u5834\u5408\u3001\u30b9\u30d1\u30e0\u5224\u5b9a\u3084\u30e6\u30fc\u30b6\u304c <a class=\"read-more\" href=\"https:\/\/sendgrid.kke.co.jp\/blog\/?p=14252\">&#8230;\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":15380,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-14252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14252"}],"version-history":[{"count":4,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14252\/revisions"}],"predecessor-version":[{"id":15381,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14252\/revisions\/15381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/media\/15380"}],"wp:attachment":[{"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sendgrid.kke.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}