Like the title says, I am trying to send email but it keeps on getting this error.
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ContactFormRequest;
use App\Mail\ContactMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class ContactController extends Controller
{
/**
* Display the user's profile form.
*/
public function contact(ContactFormRequest $request): \Illuminate\Http\RedirectResponse
{
$mailData = $request->validated();
\Log::info(config('mail.to.address'));
\Log::info($mailData);
Mail::to(config('mail.to.address'))->send(new ContactMail($mailData)); // error points to here.
return redirect()->back();
}
}
class ContactMail extends Mailable
{
use Queueable, SerializesModels;
public $mailData = [];
/**
* Create a new message instance.
*/
public function __construct($mailData)
{
\Log::info("did construct");
$this->mailData = $mailData;
}
}
[2024-04-25 02:38:59] local.INFO: to@example.com
[2024-04-25 02:38:59] local.INFO: array (
'contact-name' => 'name',
'contact-name-furigana' => 'ネイム',
'contact-email' => 'name@name.com',
'contact-phone' => '1234',
'contact-contents' => 'contents',
'contact-privacy' => 1,
)
Undefined array key "name" {"exception":"[object] (ErrorException(code: 0): Undefined array key \"name\" at /var/www/vendor/laravel/framework/src/Illuminate/Mail/MailManager.php:479)
It is not even hitting the ContactMail
class.
First I thought the issue was with the import use Illuminate\Support\Facades\Mail;
but this is what is used in the tutorial.
I think I got it.
I was trying to add MAIL_TO_ADDRESS="to@example.com"
in the .env and also in the app\config\mail.php
.
In my mail.php, I only added
'to' => [
'address' => env('MAIL_TO_ADDRESS', 'hello@example.com'),
],
I guess laravel is only accepting "to:address" and "to:name" as a pair or none at all. So I change it to
'to' => [
'address' => env('MAIL_TO_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_TO_NAME', 'Example'),
],