I have a listener which implement ShouldQueue, the listener is handling a mail sending when event occurs. The job detects which type of user gets the email and config the mail sender name :
$displayName = $user->is_type() == 1 ? 'sender_name1' : 'sender_name2';
config(['mail.from' =>
[
'name' => $displayName
]]);
On multiple actions the sender name is sent incorrectly even though the config occurs.
By printing getmypid() looks like the job runs on a process that already took care of
an older job and the sender name that used was of the older one.
Any suggestions for a solution please?
My Answer:
>isAdmin ? 'Admin' : 'User';
Config::set('mail.from.name', $displayName);
// Send the email
Mail::to($user->email)->send(new MyMail());
}
This code dynamically sets the sender name based on whether the user is an admin or a regular user before sending the email. Make sure to import the necessary classes at the top of your listener file:
php
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
This should solve the problem of dynamically configuring the sender name for the email in your Laravel job using ShouldQueue.
Rate this post
3 of 5 based on 2982 votesComments