我已经由其他人编写了此代码,问题是在发送电子邮件时始终显示“错误发送!”。即使在大多数情况下,电子邮件发送成功也是如此。
查看(表单提交):
<!-- Contact Form Starts -->
<div class="col s12 m7 l9 xl9 rightside">
<h6 class="uppercase m-none">Feel free to drop me a line</h6>
<div class="row">
<p class="col s12 m12 l7 xl7 second-font">
If you have any suggestions or consultations, please fill out the form below and I will reply shortly.
</p>
</div>
<form class="contactform" method="post" action="<?php echo site_url('en/send') ?>">
<!-- Name Field Starts -->
<div class="input-field second-font">
<i class="fa fa-user prefix"></i>
<input id="name" name="name" type="text" class="validate" required>
<label class="font-weight-400" for="name">Your Name</label>
</div>
<!-- Name Field Ends -->
<!-- Email Field Starts -->
<div class="input-field second-font">
<i class="fa fa-Email prefix"></i>
<input id="email" type="email" name="email" class="validate" required>
<label for="email">Your Email</label>
</div>
<!-- Email Field Ends -->
<!-- Start Message Textarea Starts -->
<div class="input-field second-font">
<i class="fa fa-comments prefix"></i>
<textarea id="message" name="message" class="materialize-textarea" required></textarea>
<label for="message">Your message</label>
</div>
<!-- Message Textarea Ends -->
<!-- Submit Form Button Starts -->
<div class="col s12 m12 l4 xl4 submit-form">
<button class="btn font-weight-500" type="submit" name="send">
Send Message <i class="fa fa-send"></i>
</button>
</div>
<!-- Submit Form Button Ends -->
<div class="col s12 m12 l8 xl8 form-message">
<span class="output_message center-align font-weight-500 uppercase"></span>
</div>
</form>
</div>
<!-- Contact Form Ends -->
控制器(发送功能):
function send()
{
$this->load->library('email');
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$body = '
<strong>Name: </strong>'.$this->input->post("name").'<br />
<strong>Email: </strong>'.$this->input->post("email").'<br />
<strong>Message: </strong>'.$this->input->post("message").'<br />
';
$emailto = 'anemail@email.com'; //any email
$this->load->library('email');
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to($emailto);
$this->email->subject('New Request');
$this->email->message($body);
$this->email->send();
$data['contactus'] = $this->Emodel->contactus('en');
$data['pages'] = $this->Emodel->get_pages('en');
$data['social'] = $this->Emodel->get_header_social();
$data['result'] = "Message Sent Successfully";
$data['_title'] = "Contact";
$this->load->view('en/home',$data);
}
JS文件:
(function($) {
"use strict";
$(document).ready(function() {
// PRELOADER
$("body").toggleClass("loaded");
setTimeout(function() {
$("body").addClass("loaded");
}, 3000);
// PORTFOLIO DIRECTION AWARE HOVER EFFECT
var item = $("#bl-work-items>div");
var elementsLength = item.length;
for (var i = 0; i < elementsLength; i++) {
$(item[i]).hoverdir();
}
// TEXT ROTATOR
$("#selector").animatedHeadline({
animationType: "clip"
});
// BOX LAYOUT
Boxlayout.init();
// REMOVE # FROM URL
$("a[href='#']").on("click", (function(e) {
e.preventDefault();
}));
// AJAX CONTACT FORM
$(".contactform").on("submit", function() {
$(".output_message").text("Loading...");
var form = $(this);
$.ajax({
url: form.attr("action"),
method: form.attr("method"),
data: form.serialize(),
success: function(result) {
if (result == "success") {
$(".contactform").find(".output_message").addClass("success");
$(".output_message").text("Message Sent!");
} else {
$(".contactform").find(".output_message").addClass("error");
$(".output_message").text("Error Sending!");
}
}
});
return false;
});
// MATERIAL CAROUSEL
$(".carousel.carousel-slider").carousel({
fullWidth: true,
indicators: true,
});
// RESUME CARDS ANIMATION
if ($(window).width() > 800) {
$(".resume-list-item, .resume-card").on("click", function() {
$(".resume-list-item").removeClass("is-active");
var e = parseInt($(this).data("index"),10);
$("#resume-list-item-" + e).addClass("is-active");
var t = e + 1,
n = e - 1,
i = e - 2;
$(".resume-card").removeClass("front back up-front up-up-front back-back"), $(".resume-card-" + e).addClass("front"), $(".resume-card-" + t).addClass("back"), $(".resume-card-" + n).addClass("back-back"), $(".resume-card-" + i).addClass("back")
});
}
});
})(jQuery);
我对ajax和jquery不太了解,所以我复制了整个代码
答案 0 :(得分:1)
由于您的控制器未将成功数据传递给视图,因此请使用此代码。
function send()
{
$this->load->library('email');
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$body = '
<strong>Name: </strong>'.$this->input->post("name").'<br />
<strong>Email: </strong>'.$this->input->post("email").'<br />
<strong>Message: </strong>'.$this->input->post("message").'<br />
';
$emailto = 'anemail@email.com'; //any email
$this->load->library('email');
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to($emailto);
$this->email->subject('New Request');
$this->email->message($body);
if ($this->email->send()) {
echo 'success';
} else {
echo 'error';
}
}