0
0

More than 3 years have passed since last update.

laravel6 htmlメールとtextメール両方送信できるようにする

Last updated at Posted at 2021-02-07

目的

  • htmlメールとtextメールをどちらか一つ、または両方を送信する方法をまとめる

条件

情報

  • 筆者はMacに直接作成したlaravelのローカル開発環境で本記事に記載したソースの検証を行っている。
  • 筆者の環境ではメールの受信確認はMailCatcherを用いて行う。
  • メール送信相手のアドレスは「test@example」とする。
  • メール送信者のアドレスは「app@example」とする。
  • 特筆しない限り実行しているコマンドは前のコマンドと同じディレクトリで実行するものとする。
  • 下記の記事で実装したメールをhtmlメールとtextメール両方送信するように修正する。

概要

  1. コントローラファイルの修正
  2. メールクラスの修正
  3. htmlメールのテンプレート追加
  4. 確認

詳細

  1. コントローラファイルの修正

    1. NoticeController.phpを開き、下記のように修正する。

      アプリ名ディレクトリ/app/Http/Controllers/NoticeController.php
      <?php
      
      namespace App\Http\Controllers;
      
      use Illuminate\Http\Request;
      use App\Http\Requests\MailRequest;
      use Illuminate\Support\Facades\Mail;
      use App\Mail\AppMail;
      use App\Services\MailAttachmentService;
      
      class NoticeController extends Controller
      {
          /**
           * @var MailAttachmentService
           */
          private $mailAttachmentService;
      
          public function __construct(MailAttachmentService $mailAttachmentService)
          {
              $this->mailAttachmentService = $mailAttachmentService;
          }
      
          public function index()
          {
              return view('notices.index');
          }
      
          public function mailMake()
          {
              return view('notices.mails.make');
          }
      
          public function mailConfirm(MailRequest $mailRequest)
          {
              $postData = $mailRequest->all();
      
              if (isset($postData['file'])) {
                  $postData['putFileInfo'] = $this->mailAttachmentService->saveFile($postData['file']);
              }
      
              $viewData = [
                  'postData' => $postData
              ];
      
              return view('notices.mails.confirm', ['viewData' => $viewData]);
          }
      
          public function mailSend(Request $request)
          {
              // 下記を追記
              $mailInfo = [
                  'htmlTemplate' => 'mail_templates.html_mail',
                  'textTemplate' => 'mail_templates.text_mail',
              ];
              // 上記までを追記
              $postData = $request->all();
              // 下記を修正
              Mail::to('test@example')->send(new AppMail($postData, $mailInfo));
              return redirect(route('notice.index'));
          }
      }
      
  2. メールクラスの修正

    1. AppMail.phpを開き、下記のように修正する。

      アプリ名ディレクトリ/app/Mail/AppMail.php
      <?php
      
      namespace App\Mail;
      
      use Illuminate\Bus\Queueable;
      use Illuminate\Contracts\Queue\ShouldQueue;
      use Illuminate\Mail\Mailable;
      use Illuminate\Queue\SerializesModels;
      
      class AppMail extends Mailable
      {
          use Queueable, SerializesModels;
      
          /**
           * メール送信引数
           *
           * @var array
           */
          private $postData;
      
          /**
           * Create a new message instance.
           *
           * @return void
           */
          // 下記を修正
          public function __construct($postData, $mailInfo)
          {
              $this->postData = $postData;
              // 下記を修正
              $this->mailInfo = $mailInfo;
          }
      
          /**
           * Build the message.
           *
           * @return $this
           */
          public function build()
          {
              // 下記を修正
              // デフォルト処理、fromメールアドレス、件名、テキストメールのテンプレートの設定
              $mail = $this->from('app@example')->subject($this->postData['subject'])->text($this->mailInfo['textTemplate'], ['postData' => $this->postData]);
      
              if (isset($this->postData['filePath'])) {
                  // 添付ファイルが有るときに実行
                  $mail->attachFromStorage($this->postData['filePath'], mb_encode_mimeheader($this->postData['fileName']));
              }
      
              if (isset($this->mailInfo['htmlTemplate'])) {
                  // htmlメールも同時に送信するとき
                  $mail->view($this->mailInfo['htmlTemplate'], ['postData' => $this->postData]);
              }
      
              return $mail;
              // 上記までを修正する。
          }
      }
      
  3. htmlメールのテンプレート追加

    1. 下記コマンドを実行してhtmlメールのテンプレートを作成して開く。

      $ vi resources/views/mail_templates/html_mail.blade.php
      
    2. 開いたファイルに下記の内容を記載する。

      アプリ名ディレクトリ/resources/views/mail_templates/html_mail.blade.php
      {!! nl2br(e($postData['content'])) !!}
      
      <h1>Foo</h1>
      
  4. 確認

    1. 下記コマンドを実行してローカルサーバを起動する。

      $ php artisan serve
      
    2. 下記にアクセスする。

    3. 件名と内容に任意の内容を入力して「確認」をクリックする。遷移先のページで「送信」をクリックしてメールを送信する。

      Laravel-5.png

    4. 受信したメールを確認する。

    5. MailCatcherで確認する場合「HTML」のタブを開いたときにメール内容の末尾にh1要素で「Foo」と表示されることを確認する。

      MailCatcher__24_-2.png

    6. 「Plain Text」のタブを開いたときメールの内容がそのまま表示されることを確認する。

      MailCatcher__24_.png

0
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
0
0