PHP:未发送邮件

时间:2016-02-23 04:28:14

标签: php

我正在尝试以下代码,但它一直在说Mail not sent。我如何找到真正的问题?代码如下:

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

$to = "mail@example.com";


$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: susan@example.com\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 :(得分:0)

试试此代码希望对您有所帮助。

&#13;
&#13;
<?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  = 'noreply@noreply.com';

$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 <nb158f@gmail.com>' . "\r\n";
$headers .= 'From: mysite.com <admin@site.com>' . "\r\n";
//$headers .= 'Cc: divakar.k@gmail.com' . "\r\n";

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