0

連絡先フォームを電子メールで受信者に送信するときに、CakePHP で奇妙なエラーが発生しました。連絡先フォームに記入して送信すると、CakePHP は受信者に電子メールを送信します。しかし、一度に 1 つの電子メールを送信するのではなく、同時に 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('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 に答える