0

I want to send email using laravel queue. I follow tutorial from this site https://blog.mailtrap.io/laravel-mail-queue/ . It is work (email can be send using job and queue).

But i want to include value in the email. Below are the code that i modify to send the value.

I thought $details array will carry the value and pass to the email template. But when i run, the job is failed. Is there any way that i could improve?

PController.php

$details = array(
            'email' => '[email protected]',
            'fruitname' => 'watermelon',
            'fruitid' => 'F001'
        );

dispatch(new SendEmail($details))->delay(Carbon::now()->addSeconds(10));

Mailable Class: SendEmail.php

protected $details;

public function __construct($details)
    {
        $this->details = $details;
    }

public function handle()
    {
        $email = new MailFruit();
        Mail::to($this->details['email'])->send($email);
    }

MailFruit.php

protected $details;

public function __construct($details)
    {
        $this->details = $details;
    }

public function build()
    {
        return $this->from('[email protected]')
                    ->subject('New Fruit')
                    ->view('emails/email_fruit_template')
                    ->with('details', $this->details);
    }

email_fruit_template.blade.php

@component('mail::message')

New Fruit Details

Fruit ID: {{ $details['fruitid'] }}
Fruit Name: {{ $details['fruitname'] }}

Fruit Company
@endcomponent

Any help will be grateful. Thank you.

Edit: This one appear on cmd.

$php artisan queue:work
[2020-08-12 14:36:22][58] Processing: App\Jobs\SendEmail
[2020-08-12 14:36:23][58] Failed:     App\Jobs\SendEmail
1
  • 2
    you are not passing that array to the new instance of MailFruit Commented Aug 12, 2020 at 4:37

1 Answer 1

1

Pass details array in the new instance of Mailfruit.


public function handle()
    {
        $email = new MailFruit($this->details);
        Mail::to($this->details['email'])->send($email);
    }

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for the answer. I tried to pass details array, but the email job is till failed.
[2020-08-12 14:36:23][58] Failed: App\Jobs\SendEmail.
Seems like there is no value passes to the email template
Can you provide more details on the error. it could be hundrads of reason why your job is failing. just update your question.
make the $details public and try again. let me know.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.