Laravel:使用队列和作业发送电子邮件 [英] Laravel: Send email using queue and jobs

查看:145
本文介绍了Laravel:使用队列和作业发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用laravel队列发送电子邮件。我遵循此站点上的教程 https://blog.mailtrap.io/laravel-mail-queue / 。这是工作(可以使用作业和队列发送电子邮件)。

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.

我认为 $ details 数组将携带值并传递给电子邮件模板。但是当我跑步时,工作失败了。有什么我可以改善的方法吗?

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

PController.php

$details = array(
            'email' => 'test@mail.com',
            'fruitname' => 'watermelon',
            'fruitid' => 'F001'
        );

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

可邮寄类:SendEmail.php

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

MailFruit.php

protected $details;

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

public function build()
    {
        return $this->from('admin@mail.com')
                    ->subject('New Fruit')
                    ->view('emails/email_fruit_template')
                    ->with('details', $this->details);
    }

email_fruit_template.blade.php

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.

编辑:
这一个出现在cmd上。

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


推荐答案

传递详细信息数组在新的Mailfruit实例中。

Pass details array in the new instance of Mailfruit.


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

这篇关于Laravel:使用队列和作业发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆