Here is about this article:
- Send Email with
So why do we need to use
When we execute some jobs that require longer time, like sending Emails, or uploading photos or videos, let users to wait until the jobs are completed is just not so practical.
So when users request some time consuming jobs, we need to use
queue to line them, and execute them on the background, and so the users could be released from current request immediately.
AWS SQS service
- Firstly, you need an
- Apply SQS on
- Refer to official document to complete the setting.
- Keep the information below for further usage.
On up-right corner, click your account, and choose
My Security Credentials
Usersto create a new user
Give user id, and check
Programmic access, and next
Create groupas photo below
Add the user we just created into this newly created group
Add tagsis optional, you could skip it
Now we have
Access key IDand
Secret access key. If you are afraid of forgetting them, you could download them. By the way,
Secret access keywill only be accessible this time, you will need to reproduce it if you forget it.
- The following operation is referred to official document
AWSofficial SDK as referred in official document，under the repository:
composer require aws/aws-sdk-php
php artisan make:job ProcessPodcast
class SendMailWhenOrderPlaced implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// The maximum attempts of this job
public $tries = 5;
* Create a new job instance.
* @return void
public function __construct($order, $FB_email, $Local_email)
$this->order = $order;
$this->FB_email = $FB_email;
$this->Local_email = $Local_email;
$this->job = Helpers::mailWhenOrderPlaced($order, $FB_email, $Local_email);
* Execute the job.
* @return void
public function handle()
The example above is to execute a function of sending Email called
queue in Ray’s project.
- Wherever we want to execute this
job, we use
SendMailWhenOrderPlaced::dispatch($order, $FB_email, $Local_email);
- Under the project
php artisan queue:work
- Now, when the
dispatchis executed, it will send Email with
It’s so easy, isn’t it?
By the way, because we use
queue, so we have to make sure that
qeeue works well. In this case, if the
queue fails, then the
To guarantee that
queue works well, and automatically restart after fails, we need to use
Supervisor to help us monitor and manage processes.
If you are interested in
Supervisor, you could take a look on Ray’s another article here
If you are also interested in how to use
Laravel Mail and
AWS SES, to send mail, you could take a look on Ray’s another article here as well