1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelでメンバー招待メール機能を実装

1
Posted at

実装したい内容

ユーザーをメール経由で招待したい。

Model

メールモデルを作成する。

.bash
php artisan make:mail InviteMail
InviteMail.php
class InviteMail extends Mailable
{
    use Queueable, SerializesModels;

    //コントローラで定義した変数を引き継ぐ
    public function __construct($invite_url, $companyName)
    {
        $this->invite_url = $invite_url;
        $this->companyName = $companyName;
    }

    public function build()
    {
        return $this->subject('招待メール')
        ->view('emails.test')
        ->with(['invite_url' => $this->invite_url,'companyName' => $this->companyName]);
    }
}

View

メールで招待するので、メールアドレスを打ち込むためのフォームを用意する。

.blade.php
<form action="/enterprise/add_member" method="post">    
   @csrf        
   <input class="form-control" name="email" type="text">
   <input class="btn btn-primary" type="submit" value="投稿する">
</form>

本文

emails/invite_blade.php
<h1>{{ $companyName }}スタッフへの招待</h1>

<p>あなたは{{ $companyName }}のスタッフとして招待されました</p>
<p>以下のURLをクリックしてスタッフになりましょう!!</p>
<a href="{{$invite_url}}">参加する</a>

Controller

.php

//メールファサードとメールモデルを宣言する。
use Illuminate\Support\Facades\Mail;
use App\Mail\InviteMail;
:

//メール本文に埋め込む変数を定義する。
$invite_url = "http://127.0.0.1/register";
$companyName = $company->name;

//どのメールモデルを使用するかを選び、引数に定義した変数を入れておく。
Mail::to($request->email)->send(new InviteMail($invite_url, $companyName));

//flashメッセージでも付与してviewを返してあげる。
return redirect()->back()->with('flash_message', '招待メールを送信しました。');

参考:ざっくりメール送信までのフロー図

使用するファイル
・メールモデル
・コントローラ
・メール本文

Controller.php
Mail::to($request->email)->send(new TestMail());
TestMail.php
    public function build()
    {
        return $this
        ->subject('テスト送信完了')->view('emails.test');
    }

メール
宛先:$request->email
件名:テスト送信完了
本文:(emails/test.blade.php)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?