2012-05-01 58 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

您正在使用哪个Web服务器? – debianek

+0

项目在XAMPP设置上本地托管。 – mickburkejnr

回答

1

将您的邮件发送代码放入if ($this->request->is('post')) {}并尝试。

UPDATE
更新了代码。

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

相关问题