下準備
- パッケージインストール
composer require "sendgrid/sendgrid"- .envにAPI keyを追加
SENDGRID_API_KEY={SendGridのAPI key}APIキーが未発行の場合はhttps://app.sendgrid.com/settings/api_keysから発行
- config/services.phpに追加
'sendgrid' => [
   'key' => env('SENDGRID_API_KEY'),
]これでconfig('services.sendgrid.key')で呼び出し可能になった
Controllerで使ってみる
- SendgridSampleController
 (ルーティングの設定は省略)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use SendGrid\Mail\Mail as SendGridMail;
use SendGrid;
class SendgridSampleController extends Controller
{
    /**
     * メール配信
     */
    public function send()
    {
         // SendGridへ登録
         $email = new SendGridMail();
         // From設定
         $email->setFrom(config('mail.from')['address'], config('mail.from')['name']);
         // タイトル設定
         $email->setSubject('配信メールのタイトルが入ります');
         // 送信先設定
         $tos = ['to@test.com', 'to2@test.com'];
         $email->addTos($tos);
         // 内容設定
         $email->addContent("text/plain", '内容が入ります。内容が入ります。');
         // APIキー設定
         $sendgrid = new SendGrid(config('services.sendgrid.key'));
         // 送信API実行
         $response = $sendgrid->send($email);
         // レスポンスチェック
         if ($response->statusCode() == Response::HTTP_ACCEPTED) {
           Log::info('Sendgrid登録完了');
         } else {
           Log::error('Sendgrid登録失敗');
         }
    }
}














 
	

LaravelをSendGridのAPIと連携する