简体   繁体   中英

PHP: Mail not sent

I am trying on the following code but it keeps saying Mail not sent . How do I find the real issue? Code given below:

    $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";
}

Try this code hope this helpful.

 <?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>'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM