by Sebastian Schiau , 7 years ago
So most of the web apps nowadays are featuring email notifications, newsletters and all kinds of different email implementations. Laravel eases up the process a lot with its suit of tools that allows you to send email via any driver you wish, template and customize your emails and even queue them for later execution.
The queue functionality comes in handy in particular when you want to speed up up your application by not having to wait for server response when doing requests that are sending emails. This basically allows emails to be sent asynced on the server side. Couple examples of use cases:
php artisan make:provider EmailProvider<?php
namespace App\Providers;
use App\Jobs\SendEmail;
use Illuminate\Support\ServiceProvider;
class EmailProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
*
* Generic email template method
*
* @param $email
* @param $header
* @param $content
*/
public static function sendEmail($subject, $title, $content){
dispatch(new SendEmail($subject,$title,$content));
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
php artisan make:job SendEmail<?php
namespace App\Jobs;
use App\Mail\GenericEmail;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
class SendEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $emailSubject,$emailTitle,$emailContent;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($emailSubject,$emailTitle,$emailContent)
{
//
$this->emailSubject = $emailSubject;
$this->emailTitle = $emailTitle;
$this->emailContent = $emailContent;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
Mail::to($user->email)->later(Carbon::now()->addMinute(1), new GenericEmail($this->emailSubject,$this->emailTitle,$this->emailContent));
}
}
php artisan make:mail GenericEmail<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class GenericEmail2 extends Mailable
{
use Queueable, SerializesModels;
public $subject = 'Mass email';
public $title = 'Email header';
public $content = 'Email content';
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($emailTitle,$emailTitle,$emailContent)
{
//
$this->subject = $emailTitle;
$this->title = $emailTitle;
$this->content = $emailContent;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this
->subject($this->subject)
->view('emails.template');
}
}
resources/views/emails/template.blade.php with a content like<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" style="font-size: 100%; font-family: 'Avenir Next', 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; line-height: 1.65; margin: 0; padding: 0;">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<!-- For development, pass document through inliner -->
</head>
<body style="font-size: 100%; font-family: 'Avenir Next', 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; line-height: 1.65; width: 100% !important; height: 100%; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; background: #efefef; margin: 0; padding: 0;">
<h2>{!!$emailTitle!!}</h2>
<p>{!!$content!!}</p>
</body>
</html>
EmailProvider::sendGlobal(‘This is a test email’,’Welcome friend’,’Just testin things around’);php artisan queue:tablephp artisan migrateQUEUE_DRIVER=database
easy_install supervisoryum install supervisorecho_supervisord_conf > /etc/supervisord.confnano /etc/supervisord.conf[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /var/www/html/artisan queue:work --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
numprocs=2
sudo mkdir -p /var/log/supervisor/laravel/sudo touch /var/log/supervisor/laravel/worker.logsudo service supervisord startsudo supervisorctl reloadsystemctl enable supervisordsupervisorctl rereadsupervisorctl updatesupervisorctl restart all