メール通知のMailMessageを利用するとhtml形式の送信になってしまうのでテキスト形式で送信する方法を調べていたら
同じような人がいて質問していましたが回答がなく
https://teratail.com/questions/119201
いろいろ探したのですが見当たらなかったのでMailableを使って対応。
正確には
https://readouble.com/laravel/5.5/ja/notifications.html
テンプレートのカスタマイズができるっぽいけどよくわからないのでMailableにしました。
Mailableクラスを作る
app/Mail/BaseMail.php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class BaseMail extends Mailable
{
use Queueable, SerializesModels;
public $options;
public $data;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($options, $data)
{
$this->options = $options;
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from($this->options['from'], $this->options['from_jp'])
->subject($this->options['subject'])
->text($this->options['template']);
// ->view($this->options['template']); // htmlの場合こちら
}
}
通知(Notification)クラスを作成
php artisan make:notification PasswordResetNotification
app/Notifications/PasswordResetNotification.php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Mail\BaseMail;
class PasswordResetNotification extends Notification
{
use Queueable;
public $token;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$mail_to = $notifiable->email;
$subject = "パスワードリセット";
$message = url('password/reset', $this->token);
$options = [
'from' => 'info@example.com',
'from_jp' => '【example.comより】',
'to' => $mail_to,
'subject' => $subject,
'template' => 'emails.passwordreset',
];
$data = [
'mail_to' => $mail_to,
'message' => $message,
];
return (new BaseMail($options, $data))->to($mail_to);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
メールテンプレート作成
resources/views/emails/passwordreset.blade.php
下記のurlをクリックしてパスワードリセットフォームより新しいパスワード設定を行って下さい。
{{ $data['message'] }}
心当たりがない場合は、なにもせずにこのメールを削除してください。
Mailableは細かい設定は調べて設定してください。
参考サイト
https://www.ritolab.com/entry/52#customize_password_reset_email