/**
  * Convenience method to send email
  *
  * @param string $from Sender email
  * @param string $to Receiver email
  * @param string $subject Subject
  * @param string $template Template to use
  * @param string $theme Theme to use
  * @param array  $viewVars Vars to use inside template
  * @param string $emailType user activation, reset password, used in log message when failing.
  * @return boolean True if email was sent, False otherwise.
  */
 protected function _sendEmail($from, $to, $subject, $template, $emailType, $theme = null, $viewVars = null)
 {
     if (is_null($theme)) {
         $theme = $this->theme;
     }
     $success = false;
     try {
         $email = new CakeEmail();
         $email->from($from[1], $from[0]);
         $email->to($to);
         $email->subject($subject);
         $email->template($template);
         $email->viewVars($viewVars);
         $email->theme($theme);
         $success = $email->send();
     } catch (SocketException $e) {
         $this->log(sprintf('Error sending %s notification : %s', $emailType, $e->getMessage()));
     }
     return $success;
 }
Ejemplo n.º 2
0
 /**
  * Send Email
  *
  * @param boolean $continue
  * @param array $contact
  * @return boolean
  * @access protected
  */
 protected function _send_email($continue, $contact)
 {
     $email = new CakeEmail();
     if (!$contact['Contact']['message_notify'] || $continue !== true) {
         return $continue;
     }
     $siteTitle = Configure::read('Site.title');
     try {
         $email->from($this->request->data['Message']['email'])->to($contact['Contact']['email'])->subject(__d('croogo', '[%s] %s', $siteTitle, $contact['Contact']['title']))->template('Contacts.contact')->viewVars(array('contact' => $contact, 'message' => $this->request->data));
         if ($this->theme) {
             $email->theme($this->theme);
         }
         if (!$email->send()) {
             $continue = false;
         }
     } catch (SocketException $e) {
         $this->log(sprintf('Error sending contact notification: %s', $e->getMessage()));
         $continue = false;
     }
     return $continue;
 }
Ejemplo n.º 3
0
 /**
  * sendEmail
  *
  * @param array $node Node data
  * @param array $comment Comment data
  * @return void
  * @access protected
  */
 protected function _sendEmail($node, $data)
 {
     $email = new CakeEmail();
     $commentId = $this->Comment->id;
     try {
         $email->from(Configure::read('Site.title') . ' ' . '<croogo@' . preg_replace('#^www\\.#', '', strtolower($_SERVER['SERVER_NAME'])) . '>')->to(Configure::read('Site.email'))->subject('[' . Configure::read('Site.title') . '] ' . __d('croogo', 'New comment posted under') . ' ' . $node['Node']['title'])->viewVars(compact('node', 'data', 'commentId'))->template('Comments.comment');
         if ($this->theme) {
             $email->theme($this->theme);
         }
         return $email->send();
     } catch (SocketException $e) {
         $this->log(sprintf('Error sending comment notification: %s', $e->getMessage()));
     }
 }
Ejemplo n.º 4
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;
     }
 }
 /**
  * admin_forgot_password method
  *
  * @return void
  */
 public function forgot_password()
 {
     if ($this->Session->read('Auth')) {
         $this->Session->setFlash(__('You are logged in!'), 'Flash/info');
         $this->redirect($this->referer());
     }
     if ($this->request->is('post')) {
         $this->User->recursive = -1;
         $conditions = array('User.email' => $this->request->data['User']['email']);
         $user = $this->User->find('first', compact('conditions'));
         if (!empty($user)) {
             $data = array('new_password_requested' => 'NOW()', 'new_password_hash' => 'MD5(' . time() . ')');
             $condition = array('User.id' => $user['User']['id']);
             $this->User->updateAll($data, $condition);
             //$this->User->recursive = -1;
             //$conditions = array('User.email'=>$this->request->data['User']['email']);
             $user = $this->User->find('first', compact('conditions'));
             $email = new CakeEmail('smtp');
             $email->to($user['User']['email']);
             $email->subject(__('Reset password from Locbit'));
             $email->template('Users/forgot_password', 'default');
             $email->helpers('Time', 'Html');
             $email->theme($this->theme);
             $email->viewVars(compact('user'));
             $email->send();
             $this->Session->setFlash(__('The recovery password email has been sent!'), 'Flash/success');
         } else {
             $this->Session->setFlash(__('The email you provided is not registered'), 'Flash/error');
         }
     }
 }