Skip to content
Mitsuru Mutaguchi edited this page Oct 19, 2021 · 10 revisions

目次

Sample

.env

// Setting to send mail with php mail() (php mail()でメール送信する設定)

MAIL_DRIVER=sendmail
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=hello@example.com
MAIL_FROM_NAME=Example

.env (ex gmail)

// Not G Suite.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail@gmail.com
MAIL_PASSWORD=your_gmail_app_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_gmail@gmail.com
MAIL_FROM_NAME=LaravelGmailSample

Plugin

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

class HogePlugin extends UserPluginBase
{

    public function index($request, $page_id, $frame_id, $errors = [])
    {
        // メール送信
        $to_mail_address = 'hello@example.com';
        $mail_subject = 'テスト件名';
        $mail_text = "本文\n本文2";
        Mail::to(trim($to_mail_address))->send(new ConnectMail(['subject' => $mail_subject, 'template' => 'mail.send'], ['content' => $mail_text]));
    }

(詳細)メールキュー送信の流れ

2021/10/19時点

データベースの投稿通知を例に説明。

・データベースで記事を登録時にメール送信処理が呼ばれる
 DatabasesPlugin::publicStore()
  $this->sendPostNotice($databases_inputs, $before_databases_inputs, 'detail');

・親クラス UserPluginBase:: sendPostNotice() 呼ばれ、(キュー登録 ※1)と(キュー実行 ※2)が行われる。
 UserPluginBase:: sendPostNotice() 内
  // キュー登録
  PostNoticeJob::dispatch($this->frame, $this->buckets, $post_row, $title, $show_method, "notice_create");
   // 非同期でキューワーカ実行
  $this->asyncQueueWork();

(※1 キュー登録)
// dispatchメソッドへ渡す引数は、ジョブのコンストラクタへ渡されます。
App\Jobs\PostNoticeJob::__construct()
・コンストラクタで、プライベートクラス変数に値をセットしています。
・この時、ジョブクラスは SerializesModelsトレイトを使っていて、ジョブ(キュー)は、DBの jobs テーブルに一度格納されます。
 ・キューに入っているジョブがコンストラクタで Eloquent モデルを受け入れる場合、モデルの識別子だけがキューにシリアル化されます。

(※2 キュー実行)
 ・ジョブが実際に処理されると、キュー システムはデータベースからモデルインスタンス全体を自動的に再取得します。

・キューを実行して、Mail::to()でメール送信する。メールの内容はnew PostNotice()から取得。
 PostNoticeJob::handle() 内
  Mail::to($notice_address)->send(new PostNotice($this->frame, $this->bucket, $this->post, $this->title, $this->show_method, $this->notice_method, $bucket_mail));

App\Mail\PostNotice::__construct() で値をセット
App\Mail\PostNotice::build() でメール内容をセット。定型文は 'mail.post.post_text‘。

    public function build()
    {
        return $this->text('mail.post.post_text')
                    ->subject($this->bucket_mail->notice_subject)
                    ->with([
                        'frame'         => $this->frame,
                        'bucket'        => $this->bucket,
                        'post'          => $this->post,
                        'title'         => $this->title,
                        'show_method'   => $this->show_method,
                        'notice_method' => $this->notice_method,
                        'bucket_mail'   => $this->bucket_mail,
                    ]);
    }


定型文 'mail.post.post_text‘。
resources\views\mail\post\post_text.blade.php

{{$bucket_mail->getFormatedNoticeBody($frame, $bucket, $post, $title, $show_method, $notice_method)}}


BucketsMail::getFormatedNoticeBody() 参照
ここで埋め込みタグを使ってる。
問題の モデル変数 $post は、他のメソッドも含めて今のところ $post->title $post->id 位しか使っていない。

メールキュー参考

ジョブのディスパッチ- キュー 6.x Laravel

dispatchメソッドへ渡す引数は、ジョブのコンストラクタへ渡されます。

php - Laravelキューシリアライズモデル特性が関連する値を削除 - スタックオーバーフロー

キューに入っているジョブがコンストラクタで Eloquent モデルを受け入れる場合、モデルの識別子だけがキューにシリアル化されます。ジョブが実際に処理されると、キュー システムはデータベースからモデルインスタンス全体を自動的に再取得します。

Reference(参照・参考)

Clone this wiki locally