Sending an email shouldn’t have to be complicated. Laravel provides a clean, simple email API powered by the popular Symfony Mailer component. Laravel and Symfony Mailer provide drivers for sending email via SMTP, Mailgun, Postmark, Amazon SES, and sendmail, allowing you to quickly get started sending mail through a local or cloud based service of your choice.
The best way to send an email in laravel is to use the laravel queuing system. Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time.
https://onlinecode.org/how-to-send-mail-using-queue-in-laravel-4/?amp=1
Step 1 : Create Mail
php artisan make:mail SendEmailTest
After executing the code above, Laravel will create a new email file app/Mail/SendEmailTest.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendEmailTest extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.test');
}
}
Now we need to create a new view for this email resources/views/emails/test.blade.php
Visit Our Website : ItSolutionStuff.com
Hi, Sir
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Thank you Sir. :)>
Now we need to set up sending mail
So let’ set configuration in .env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xyz@gmail.com
MAIL_PASSWORD=123456
MAIL_ENCRYPTION=tls
Step 2 : Configuration of Queue
The next step is to configure the queues themselves. To begin with, in the .env file, we will specify the database as the queue driver
QUEUE_DRIVER=database
Then we need to generate the migration and create tables for the queues
php artisan queue:table
php artisan migrate
Step 3 : Create Queue Job
Now, create a queue task handler
php artisan make:job SendEmailTest
This command will create the app/Jobs/SendEmailJob.php file. Let's fill it with the following code
<?php
namespace AppJobs;
use IlluminateBusQueueable;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use AppMailSendEmailTest as SendEmailTestMail;
use Mail;
class SendEmailTest implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $details;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email = new SendEmailTestMail();
Mail::to($this->details['email'])->send($email);
}
}
Step 4 : Use Queue Job
Let's test the queues. First of all, let's create a route in which a new task for the queue will be created.
routes/web.php
Route::get('email-test', function(){
$details['email'] = 'add_your_email@gmail.com';
dispatch(new AppJobsSendEmailTest($details));
dd('done');
});
Ok, the route has been created. Start the queue listener using the following command
php artisan queue:listen
Let's start the server with the command
php artisan serve
Top comments (0)