fahdshaykh's avatar

Email is not sending

giving error: Trying to access array offset on value of type null

my controller function

    // Validate the request
        $validated = $request->validate([
           'full_name' => 'required|string|max:255',
           'email' => 'required|email|max:255',
           'phone' => 'required',
           'subject' => 'required|max:255',
           'message' => 'nullable|string',
        ]);
       // dd($validated);
        Contact::create($validated);
       // Send the email

     // Prepare the email data
     $data = [
        'full_name' => $validated['full_name'],
        'email' => $validated['email'],
        'user_message' => $validated['message'],
    ];
    // Mail::to('[email protected]')->send(new ContactMail($validated));
    Mail::send('emails.contact', $data, function ($message) use ($data) {
        $message->to('[email protected]') // Replace with your email
                ->subject('New Contact Form Submission')
                ->from($data['email'], $data['full_name']);
    });

data is passing to the blade file on both way through Mailable class or PHP and when i use dd and check data is receving in emails contact blade file and also i test with bank blade file still giving error. my blade file is:

      <h1>New Contact Form Submission</h1>
    @php
          dd($full_name);
       @endphp
  <p><strong>Full Name:</strong> {{ $full_name }}</p>
 <p><strong>Email:</strong> {{ $email }}</p>
 <p><strong>Message:</strong></p>
  <p>{{ $user_message }}</p>
0 likes
7 replies
vincent15000's avatar

The error seems to come from any of your arrays.

$array[$someKey]

Probably a problem with a key.

Can you show the entire error message please ?

Can you also share with us the last log ?

1 like
fahdshaykh's avatar

@vincent15000 here is the log.

[2024-08-28 05:00:07] local.ERROR: Trying to access array offset on value of type null {"exception":"[object] (ErrorException(code: 0): Trying to access array offset on value of type null at D:\\work\\works techmech\\techmechsolutions\\vendor\\symfony\\mailer\\Transport\\Smtp\\Stream\\AbstractStream.php:91)
tisuchi's avatar

@fahdshaykh What if you try this?


$data = [
    'full_name' => $validated['full_name'] ?? '',
    'email' => $validated['email'] ?? '',
    'user_message' => $validated['message'] ?? '',
];
fahdshaykh's avatar

@tisuchi i tried with this one but still same error while dd() giving that data receiving correctly in blade file then i remove dd to send mail and then error come.

tisuchi's avatar

@fahdshaykh Then I think it's coming from the emails.contact file. Can you show your code?

fahdshaykh's avatar

Oh sorry it was my mistake in env mailer credentials was not configured correctly.

jaseofspades88's avatar

If you'd written a simple test with a Mail::fake(); you'd have easily spotted the problem straight away

Please or to participate in this conversation.