简体   繁体   中英

What is the best option to send emails in laravel with different email template

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);

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());
         }
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM