从codeigniter发送电子邮件

时间:2015-05-18 15:49:34

标签: codeigniter email codeigniter-form-helper

我正在尝试在我的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
?>

我在这上花了三个小时。但仍无法找到解决方案。

1 个答案:

答案 0 :(得分: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;

Here is the documentation