Example #1
0
 /**
  * メールを送信する
  *
  * @param string $to 送信先アドレス
  * @param string $title タイトル
  * @param mixed $body 本文
  * @param array $options オプション
  * @return bool 送信結果
  */
 public function sendMail($to, $title = '', $body = '', $options = array())
 {
     $options = array_merge(array('agentTemplate' => true, 'template' => 'default'), $options);
     if (!empty($this->siteConfigs['smtp_host'])) {
         $transport = 'Smtp';
         $host = $this->siteConfigs['smtp_host'];
         $port = $this->siteConfigs['smtp_port'] ? $this->siteConfigs['smtp_port'] : 25;
         $username = $this->siteConfigs['smtp_user'] ? $this->siteConfigs['smtp_user'] : null;
         $password = $this->siteConfigs['smtp_password'] ? $this->siteConfigs['smtp_password'] : null;
         $tls = $this->siteConfigs['smtp_tls'] && $this->siteConfigs['smtp_tls'] == 1;
     } else {
         $transport = 'Mail';
         $host = 'localhost';
         $port = 25;
         $username = null;
         $password = null;
         $tls = null;
     }
     $config = array('transport' => $transport, 'host' => $host, 'port' => $port, 'username' => $username, 'password' => $password, 'tls' => $tls);
     $cakeEmail = new CakeEmail($config);
     // charset
     if (!empty($this->siteConfigs['mail_encode'])) {
         $encode = $this->siteConfigs['mail_encode'];
     } else {
         $encode = 'ISO-2022-JP';
     }
     // ISO-2022-JPの場合半角カナが文字化けしてしまうので全角に変換する
     if ($encode == 'ISO-2022-JP') {
         $title = mb_convert_kana($title, 'KV', "UTF-8");
         if (is_string($body)) {
             $body = mb_convert_kana($body, 'KV', "UTF-8");
         } elseif (isset($body['message']) && is_array($body['message'])) {
             foreach ($body['message'] as $key => $val) {
                 if (is_string($val)) {
                     $body['message'][$key] = mb_convert_kana($val, 'KV', "UTF-8");
                 }
             }
         }
     }
     //CakeEmailの内部処理のencodeを統一したいので先に値を渡しておく
     $cakeEmail->headerCharset($encode);
     $cakeEmail->charset($encode);
     //$format
     if (!empty($options['format'])) {
         $cakeEmail->emailFormat($options['format']);
     } else {
         $cakeEmail->emailFormat('text');
     }
     //bcc 'mail@example.com,mail2@example.com'
     if (!empty($options['bcc'])) {
         // 文字列の場合
         $bcc = array();
         if (is_string($options['bcc'])) {
             if (strpos($options['bcc'], ',') !== false) {
                 $bcc = explode(',', $options['bcc']);
             } else {
                 $bcc[] = $options['bcc'];
             }
             // 配列の場合
         } elseif (is_array($options['bcc'])) {
             $bcc = $options['bcc'];
         }
         foreach ($bcc as $val) {
             if (Validation::email(trim($val))) {
                 $cakeEmail->addBcc($val);
             }
         }
         unset($bcc);
     }
     //cc 'mail@example.com,mail2@example.com'
     if (!empty($options['cc'])) {
         // 文字列の場合
         $cc = array();
         if (is_string($options['cc'])) {
             if (strpos($options['cc'], ',') !== false) {
                 $cc = explode(',', $options['cc']);
             } else {
                 $cc[] = $options['cc'];
             }
             // 配列の場合
         } elseif (is_array($options['cc'])) {
             $cc = $options['cc'];
         }
         foreach ($cc as $val) {
             if (Validation::email(trim($val))) {
                 $cakeEmail->addCc($val);
             }
         }
         unset($cc);
     }
     // to 送信先アドレス (最初の1人がTOで残りがBCC)
     if (strpos($to, ',') !== false) {
         $_to = explode(',', $to);
         $i = 0;
         if (count($_to) >= 1) {
             foreach ($_to as $val) {
                 if ($i == 0) {
                     $cakeEmail->addTo($val);
                     $toAddress = $val;
                 } else {
                     $cakeEmail->addBcc($val);
                 }
                 ++$i;
             }
         }
     } else {
         $cakeEmail->addTo($to);
     }
     // 件名
     $cakeEmail->subject($title);
     //From
     $fromName = $from = '';
     if (!empty($options['from'])) {
         $from = $options['from'];
     } else {
         if (!empty($this->siteConfigs['email'])) {
             $from = $this->siteConfigs['email'];
             if (strpos($from, ',') !== false) {
                 $from = explode(',', $from);
             }
         } else {
             $from = $toAddress;
         }
     }
     if (!empty($options['fromName'])) {
         $fromName = $options['fromName'];
     } else {
         if (!empty($this->siteConfigs['formal_name'])) {
             $fromName = $this->siteConfigs['formal_name'];
         } else {
             $formalName = Configure::read('BcApp.title');
         }
     }
     $cakeEmail->from($from, $fromName);
     //Reply-To
     if (!empty($options['replyTo'])) {
         $replyTo = $options['replyTo'];
     } else {
         $replyTo = $from;
     }
     $cakeEmail->replyTo($replyTo);
     //Return-Path
     if (!empty($options['returnPath'])) {
         $returnPath = $options['returnPath'];
     } else {
         $returnPath = $from;
     }
     $cakeEmail->returnPath($returnPath);
     //$sender
     if (!empty($options['sender'])) {
         $cakeEmail->sender($options['sender']);
     }
     //$theme
     if ($this->theme) {
         $cakeEmail->theme($this->theme);
     }
     if (!empty($options['theme'])) {
         $cakeEmail->theme($options['theme']);
     }
     //viewRender (利用するviewクラスを設定する)
     $cakeEmail->viewRender('BcApp');
     //template
     if (!empty($options['template'])) {
         $layoutPath = $subDir = $plugin = '';
         if ($options['agentTemplate'] && Configure::read('BcRequest.agent')) {
             $layoutPath = Configure::read('BcRequest.agentPrefix');
             $subDir = Configure::read('BcRequest.agentPrefix');
         }
         list($plugin, $template) = pluginSplit($options['template']);
         if ($subDir) {
             $template = "{$subDir}/{$template}";
         }
         if (!empty($plugin)) {
             $template = "{$plugin}.{$template}";
         }
         if (!empty($options['layout'])) {
             $cakeEmail->template($template, $options['layout']);
         } else {
             $cakeEmail->template($template);
         }
         $content = '';
         if (is_array($body)) {
             $cakeEmail->viewVars($body);
         } else {
             $cakeEmail->viewVars(array('body' => $body));
         }
     } else {
         $content = $body;
     }
     // $attachments tmp file path
     $attachments = array();
     if (!empty($options['attachments'])) {
         if (!is_array($options['attachments'])) {
             $attachments = array($options['attachments']);
         } else {
             $attachments = $options['attachments'];
         }
     }
     $cakeEmail->attachments($attachments);
     try {
         $cakeEmail->send($content);
         return true;
     } catch (Exception $e) {
         $this->log($e->getMessage());
         return false;
     }
 }
 function view_certificate()
 {
     if (empty($this->request->data)) {
         return 'This certificate cannot be generated.';
     } else {
         $is_tutorial = false;
         $is_quiz = false;
         $this->set('tutorial_grades', array());
         $this->set('quiz_grades', array());
         if (array_key_exists('tutorial_id', $this->request->data) && is_numeric($this->request->data['tutorial_id'])) {
             $tutorial = $this->Tutorial->read(null, $this->request->data['tutorial_id']);
             if ($tutorial['Tutorial']['certificate']) {
                 $is_tutorial = true;
                 $subject = $tutorial['Tutorial']['title'];
                 if (!isset($this->request->data['questions'])) {
                     $this->request->data['questions'] = array();
                 }
                 $this->set('tutorial_grades', $this->Tutorial->grade($this->request->data['tutorial_id'], $this->request->data['questions']));
             }
         }
         if (!isset($this->request->data['free-response'])) {
             $this->request->data['free-response'] = array();
         }
         $this->set('free_responses', $this->request->data['free-response']);
         if (array_key_exists('quiz_id', $this->request->data) && is_numeric($this->request->data['quiz_id'])) {
             $final_quiz = $this->Tutorial->FinalQuiz->read(null, $this->request->data['quiz_id']);
             if ($final_quiz['FinalQuiz']['certificate']) {
                 $is_quiz = true;
                 $subject = $final_quiz['Tutorial']['title'];
                 if (!isset($this->request->data['questions'])) {
                     $this->request->data['questions'] = array();
                 }
                 $this->set('quiz_grades', $this->Tutorial->FinalQuiz->grade($this->request->data['quiz_id'], $this->request->data['questions']));
             }
         }
         // parse supplied email fields
         if ($is_tutorial || $is_quiz) {
             $to_string = '';
             if (!empty($tutorial['Tutorial']['certificate_email'])) {
                 $to_string .= $tutorial['Tutorial']['certificate_email'];
             }
             if (!empty($this->request->data['certificate_email'])) {
                 if (!empty($to_string)) {
                     $to_string .= ',';
                 }
                 $to_string .= $this->request->data['certificate_email'];
             }
             $this->set('date', date('F j, Y'));
             $this->set('time', date('g:ia'));
             $this->set('name', Sanitize::paranoid($this->request->data['certificate_name'], array(' ')));
             $this->set('title', $subject);
             $message = new CakeEmail('default');
             $message->subject("Certificate of Completion for {$subject}");
             $message->template('certificate_of_completion');
             $message->emailFormat('html');
             $message->viewVars($this->viewVars);
             $to_array = explode(',', $to_string);
             foreach ($to_array as $key => $to) {
                 if ($to == 'Emailaddresses' || empty($to)) {
                     unset($to_array[$key]);
                     continue;
                 }
                 $message->addBcc($to);
             }
             try {
                 $message->send();
                 $email_success = true;
             } catch (Exception $e) {
                 $email_success = $e->getMessage();
             }
             $this->viewPath = 'Emails/html';
             $this->layout = 'Emails/html/default';
             $this->set('dialog', true);
             if (empty($to_array)) {
                 $this->Session->setFlash('This certificate could not be emailed because no email addresses' . ' were supplied.');
             } else {
                 if (true === $email_success) {
                     $this->Session->setFlash('The following message was sent.');
                 } else {
                     $this->Session->setFlash('The following message could <strong>not</strong> be sent.');
                 }
             }
             return $this->render('certificate_of_completion');
         } else {
             return 'This certificate cannot be generated.';
         }
     }
 }