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

1 回答 1

1

使用模板渲染器制作一个表格或硬编码数组,然后让这些渲染器根据您提供的变量以及您需要输入邮件程序的所有其他变量来渲染一个 twig/blade/php 模板。

然后只需遍历所有接收候选人并使用正确的渲染器渲染适当的电子邮件。

您必须创建一些实用程序类来完成此操作,但是一旦您将其整理好并进行了排序,就可以轻松管理和扩展更多模板。

只是我将使用的粗略轮廓

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());
         }
     }
}
于 2019-01-10T13:57:28.450 回答