コード例 #1
0
ファイル: Notification.php プロジェクト: allanth4/taxibo
 /**
  * Send the notification.
  *
  * This method sends the notification to all recipients, rendering the template with the corresponding parameters for each recipient.
  * @throws phpmailerException The message could not be sent.
  */
 public function send()
 {
     $mail = new \PHPMailer(true);
     $mail->CharSet = 'UTF-8';
     $mail->From = $this->from_email;
     $mail->FromName = $this->from_name;
     $mail->addReplyTo($this->reply_to_email, $this->reply_to_name);
     $twig = static::$app->view()->getEnvironment();
     // Loop through email recipients, sending customized content to each one
     foreach ($this->email_recipients as $recipient) {
         $mail->addAddress($recipient->getEmail(), $recipient->getName());
         // Add any CCs and BCCs
         if ($recipient->getCCs()) {
             foreach ($recipient->getCCs() as $cc) {
                 $mail->addCC($cc['email'], $cc['name']);
             }
         }
         if ($recipient->getBCCs()) {
             foreach ($recipient->getBCCs() as $bcc) {
                 $mail->addBCC($bcc['email'], $bcc['name']);
             }
         }
         $params = $recipient->getParams();
         // Must manually merge in global variables for block rendering
         $params = array_merge($twig->getGlobals(), $params);
         $mail->Subject = $this->template->renderBlock('subject', $params);
         $contentHtml = $this->template->renderBlock('body', $params);
         $mailHtml = TaxiMail::getMailHtml($params['headerLine1'], $params['headerLine2'], $contentHtml, $params['footerLine']);
         $mail->Body = $mailHtml;
         $mail->isHTML(true);
         // Set email format to HTML
         // Send mail as SMTP, if desired
         if (static::$app->config('mail') == 'smtp') {
             $config = static::$app->config('smtp');
             $mail->isSMTP(true);
             $mail->Host = $config['host'];
             $mail->Port = $config['port'];
             $mail->SMTPAuth = $config['auth'];
             $mail->SMTPSecure = $config['secure'];
             $mail->Username = $config['user'];
             $mail->Password = $config['pass'];
         }
         // Try to send the mail.  Will throw an exception on failure.
         error_log("sending...");
         $mail->send();
         // Clear all PHPMailer recipients (from the message for this iteration)
         $mail->clearAllRecipients();
     }
 }