0

I want to send email in laravel. 2 user get different email content. So, here is my code.

TransactionController.php

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

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

Mail::to([email protected])->send(new MailFruitCustomer($data));
Mail::to([email protected])->send(new MailFruitSeller($data));

MailFruitCustomer.php (In Mail folder)

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

public function build(){
    $result = $this->from('[email protected]')
                   ->subject('Customer New Transaction')
                   ->markdown('emails/customerreceipt')
                   ->with('data', $this->data);

    return $result;
}

MailFruitSeller.php (In Mail folder)

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

public function build(){
    $result = $this->from('[email protected]')
                   ->subject('Seller New Transaction')
                   ->markdown('emails/sellerreceipt')
                   ->with('data', $this->data);

    return $result;
}

customerreceipt.blade.php

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

sellerreceipt.blade.php

@component('mail::message')
Seller Receipt Detail
Fruit ID: {{ $data['fruitid'] }}
Fruit Name: {{ $data['fruitname'] }}
Seller Code: {{ $data['sellercode'] }}
Thank you.
@endcomponent

It is working and both user get email with different content. But the process take longer time. Is there any method of mail in laravel that i could apply?

Any help will be grateful. Thank you.

2 Answers 2

1

You are searching for Queues: https://laravel.com/docs/7.x/queues

You create a job that sends the mail and queue the job. Laravel can send the response faster and the Mail is sent asynchronous.

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

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

Comments

0

I continue to learn Queues and create job that sends mail.

I got another problem and fix it with help from others. Thank you.

Here is the link https://stackoverflow.com/a/63370095/12022919.

Comments

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.