2012-05-01 2 views
0

Я встретил странную ошибку в CakePHP при отправке формы контакта получателю по электронной почте. Когда контактная форма заполняется и отправляется, CakePHP затем отправляет электронное письмо получателю абсолютно точно. Но вместо того, чтобы отправлять один адрес электронной почты, он, как представляется, отправляет два письма одновременно.CakePHP 2 отправляет 2 письма каждый раз

Мой код контроллера заключается в следующем:

var $helpers = array('Html', 'Form', 'Js'); 
var $components = array('Email', 'Session'); 

public function index() { 
    $this->layout = 'modal'; 

    if(isset($this->data['Contact'])) { 
     $userName = $this->data['Contact']['yourname']; 
     $userPhone = $this->data['Contact']['yourtelephone']; 
     $userEmail = $this->data['Contact']['youremail']; 
     $userMessage = $this->data['Contact']['yourquestion']; 

     $email = new CakeEmail(); 
     $email->viewVars(array(
       'userName' => $userName, 
       'userPhone' => $userPhone, 
       'userEmail' => $userEmail, 
       'userMessage' => $userMessage 
     )); 
     $email->subject(''.$userName.' has asked a question from the website'); 
     $email->template('expert', 'standard') 
      ->emailFormat('html') 
      ->to('[email protected]') 
      ->from('[email protected]', 'The Postman') 
      ->send(); 

     if ($email->send($userMessage)) { 
      $this->Session->setFlash('Thank you for contacting us'); 
     } 
     else { 
      $this->Session->setFlash('Mail Not Sent'); 
     } 

    } 
} 

И это код для формы обратной связи:

 <?php 
      echo $this->Form->create('Contact', array('url' => '/contact/ask-the-expert/', 'class' => 'contact')); 

      echo $this->Form->input('yourname', array(
       'type' => 'text', 
       'label' => 'Your Name <span class="star">*</span>', 
      )); 
      echo $this->Form->input('yourtelephone', array(
       'type' => 'text', 
       'label' => 'Your Telephone', 
      )); 
      echo $this->Form->input('youremail', array(
       'type' => 'text', 
       'label' => 'Your Email <span class="star">*</span>', 
      )); 
      echo $this->Form->input('yourquestionAnd ', array(
       'type' => 'textarea', 
       'label' => 'Your Question <span class="star">*</span>', 
      )); 
      echo $this->Form->submit(); 

      echo $this->Form->end(); 
     ?> 

Ура!

+0

Какой сервер вы используете? – debianek

+0

Проект размещается локально на настройке XAMPP. – mickburkejnr

ответ

1

Поместите свою почту Отправить код внутри if ($this->request->is('post')) {} и попробуйте.

ОБНОВЛЕНИЕ
обновленный код.

var $helpers = array('Html', 'Form', 'Js'); 
var $components = array('Email', 'Session'); 

public function index() { 
$this->layout = 'modal'; 

if(isset($this->data['Contact'])) { 
    $userName = $this->data['Contact']['yourname']; 
    $userPhone = $this->data['Contact']['yourtelephone']; 
    $userEmail = $this->data['Contact']['youremail']; 
    $userMessage = $this->data['Contact']['yourquestion']; 

    $email = new CakeEmail(); 
    $email->viewVars(array(
      'userName' => $userName, 
      'userPhone' => $userPhone, 
      'userEmail' => $userEmail, 
      'userMessage' => $userMessage 
    )); 
    $email->subject(''.$userName.' has asked a question from the website'); 
    $email->template('expert', 'standard') 
     ->emailFormat('html') 
     ->to('[email protected]') 
     ->from('[email protected]', 'The Postman') 


    if ($email->send($userMessage)) { 
     $this->Session->setFlash('Thank you for contacting us'); 
    } 
    else { 
     $this->Session->setFlash('Mail Not Sent'); 
    } 

    } 
} 
+0

Изменил это, и он по-прежнему доставляет два сообщения электронной почты. – mickburkejnr

+0

@mickburkejnr обновил код, удалив функцию 'Send()'. – Saanch

+0

Это исправлено, но мне не нужно было использовать 'if ($ this-> request-> is ('post')) {}'. – mickburkejnr