laravel5 sendgrid メール送信


新しい記事は
https://qiita.com/ma7ma7pipipi/items/12aec6efa95cfc6c7e5a

参考
https://qiita.com/ubonsa/items/5514fb9c5d5783bcc758
https://github.com/s-ichikawa/laravel-sendgrid-driver

追記、Sendgridメールが届かない場合。

何度やっても指定したアドレスにメールが届かない。
その場合は、sendgrid にログイン。

左側メニューの Activityfedd で送信状況を確認。
Dropになっていると送信されていない。

そこで、解決方法。

左側 メニューの Suppressions > Bounces。
この中にメールアドレスが入ってしまっていると Drop されて
送信すらされないので、

ここにもしアドレスが入っていたら削除する。
そして再度送信すると、また届くようになる。

toの値が有効にならないので。
以下を追記。

sendmail.php



        if(!empty($this->option['to'])){
            $res->to($this->option['to']['email'],$this->option['to']['name']);
        }

条件
Sendgridの設定が終わっていること。

インストール


composer require s-ichikawa/laravel-sendgrid-driver

.env

APP_NAME=テストへるぴー//要変更
[email protected]
MAIL_DRIVER=sendgrid
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'//要変更

設定

config/app.php

'providers' => [
   Sichikawa\LaravelSendgridDriver\SendgridTransportServiceProvider::class
];

config/services.php

    'sendgrid' => [
        'api_key' => env('SENDGRID_API_KEY'),//何も変更せずこのままコピペ
    ],

app.php

'email' => env('APP_EMAIL'),//追記

キャッシュクリア&ファイル生成


php artisan config:cache
php artisan make:mail TestMail

メール送信ファイルを作成

app/Mail/SendMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

use Sichikawa\LaravelSendgridDriver\SendGrid;


class SendMail extends Mailable
{
    use Queueable, SerializesModels;
    use SendGrid;//これ大事
    protected $data;//これ大事
    protected $option;//これ大事

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data,$option)
    {
        // 引数で受け取ったデータを変数にセット

        $this->option = $option;
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {


        if(!isset($this->option['from'])){

            $this->option['from'] = [
                'email' => config('app.email'),
                'name' => config('app.name')
            ];

        }

        return $this
            ->text($this->option['view'],$this->data)//text に変更した。これで改行が有効になる
            ->subject($this->option['subject'])
            ->from($this->option['from']['email'],$this->option['from']['name'])//ここは配列では無い!ので要注意。
            ->with(['data' => $this->data])

            //なんの値かわからんけど、必須
            ->sendgrid([
                'personalizations' => [
                    [
                        'substitutions' => [
                            ':myname' => 'hideki',
                        ],
                    ],
                ],
            ]);

    }

}


テンプレートファイルを作成

resources/views/email/contact.blade.php

テスト送信の本文
こんにちわ{{$name}}さん

送信してみる

Http/Controllers/HogeController.php

<?php

namespace App\Http\Controllers;
use App\Model;


use App\Mail\SendMail;
use Illuminate\Support\Facades\Mail;

class HogeController extends Controller
{

    public static function test()
    {


        $data = ['name' => 'はなこ'];

        Mail::to(config('app.email'))
            ->send(new SendMail($data,[
                'subject' => 'お問い合わせ',
                'from' => [
                    'email' => '[email protected]',
                    'name' => 'おくりもと'
                ],
                'to' => [
                    'email' => '[email protected]',
                    'name' => '送信先'
                ],
                'view' => 'email.contact'//views/email/contact.blade.php
            ]));

        die;

    }


}

ファイル添付版

SendMail.php

    public function build()
    {


        if(!isset($this->option['from'])){

            $this->option['from'] = [
                'email' => config('app.email'),
                'name' => config('app.name')
            ];

        }


        $res = $this
//            ->attach(public_path('/tmp/5e27a3b19b5e21579656113.jpg'))
            ->text($this->option['view'],$this->data)//text に変更した。これで改行が有効になる
            ->subject($this->option['subject'])
            ->from($this->option['from']['email'],$this->option['from']['name'])//ここは配列では無い!ので要注意。
            ->with(['data' => $this->data])

            //なんの値かわからんけど、必須
            ->sendgrid([
                'personalizations' => [
                    [
                        'substitutions' => [
                            ':myname' => 'hideki',
                        ],
                    ],
                ],
            ]);

        if(!empty($this->option['attach'])){
            foreach ($this->option['attach'] as $v) {
                $res->attach($v);
            }
        }


        return $res;








    }

呼び出し方法

Http/Controllers/HogeController.php

Mail::to($to)
            ->send(new SendMail($data,[
                'attach' => [
                    public_path('/tmp/5e27a3b19b5e21579656113.jpg')
                ],
                'from' => [
                    'email' => $data['email'],
                    'name' => $data['name']
                ],
                // 相手先の名前を入れるために、 ここに to で email と name の両方を入れておく
                'to' => [
                    'email' => $to,
                    'name' => 'テストへるぴー'
                ],
                'subject' => 'いいかなresukaesi再度添付ファイルも送信できるか',
                'view' => 'email.contact'//views/email/contact.blade.php
            ]));