我正在尝试在我的 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
?>
我在这上面花了差不多三个小时。但还是找不到解决办法。