Laravel:向多个用户发送电子邮件

时间:2020-07-29 07:09:18

标签: laravel email

我想用Laravel发送电子邮件。 2个用户获得不同的电子邮件内容。所以,这是我的代码。

TransactionController.php

tsc --noEmit $(changedFile)

MailFruitCustomer.php(在Mail文件夹中)

use App\Mail\MailFruitCustomer;
use App\Mail\MailFruitSeller;

$data = array(
            'fruitid' => 'F01',
            'fruitname' => 'Banana',
            'customercode' => 'B2345',
            'sellercode' => 'S9546'
        );

Mail::to(customer1@mail.com)->send(new MailFruitCustomer($data));
Mail::to(seller1@mail.com)->send(new MailFruitSeller($data));

MailFruitSeller.php(在Mail文件夹中)

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

public function build(){
    $result = $this->from('admin@mail.com')
                   ->subject('Customer New Transaction')
                   ->markdown('emails/customerreceipt')
                   ->with('data', $this->data);

    return $result;
}

customerreceipt.blade.php

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

public function build(){
    $result = $this->from('admin@mail.com')
                   ->subject('Seller New Transaction')
                   ->markdown('emails/sellerreceipt')
                   ->with('data', $this->data);

    return $result;
}

sellerreceipt.blade.php

@component('mail::message')
Customer Receipt Detail
Fruit ID: {{ $data['fruitid'] }}
Fruit Name: {{ $data['fruitname'] }}
Customer Code: {{ $data['customercode'] }}
Thank you.<br>
@endcomponent

它正在运行,并且两个用户都收到具有不同内容的电子邮件。但是该过程需要更长的时间。在laravel中有什么我可以应用的邮件方法吗?

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

您正在搜索队列:https://laravel.com/docs/7.x/queues

您创建一个发送邮件并将其排队的作业。 Laravel可以更快地发送响应,并且邮件可以异步发送。

其他:https://laravel.com/docs/7.x/mail#queueing-mail

答案 1 :(得分:0)

我继续学习Queue并创建发送邮件的作业。

我遇到了另一个问题,并在其他人的帮助下进行了修复。谢谢。

这里是链接https://stackoverflow.com/a/63370095/12022919