0

通过电子邮件向收件人发送联系表单时,我在 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();
         ?>

干杯!

4

1 回答 1

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('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');
    }

    }
}
于 2012-05-02T03:38:39.823 回答