在我的代码中我试图使用PHPMailer从服务器发送邮件,但它不起作用。我的表单是。
<div class="span12 contact-form centered">
<h3>Say Hello</h3>
<div id="successSend" class="alert alert-success invisible">
<strong>Well done!</strong>Your message has been sent.</div>
<div id="errorSend" class="alert alert-error invisible">There was an error.</div>
<form id="contact-form" action="ink.php">
<div class="control-group">
<div class="controls">
<input class="span12" type="text" id="cname" name="cname" placeholder="* Company Name..." />
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="span12" type="text" id="name" name="name" placeholder="* Contact Person..." />
</div>
</div>
<div class="control-group">
<div class="controls">
<textarea class="span12" name="address" id="address" placeholder="* Address..."></textarea> </div>
</div>
<div class="control-group">
<div class="controls">
<input class="span12" type="text" id="mobile" name="mobile" placeholder="* Mobile No..." />
<div class="error left-align" id="err-mobile">Please enter Valid Mobile No.</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="span12" type="text" id="country" name="country" placeholder="* Country..." />
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="span12" type="text" id="city" name="city" placeholder="* City..." /> </div>
</div>
<div class="control-group">
<div class="controls">
<input class="span12" type="text" id="telephone" name="telephone" placeholder="* Telephone..." />
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="span12" type="email" name="email" id="email" placeholder="* Email..." />
<div class="error left-align" id="err-email">Please enter valid email adress.</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="span12" type="text" id="web" name="web" placeholder="* Website..." /> </div>
</div>
<div class="control-group">
<div class="controls">
<textarea class="span12" name="message" id="message" placeholder="* Message..."></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<button id="send-mail" class="message-btn">Send message</button>
</div>
</div>
</form>
</div>
我的php代码是ink.php。
<?php
include('config.php');
include('PHPMailer-master/PHPMailerAutoload.php');
$cname=$_POST['cname'];
$name=$_POST['name'];
$addr=$_POST['address'];
$mobile=$_POST['mobile'];
$country=$_POST['country'];
$city=$_POST['city'];
$telephone=$_POST['telephone'];
$email=$_POST['email'];
$web=$_POST['web'];
$msg=$_POST['message'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$data['success'] = false;
}
$mail = new PHPMailer;
//From email address and name
$mail->From = "inquiry@vkinds.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("jesadiyadivyesh@gmail.com", "Recepient Name");
//Address to which recipient will reply
$mail->addReplyTo("jesadiyadivyesh@gmail.com", "Reply");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = 'VK Industries Web Inquiry';
$mail->Body = '<html><head><title>VK Industries Web Inquiry</title></head><body><table><tr><td>Company Name : </td><td> '.$cname.'</td></tr><tr><td>Email id : </td><td> '.$userEmail.'</td></tr><tr><td>Person Name : </td><td> '.$name.'</td></tr><tr><td>Address : </td><td> '.$addr.'</td></tr><tr><td>Mobilr No : </td><td> '.$mobile.'</td></tr><tr><td>Country : </td><td> '.$Country.'</td></tr><tr><td>City : </td><td> '.$city.'</td></tr><tr><td>Telephone : </td><td> '.$telephone.'</td></tr><tr><td>Email id : </td><td> '.$email.'</td></tr><tr><td>Website : </td><td> '.$web.'</td></tr><tr><td>Message : </td><td> '.$msg.'</td></tr><tr></table></body></html>';
$mail->AltBody = $name;
if($mail->send()) {
$data['success'] = true;
} else {
$data['success'] = false;
}
echo json_encode($data);
?>
我也有jquery功能。
$("#send-mail").click(function () {
var nameCompare = /^([0-9+]{6,15})$/; // Syntax to compare against input
var name = $('input#mobile').val(); // get the value of the input field
var error = false;
if (name == "" || name == " " || !nameCompare.test(name)) {
$('#err-mobile').show(500);
$('#err-mobile').delay(4000);
$('#err-mobile').animate({
height: 'toggle'
}, 500, function () {
// Animation complete.
});
error = true; // change the error state to true
}
var emailCompare = /^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/; // Syntax to compare against input
var email = $('input#email').val().toLowerCase(); // get the value of the input field
if (email == "" || email == " " || !emailCompare.test(email)) {
$('#err-email').show(500);
$('#err-email').delay(4000);
$('#err-email').animate({
height: 'toggle'
}, 500, function () {
// Animation complete.
});
error = true; // change the error state to true
}
/* var comment = $('textarea#comment').val(); // get the value of the input field
if (comment == "" || comment == " ") {
$('#err-comment').show(500);
$('#err-comment').delay(4000);
$('#err-comment').animate({
height: 'toggle'
}, 500, function () {
// Animation complete.
});
error = true; // change the error state to true
}*/
if (error == false) {
var dataString = $('#contact-form').serialize(); // Collect data from form
$.ajax({
type: "POST",
url: $('#contact-form').attr('action'),
data: dataString,
timeout: 6000,
error: function (request, error) {
},
success: function (response) {
response = $.parseJSON(response);
if (response.success) {
$('#successSend').show();
$("#cname").val('');
$("#name").val('');
$("#address").val('');
$("#mobile").val('');
$("#country").val('');
$("#city").val('');
$("#telephone").val('');
$("#email").val('');
$("#web").val('');
$("#message").val('');
} else {
$('#errorSend').show();
}
}
});
return false;
}
return false; // stops user browser being directed to the php file
});
每次我发送消息时都显示成功消息,但在我的邮箱中没有收到邮件。