laravel + livewire + tailwind - メール送信

(2025-01-05)

laraconf にメール送信機能を加えます。

Livewireコンポーネントを作成

php artisan make:livewire SendWriteRequest

編集。

app/Livewire/SendWriteRequest.php
<?php
namespace App\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Mail;
use App\Mail\WriteRequestNotification;
use App\Models\User;
class SendWriteRequest extends Component
{
public function sendEmails()
{
$users = User::whereNotNull('email')->get();
foreach ($users as $user) {
Mail::to($user->email)->send(new WriteRequestNotification());
}
session()->flash('success', 'メールが送信されました!');
}
public function render()
{
return view('livewire.send-write-request');
}
}

メールの設定

php artisan make:mail WriteRequestNotification

編集。

app/Mail/WriteRequestNotification.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class WriteRequestNotification extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
// 必要な場合は、コンストラクタにパラメータを追加してデータを渡す
}
public function build()
{
return $this->subject('laraconf より')
->view('emails.write-request');
}
}

resources/views/emails/write-request.blade.php を作成します。

resources/views/emails/write-request.blade.php
<!DOCTYPE html>
<html>
<head>
<title>書き込み依頼</title>
</head>
<body>
<p>書き込みをお願いします。</p>
</body>
</html>

resources/views/livewire/send-write-request.blade.php

resources/views/livewire/send-write-request.blade.php
<div class="m-4">
<h1 class="text-xl font-bold mb-4 border border-gray-500 bg-slate-200 p-2">メール送信</h1>
<p class="border border-slate-300 p-2 rounded shadow-md bg-gray-100 leading-7 mb-5">
メンバー全員にメールを送信します。<br>
メール内容は「メンバーからメール送信依頼がありましたので、メンバー全員にお送りしています。」です。<br>
新しいテーマを作成した場合や、書き込みをして回答を希望される場合などに利用して下さい。
</p>
<p class="border border-slate-300 p-2 rounded shadow-md bg-gray-100 leading-7 mb-5">
メールはすぐに送信されない場合があります、1分ほど経っても送られない場合には再度送信して下さい。
</p>
<button wire:click="sendEmails" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
メール送信
</button>
@if (session()->has('success'))
<div class="text-sky-800">
{{ session('success') }}
</div>
@endif
<div class="mt-5"><a href="{{ route('dashboard') }}" class="px-4 py-2 bg-orange-900 text-white rounded hover:bg-orange-700">トップへ</a></div>
</div>

ダッシュボード変更

<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">{{ __('Menu') }}</h2>
<ul class="list-disc leading-10 mx-10 px-2">
<li class="text-blue-800 hover:text-orange-800 delay-150"><a href="{{ route('theme.list') }}">テーマ一覧</a></li>
<li class="text-blue-800 hover:text-orange-800 delay-150"><a href="{{ route('theme.create') }}">テーマ作成</a></li>
<li class="text-blue-800 hover:text-orange-800 delay-150"><a href="{{ route('zip.image') }}">画像アップロード</a></li>
<li class="text-blue-800 hover:text-orange-800 delay-150"><a href="{{ route('send.request') }}">メール送信</a></li>
<li class="text-blue-800 hover:text-orange-800 delay-150"><a href="{{ route('admin.dashboard') }}">管理者用サイト</a></li>
</ul>
</x-slot>
</x-app-layout>

.env 編集

MAIL_MAILER=smtp
MAIL_SCHEME=null
MAIL_HOST=sv111123.xserver.jp
MAIL_PORT=587
MAIL_USERNAME=hoge@moheno.xsrv.jp
MAIL_PASSWORD=password
MAIL_FROM_ADDRESS=hoge@moheno.xsrv.jp
MAIL_FROM_NAME="laraconf"

ルーティング

<?php
use Illuminate\Support\Facades\Route;
use App\Livewire\ThemeCreate;
use App\Livewire\ThemeList;
use App\Livewire\ThemeShow;
use App\Livewire\ZipImage;
use App\Livewire\ThemeImage;
use App\Http\Middleware\AdminMiddleware;
use App\Livewire\Admin\AdminDashboard;
use App\Livewire\Admin\CreateMember;
use App\Http\Controllers\ProfileController;
use App\Mail\ThemeCreatedNotification;
use Illuminate\Support\Facades\Log;
use App\Livewire\SendWriteRequest;
use App\Models\User;
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Route::middleware('auth')->group(function () {
Route::get('/theme/create', ThemeCreate::class)->name('theme.create');
Route::get('/theme/list', ThemeList::class)->name('theme.list');
Route::get('/theme/{theme_id}', ThemeShow::class)->name('theme.show');
Route::get('/zip-image', ZipImage::class)->name('zip.image');
Route::get('/theme/{theme_id}/images/{folderName?}', ThemeImage::class)->name('theme.image');
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
Route::get('/send-request', SendWriteRequest::class)->name('send.request');
});
Route::middleware(['auth', AdminMiddleware::class])->group(function () {
Route::get('/admin/dashboard', AdminDashboard::class)->name('admin.dashboard');
Route::get('/admin/create-member', \App\Livewire\Admin\CreateMember::class)->name('admin.create-member');
});
require __DIR__.'/auth.php';