skip to Main Content

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 AppLivewireInbox;

use LivewireAttributesTitle;
use LivewireAttributesValidate;
use LivewireComponent;
use IlluminateSupportFacadesMail;
use AppMailComposeMail;

#[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 AppMail;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateMailMailablesContent;
use IlluminateMailMailablesEnvelope;
use IlluminateQueueSerializesModels;
use IlluminateMailMailablesAddress;

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, IlluminateMailMailablesAttachment>
     */
    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

Answers


  1. 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.

    Login or Signup to reply.
  2. 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]"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search