2

我正在尝试在我的 codeigniter 应用程序中实现联系表单。但我无法让它工作。我尝试了很多教程,但找不到错误。

我的视图文件。

<div id="contact">
    <div class="container">
        <div class="section-title">
            <h1>Contact Us</h1>
        </div>
        <div class="row">
            <div class="col-sm-10 col-sm-offset-1">
                <div class="row contact-details">
                    <div class="col-sm-4">
                        <span><i class="fa fa-map-marker"></i></span>
                        <p class="contact-info">1234 Street Name, City Name</p>
                    </div>
                    <div class="col-sm-4">
                        <span><i class="fa fa-phone"></i></span>
                        <p class="contact-info">+(123) 456-7890 </p>
                    </div>
                    <div class="col-sm-4">
                        <span><i class="fa fa-envelope"></i></span>
                        <a class="contact-info" href="#">contact@humanity.com</a>
                    </div>
                </div>
                <div class="contact-form">
                    <form id="contact-form" class="contact-form" name="contact-form" method="post" action="contactform/index">

                        <div class="row">
                            <div class="form-group col-sm-6">
                                <input type="text" name="name" class="form-control" required="required" placeholder="Name">
                            </div>
                            <div class="form-group col-sm-6">
                                <input type="email" name="email" class="form-control" required="required" placeholder="Email Address">
                            </div>
                            <div class="form-group col-sm-12">
                                <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
                            </div> 
                        </div>                                                 
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">Send Your Message</button>
                        </div>
                    </form> 
                </div>
            </div>
        </div>
    </div>
</div><!--/#Contact--> 

我的控制器类。

<?php
class ContactForm extends CI_Controller {
public function __construct() {
    parent::__construct ();
}

// Custom validation function to accept only alphabets and space input
function alpha_space_only($str) {
    if (! preg_match ( "/^[a-zA-Z ]+$/", $str )) {
        $this->form_validation->set_message ( 'alpha_space_only', 'The %s field must contain only alphabets and space!' );
        return FALSE;
    } else {
        return TRUE;
    }
}
function index() {
    // set validation rules
    $this->form_validation->set_rules ( 'name', 'Name', 'trim|required|callback_alpha_space_only' );
    $this->form_validation->set_rules ( 'email', 'Emaid ID', 'trim|required|valid_email' );
    $this->form_validation->set_rules ( 'message', 'Message', 'trim|required' );

    // run validation on form input
    if ($this->form_validation->run () == FALSE) {
        // validation fails
        $this->load->view('_main_layout');
    } else {
        // get the form data
        $name = $this->input->post ( 'name' );
        $from_email = $this->input->post ( 'email' );
        $message = $this->input->post ( 'message' );

        // set to_email id to which you want to receive mails
        $to_email = 'proisuru@outlook.com';

        // send mail
        $this->email->from ( $from_email, $name );
        $this->email->to ( $to_email );
        $this->email->message ( $message );
        if ($this->email->send ()) {
            // mail sent        
            print "<script type=\"text/javascript\">alert('Error: Email Sending Successful.');</script>";
        } else {
            // error
            print "<script type=\"text/javascript\">alert('Error: Email Sending Failed.');</script>";
        }
    }
 }
}

和 email.php 文件

<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = 'proisuru@gmail.com'; //change this
$config['smtp_pass'] = 'password'; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'UTF-8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
?>

我在这上面花了差不多三个小时。但还是找不到解决办法。

4

1 回答 1

0

在您的电子邮件配置文件中,您指定您希望您的电子邮件为 html:$config[mailtype] = 'html'但是当您发送电子邮件时,您将 设置$this->email->message为表单消息输入。当您在 Codeigniter 中发送 html 电子邮件(至少是我使用的版本)时,您需要发送一个完整的网页。

例如:

 $data['message'] = $this->input->post ( 'message' );
 $this->email->message($this->load->view('path/to/email/message_view',$data, true ));

然后在您看来,您可以执行任何您想要的 html,或者只是回显您的信息:

echo $message;

这是文档

于 2015-05-18T18:43:19.917 回答