1

We are currently using laravel Event listener to send emails for laravel. Basically this is a slot booking option, so sometimes we have to send emails to sender and sometimes we have to send to receiver and sometimes we have to send emails other partners of the slots. In the current case we are using a single Event Listner to send different emails fir the different actions users taking on the slot like cancel meeting, add one more member etc. But generally in the case the email templates would be different only the dunamic variables we need to change.

But in the new case we have to send 4 or 5 emails to different users with different email templates and different contents on a single action. If we plan this in a single event listner, how we can handle this?

     $event_id=$event->user['XXXXX'];//event id

      $slot_type=$event->user['XXXXX'];//slot type
      $notification_type=$event->user['XXXXX']; //slot type
      $scheduler_slot_info_ids=$event->user['XXXX'];

      $data = $schedulerHelper->getOnetoOneNotificationContents($scheduler_slot_info_ids,$event_id,$slot_type);


     $action_trigger_by=$event->user['XXXXX'];
     //$data['subject']  =  'CARVRE SEVEN|MEETING CONFIRMED';
     $data['subject']  =  $event->user['XXXX'];
     // $data['template'] =  'emailtemplates.scheduler.oneToOneMeetingConfirmed';
     $data['template'] =  $event->user['XXXX'];

     $invitee_id=Crypt::encryptString($data['XXXX']);
     $crypt_event_id=Crypt::encryptString($event_id);
     $data['link']           =  url('XXXX');
     $data['email_admin']    =  env('FROM_EMAIL');
     $data['mail_from_name'] =  env('MAIL_FROM_NAME');
    // $data['receiver_email'] =  'XXXXXXX';//$invitee['email'];

       //Calling mail helper function
      MailHelper::sendMail($data);
anoop
  • 1,604
  • 6
  • 24
  • 50
  • 1
    You could swap out the templates being used based on some conditions. If you show us what you have tried so far, we could be more helpful. – Mozammil Jan 10 '19 at 13:49
  • do users have somekind of "role" and are recognizable? (example: role in their model, relatioship etc...) if yes, you can have different templates and in the handler take care of switch template depending on the receiver. – bLuke Jan 10 '19 at 13:49
  • @Mozammil That seems to be a good suggestion will try that. But if i am setting this in an array with first elemat contan the details of the first sender and his template subject etc and if we have an array like that do we need to loop the data 4 or 5 times to send mails or do we have an option to trigger all the 5 users mail send in a single mail call? – anoop Jan 10 '19 at 13:59

1 Answers1

1

Make either a table or hardcoded array with template renderers, then have those renderers render a twig/blade/php template based upon the variables you're supplying and all other variables you'd need for feeding into the mailer.

Then just loop through all your receiving candidates and render the appropriate emails with the correct renderer.

You'll have to make a few utility classes and all to accomplish this, but once you get it up and sorted it will be easy to manage and expand with more templates.

Just a rough outline of what I'd use

protected $renderers = [
  'templateA' => '\Foo\Bar\BazEmailRender',
  'templateB' => '\Foo\Bar\BbyEmailRender',
  'templateC' => '\Foo\Bar\BcxEmailRender',
];

public function getTemplate($name) 
{
    if(array_key_exists($name, $this->renderers)) {
        $clazz = $this->renderers[$name];
        return new $clazz();
    }
    return null;
}

public function handleEmails($list, $action) 
{
     $mailer = $this->getMailer();
     foreach($list as $receiver) {
        if(($template = $this->getTemplate($receiver->getFormat()))) {
            $template->setVars([
                 'action' => $action, 
                 'action_name' => $action->getName(),
                 'action_time' => $action->created_at,
                 // etc...
            ]);

            $mailer->send($receiver->email, $template->getSubject(), $template->getEmailBody());
         }
     }
}
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Instead of loop this data, do we have any option to send this in a single mail call and may be we push the mail send to a queue or something. If we loop the data here the processing will be late and users will get more time to get response? – anoop Jan 10 '19 at 14:04
  • You could put it into the scheduler, and have a cron job kick it every minute to check to email it. It's what I do to hand out customer requests to employees. Have a model that has a belongsTo to the action and the receiver and a boolean that sets sent or not. Then have the scheduler https://laravel.com/docs/5.7/scheduling handle the emailing as described above on all the not sent tasks `->where('sent',false)` and set them to `$task->sent = true;` afterwards or delete them if you don't need the log of who got what. – Tschallacka Jan 10 '19 at 14:47