我无法发送邮件

时间:2014-08-17 14:05:21

标签: php jquery html css ajax

***我使用下面的代码,但不幸的是,我无法发送邮件。

邮件显示为已发送,但不是。 *

<script type="text/javascript">
$(document).ready(function() {
$("#submit_quote").click(function() { 
//get input field values
var user_name = $('input[name=name]').val(); 
var user_phone = $('input[name=phone]').val();
var user_email = $('input[name=email]').val();
var user_company = $('input[name=company]').val();
var user_from = $('input[name=from]').val();
var user_des = $('input[name=des]').val();
var user_items = $('input[name=items]').val();
var user_message = $('textarea[name=message]').val();
//simple validation at client's end
//we simply change border color to red if empty field using .css()
var proceed = true;
if(user_name==""){ 
$('input[name=name]').css('border-color','red'); 
proceed = false;
}
if(user_phone=="") { 
$('input[name=phone]').css('border-color','red'); 
proceed = false;
}
if(user_email==""){ 
$('input[name=email]').css('border-color','red'); 
proceed = false;
}
if(user_company==""){ 
$('input[name=company]').css('border-color','red'); 
proceed = false;
}
if(user_from==""){ 
$('input[name=from]').css('border-color','red'); 
proceed = false;
}
if(user_des==""){ 
$('input[name=des]').css('border-color','red'); 
proceed = false;
}
if(user_items==""){ 
$('input[name=items]').css('border-color','red'); 
proceed = false;
}
if(user_message=="") { 
$('textarea[name=message]').css('border-color','red'); 
proceed = false;
}

//everything looks good! proceed...
if(proceed) 
{
//data to be sent to server
post_data = {'userName':user_name, 'userPhone':user_phone, 'userEmail':user_email, 'userCompany':user_company, 'userFrom':user_from, 'userDest':user_des, 'userItems':user_items, 'userMessage':user_message};
//Ajax post data to server
$.post('quote_me.php', post_data, function(response){
//load json data from server and output message 
if(response.type == 'error')
{
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">'+response.text+'</div>';

//reset values in all input fields
$('#quoteform input').val(''); 
$('#quoteform textarea').val(''); 
}

$("#result").hide().html(output).slideDown();
}, 'json');

}
});

//reset previously set border colors and hide all message on .keyup()
$("#quoteform input, #quoteform textarea").keyup(function() { 
$("#quoteform input, #quoteform textarea").css('border-color',''); 
$("#result").slideUp();
});

});
</script>
<script type="text/javascript">
var messageDelay = 5000; // How long to display status messages (in milliseconds)

// Init the form once the document is ready
$( init );
function init() {
// Initialize the form
$('#quoteform').submit( submitForm );
}
// Submit the form via Ajax

function submitForm() {
var quoteform = $(this);

// Are all the fields filled in?

if ( !$('#name').val() || !$('#phone').val() || !$('#email').val() || !$('#from').val() || !$('#des').val() || !$('#items').val() || !$('#message').val() ) {

// No; display a warning message and return to the form
$('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
quoteform.fadeOut().delay(messageDelay).fadeIn();

} else {

// Yes; submit the form to the PHP script via Ajax

$('#sendingMessage').fadeIn();
quoteform.fadeOut();

$.ajax( {
url: quoteform.attr( 'action' ) + "?ajax=true",
type: quoteform.attr( 'method' ),
data: quoteform.serialize(),
success: submitFinished
} );
}

// Prevent the default form submission occurring
return false;
}
// Handle the Ajax response

function submitFinished( response ) {
response = $.trim( response );
$('#sendingMessage').fadeOut();

if ( response == "success" ) {

// Form submitted successfully:
// 1. Display the success message
// 2. Clear the form fields
// 3. Fade the content back in

$('#successMessage').fadeIn().delay(messageDelay).fadeOut();
$('#name').val( "" );
$('#phone').val( "" );
$('#email').val( "" );
$('#company').val( "" );
$('#from').val( "" );
$('#des').val( "" );
$('#items').val( "" );
$('#message').val( "" );

} else {

// Form submission failed: Display the failure message,
// then redisplay the form
$('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
$('#quoteform').delay(messageDelay+500).fadeIn();
}
}

</script> 
<div id="switch">
<div class="content-switcher">
<h4 style="text-align:center">Quick Quote</h4>
<div id="result"></div>
<form id="quoteform" action="quote_me.php" method="post">
<ul class="header">
<li><input type="text" name="name" id="name" placeholder="Name *" autocomplete="off" tabindex="1" required/></li>
<li><input type="tel" name="phone" id="phone" placeholder="Phone number *" autocomplete="off" tabindex="2" required/></li>
<li><input type="text" name="email" id="email" placeholder="Email *" autocomplete="off" tabindex="3" required/></li>
<li><input type="text" name="company" id="company" placeholder="Company" autocomplete="off" tabindex="4"/></li>
<li><input type="text" name="from" id="from" placeholder="From *" autocomplete="off" tabindex="5" required/></li>
<li><input type="text" name="des" id="des" placeholder="Destination *" autocomplete="off" tabindex="6" required/></li>
<li><input type="text" name="items" id="items" placeholder="Items *" autocomplete="off" tabindex="7" required/></li>
<li><textarea name="message" id="message" placeholder="Message" tabindex="8" ></textarea></li>
<li><span class="input-group-btn"><button class="btn btn-default" type='submit' value='Submit' name='submit_quote' id="submit_quote" style="width:120px;height:40px;">SEND</button></span>
</li>
</ul>
</form>
<div id="sendingMessage" class="statusMessage"><p>Sending your message. Please wait...</p></div>
<div id="successMessage" class="statusMessage"><p>Thanks for sending your message! We'll get back to you shortly.</p></div>
<div id="failureMessage" class="statusMessage"><p>There was a problem sending your message. Please try again.</p></div>
<div id="incompleteMessage" class="statusMessage"><p>Please complete all the fields in the form before sending.</p></div>
<div class="clear"></div>
<div id="hide"><i class="fa fa-times"></i></div>
</div> 
</div>
<div id="show" style="display: block;">
<div id="setting"><i class="fa fa-edit"></i></div> 
</div>

这是quote-me.php文件

<?php

// Define some constants
define( "RECIPIENT_NAME", "XXXXX XXXX" );
define( "RECIPIENT_EMAIL", "info@xxx.com" );
define( "EMAIL_SUBJECT", "Visitor's Message from Quote" );

// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderPhone = isset( $_POST['phone'] ) ? preg_replace( "/[^\.\-\' 0-9]/", "", $_POST['phone'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$senderCompany = isset( $_POST['company'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['company'] ) : "";
$senderFrom = isset( $_POST['from'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['from'] ) : "";
$senderDes = isset( $_POST['des'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['des'] ) : "";
$senderItems = isset( $_POST['items'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['items'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";


// $sender_message .= "Phone No: $senderPhone.\n";
// $sender_message .= "Email: $senderEmail.\n";
// $sender_message .= "Company Name: $senderCompany.\n";
// $sender_message .= "From: $senderFrom.\n";
// $sender_message .= "Destinstion: $senderDes.\n";
// $sender_message .= "Items to be delivered: $senderItems.\n";
// $sender_message .= "Message: $message.\n";

// If all values exist, send the email
if ( $senderName && $senderPhone && $senderEmail && $senderFrom && $senderDes && $senderItems && $message) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}

// Return an appropriate response to the browser
if ( isset($_GET["ajax"]) ) {
echo $success ? "success" : "error";
} else {
?>
<html>
<head>
<title>Thanks!</title>
</head>
<body>
<?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
<?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
<p>Click your browser's Back button to return to the page.</p>
</body>
</html>
<?php
}
?>

**非常感谢任何帮助 - 我被困住了。有人能告诉我问题在哪里吗?

0 个答案:

没有答案