在CakePHP中发送电子邮件后,重定向无法正常工作

时间:2018-02-10 10:13:27

标签: email redirect cakephp-2.0

我正在CakePHP 2中以Contact-Us形式发送电子邮件。邮件正在正常发送,但问题是在发送电子邮件后它没有重定向到主页。我正在使用SMTP发送电子邮件。

我的控制器方法

public function contactForm()
{
    if ($this->request->is(['post','put'])) {
        $name = $this->request->data['home']['name'];       
        $email = $this->request->data['home']['email'];
        $phone = $this->request->data['home']['phone'];
        $message = $this->request->data['home']['message'];

        $emailMessage = "Name - $name\n";
        $emailMessage .= "Email - $email\n";
        $emailMessage .= "Phone - $phone\n";
        $emailMessage .= "Message - $message\n";


        $Email = new CakeEmail();
        $Email->config('smtp');
        $Email->from(array($email =>$name));
        $Email->to('info@myserver.com');
        $Email->subject('Contact Us - Form');
        $Email->send($emailMessage);
        $this->Session->setFlash('Thank for contacting us. Our representative will reach you soon.','flash', array('alert'=> 'success'));
        $this->redirect(array('action' => 'index'));
    }           
}

索引方法也在那里。这是我的HTML表单

<?php echo $this->Form->create('home', array( 'controller' => 'homes', 'action' => 'contactForm','name'=>'contact-form','id'=>'contact-form','class'=>'contact-form'));?>
            <div class="row flex-box">
                <fieldset class="column column-1-2">
                    <label>YOUR NAME</label>
                    <?php echo $this->Form->input('name',array('type'=>'text','id'=>'name','value'=>'','label' => false,'class'=>'text-input validate[required]','div'=>false));?>
                    <label>YOUR EMAIL</label>
                    <?php echo $this->Form->input('email',array('type'=>'email','id'=>'email','value'=>'','label' => false,'class'=>'text-input validate[required]','div'=>false));?>
                    <label>YOUR PHONE</label>
                    <?php echo $this->Form->input('phone',array('type'=>'tel','id'=>'phone','value'=>'','label' => false,'class'=>'text-input validate[required]','div'=>false));?>
                </fieldset>
                <fieldset class="column column-1-2">
                    <label>MESSAGE</label>
                    <?php echo $this->Form->input('message',array('type'=>'textarea','id'=>'content','value'=>'','div'=>false));?>
                </fieldset>
            </div>
            <div class="row margin-top-30">
                <div class="column column-1-1">
                    <div class="row margin-top-15 padding-bottom-16 align-center">
                        <button type="submit" class="more submit-contact-form" style="display:inline-block;">Send message</button>
                    </div>
                </div>
            </div>
        </form>

我正在接收邮件。无法区分错误是什么。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

请检查您是否正确关闭了PHP控制器文件,关闭后应该没有空格?&gt;控制器文件中的PHP标记。

同时检查其他控制器。