简体   繁体   中英

Why is this PHP script not sending the e-mail?

Note: E-Mail address removed for privacy purposes.

Whenever the form executes this script, it returns the success JSON array at the end, however, the e-mail is never received at the e-mail address specified.

<?php

if(empty($_POST['fullname']) || empty($_POST['phonenumber']) || empty($_POST['emailaddress']))
    {
    $response = array('status' => 0, 'txt' => 'Please verify all required fields are complete.');
    echo json_encode($response);
    die();
    }

if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['emailaddress'])))
    {
    $response = array('status' => 0, 'txt' => 'Please Provide a Valid E-Mail Address.');
    echo json_encode($response);
    die();
    }

$name = mysql_real_escape_string($_POST['fullname']);
$phonenumber = mysql_real_escape_string($_POST['phonenumber']);
$email = mysql_real_escape_string($_POST['emailaddress']);
$comments = mysql_real_escape_string($_POST['comments']);

$emailbody = 'Name: ' . $name . ' 
Phone Number: ' . $phonenumber . ' 
E-Mail Address: ' . $email . ' 

Comments: ' . $comments . ' ';

mail("example@example.com","New Consultation Request",$emailbody,"From: noreply@example.com"); 

$response = array('status' => 1, 'txt' => 'consultation-request-successful');
echo json_encode($response);

?>

You've never bothered connecting to the database, so mysql_real_escape_string() is going to be turning a boolean FALSE for failure - it REQUIRES an active connection to the DB to do its work.

That means your $name, $phonenumber, $email, $comments are all going to be boolean FALSE, and translated into an empty string when you build your mail text.

As well, using mysql escaping for a simple email is beyond utterly pointless. Email is not vulnerable to SQL injection attacks.

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