Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Thomas_Shelby's avatar

Trying to access array offset on value of type null in laravel 10 when sending email

i'm using laravel 10 and livewire 3. i'm trying to send email using mailtrap and i'm working on my local machine now. This is my livewire component


namespace App\Livewire\Inbox;

use Livewire\Attributes\Title;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Illuminate\Support\Facades\Mail;
use App\Mail\ComposeMail;

#[Title('Email Compose')]
class Compose extends Component
{
    #[Validate('required|email')]
    public $to;
    #[Validate('required|string|max:255')]
    public $subject;
    #[Validate('required|string')]
    public $message;

    public function send()
    {
        $this->validate();

        if(!is_null($this->to)){
            Mail::to($this->to)->send(new ComposeMail($this->subject, $this->message));
        }


        session()->flash('success', 'Mail sended successfully');
    }

    public function render()
    {
        return view('livewire.inbox.compose');
    }
}

This is my laravel mailable

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Address;

class ComposeMail extends Mailable
{
    use Queueable, SerializesModels;

    public $subject;
    public $messages;

    /**
     * Create a new message instance.
     */
    public function __construct($subject, $messages)
    {
        $this->subject = $subject;
        $this->messages = $this->sanitizeMessage($messages);
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            from: new Address('[email protected]', 'john'),
            subject: 'testing',
        );
    }
    
    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'mails.send',
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }

    private function sanitizeMessage($message): string
    {
        return htmlentities($message);
    }
}

blade file

<h1>mail sent</h1>

.env file

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=6c9158bd41cb6e
MAIL_PASSWORD=********686d
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="[email protected]"

when i click on send button to send the email i get following error

ErrorException
PHP 8.2.12

10.43.0
Trying to access array offset on value of type null
and show the red at this code of line " Mail::to($this->to)->send(new ComposeMail($this->subject, $this->message));"

when i was start this project i was using 8.1 version of php.

0 likes
3 replies
Thomas_Shelby's avatar

i found the problem in my code. there was mailtrap credentials issue.

Bk32's avatar

I phase same problem please share the solution

Thomas_Shelby's avatar

the problem was my mailtrap credentials. i coped the credentials using mouse select. but you need to copy credentials using copy button. in my case following line was the problem MAIL_PASSWORD=********686d

3 likes

Please or to participate in this conversation.