JoBins採用企業サイト JoBinsエージェントサイト

アカウントをお持ちの方はこちら 採用企業ログイン
エージェントログイン


background

Laravel sending multi-user email notification through Eloquent Model

Jan 19, 2018

Laravel provides one of the best support for sending notifications with multiple delivery channels including MAIL, SMS(Nexmo), Slack Notifications and notifications can be stored in stored in the database so can be easily displayed in the website

Create Notification Class

At first, we need to create notification class in our app directory by using make:notification Artisan command which will create Notifications directory and inside that directory notification class file will be created.

Using command prompt or your editor terminal go to your project directory and type the command:

php artisan make:notification Sample (Note: Sample is a class name so it can be any text)

After running this command you will see app/Notifications/Sample.php notification class file inside your app directory.

Setup mail server

We need to set mail server parameter in the environment file. Open the .env file set the server parameter. Now we are using Gmail for sending email

 MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=*****(email)
MAIL_PASSWORD=*********(password)
MAIL_ENCRYPTION=tls

After save clear the cache using artisan command

php artisan cache:clear

Note: laravel default mailer requires SSL connection and throws an error in local. So to overcome this security certificate error, open config/mail.php  and add the following code

'stream' => [
    'ssl' => [
        'allow_self_signed' => true,
        'verify_peer' => false,
        'verify_peer_name' => false,
    ],
],>

Controller Sending mail

Now we are using Notifiable facade method because we need to send multiple users at a time with eloquent model so after class add use Notifiable; method

namespace App\Http\Controllers;

use App\Notifications\Sample;
use App\Subscribers;
use Illuminate\Http\Request;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Notification;

class Blog extends Controller
{
    use Notifiable;

setup the mail data in controller

 $mail['message_link'] ="redirect link when user click button in mail message body";
$mail['mail_message'] = "Email message body";
$mail['greeting'] = "Greetings";
$mail['mail_subject'] =  "Email subject";
$mail['buttton_text'] =  "Button Text";
$mail['mail_footer'] = "Email Footer text";

Notification::send(Subscribers::all(), new Sample($mail));

Here Notification::send function is used to send mail notifications and Subscribers::all is to get all user email from Subscribes model and we add all mailing data within the array.

Note: we need to add use Notifiable instance in the model and Subscribers is our sample eloquent model. To send the mail notification there should be email field in the database table of respective model.

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;


class Subscribers extends Model
{
    use Notifiable;

In default added mail parameters we have to add some extra parameters so we need to modify the Sample.php notification file.

Modify the sample file

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

After class initiliaze the dynamic data

class Sample extends Notification
{
    use Queueable;

    public  $mail_data;
    public function __construct($mail_data)
    {
        //
        $this->mail_data =  (object)$mail_data;
    }

       
public function via($notifiable)
    {
        return ['mail'];
    }

Modify toMail function

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->greeting($this->mail_data->greeting)
                    ->from('info@sample.com', 'Sample')
                    ->subject($this->mail_data->mail_subject)
                    ->line($this->mail_data->mail_message)
                    ->action($this->mail_data->button_text, url($this->mail_data->message_link))
                    ->line($this->mail_data->mail_footer);
    }

Output(sample email)

sample email