2016-02-23 152 views
0

我想在下面的代碼,但它一直說Mail not sent。我如何找到真正的問題?代碼如下:PHP:郵件沒有發送

$full_name = htmlspecialchars(trim($_POST['full_name'])); 
    $email = htmlspecialchars(trim($_POST['email'])); 
    $phone = htmlspecialchars(trim($_POST['phone'])); 
    $message = htmlspecialchars(trim($_POST['message'])); 

$to = "[email protected]"; 


$subject = $full_name . ' is interested to have a business discussion with you'; 

$headers = "From: " . strip_tags($_POST['email']) . "\r\n"; 
$headers .= "Reply-To: " . strip_tags($_POST['email']) . "\r\n"; 
// $headers .= "CC: [email protected]\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

$message = '<html><body>'; 
$message .= '<h3>Full Name: </h3>: ' . $full_name . '<br>'; 
$message .= '<h3>Phone: </h3>: ' . $phone . '<br>'; 
$message .= '<h3>Message: </h3>: ' . $message . '<br><br>=======================<br>'; 
$message .= '<h3>IP Address: </h3>: ' . $ip . '<br>'; 
$message .= '</body></html>'; 

if(mail($to, $subject, $message, $headers)) 
{ 
    echo "Mail Sent Successfully"; 
}else{ 
    echo " Mail Not Sent"; 
} 
+1

哪裏是$變量的值? –

+0

一個電子郵件地址。 – Volatil3

+0

您的'$ to'在哪裏?您還在哪裏分配變量'$ full_name' – StreetCoder

回答

0

試試這個代碼希望這是很有幫助的。

<?php 
 
//print_r($_POST); 
 

 
//$fname = $_POST['fname']; 
 
//$lname = $_POST['lname']; 
 
//$email = $_POST['email']; 
 
//$message = $_POST['message']; 
 

 

 
if(isset($_POST['fname']) && $_POST['fname'] != ''){ 
 
    $fname = $_POST['fname']; 
 
} 
 
if(isset($_POST['lname']) && $_POST['lname'] != ''){ 
 
    $lname = $_POST['lname'];//phone number 
 
} 
 
if(isset($_POST['email']) && $_POST['email'] != ''){ 
 
    $email = $_POST['email']; 
 
} 
 

 
if(isset($_POST['message']) && $_POST['message'] != ''){ 
 
    $com = $_POST['message']; 
 
} 
 

 
$to = '[email protected]'; 
 

 
$subject = 'Site Visiter.'; 
 

 
// message 
 
$message = sprintf("Site visiter details: <br>Name:- %s <br> Phone:- %s <br> Email:- %s<br> Message:- %s",$fname,$lname,$email,$com); 
 

 
// To send HTML mail, the Content-type header must be set 
 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
 

 
// Additional headers 
 
$headers .= 'To: Mary <[email protected]>' . "\r\n"; 
 
$headers .= 'From: mysite.com <[email protected]>' . "\r\n"; 
 
//$headers .= 'Cc: [email protected]' . "\r\n"; 
 

 
// Mail it 
 
$flag = mail($to, $subject, $message, $headers); 
 
echo '<script>alert("Mail Sent :");</script>'; 
 
echo '<script>window.location.href="index.php";</script>';