CakePHP 2每次发送2封电子邮件

时间:2012-05-01 14:55:34

标签: email cakephp-2.0 html-email

在通过电子邮件向收件人发送联系表单时,我在CakePHP中遇到了一个奇怪的错误。当填写联系表格并提交时,CakePHP然后将电子邮件发送给收件人绝对罚款。但是,一旦它似乎同时发送两封电子邮件,而不是发送一封电子邮件。

我的控制器代码是:

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('recipient-email@gmail.com')
            ->from('postman@website.co.uk', '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();
         ?>

干杯!

1 个答案:

答案 0 :(得分: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('recipient-email@gmail.com')
        ->from('postman@website.co.uk', 'The Postman')


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

    }
}