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

117 views Asked by At

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

<?php

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));"

i'm tryng to send email in laravel 10 and livewire 3 using mailtrap but i'm geting an error. i'm expecting the solution of the error and the reason why this error occur.

2

There are 2 answers

1
Mani On

This error is generally thrown on arrays, you should first check the type of $to variable. Most probably this must be coming as an array. On empty arrays if(!is_null($this->to) condition will not return false.

0
Mahadi Hasan On

I got it fixed. You just need to focus on that config. That problem comes from that configuration. For example, the MAIL_FROM_ADDRESS should be the same as your domain, etc.

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]"