PHP邮件脚本不发送邮件

时间:2014-01-14 14:49:02

标签: php sendmail contact contact-form

我在网上发现了一个不适合我的PHP脚本,虽然它与其他有效的脚本几乎相同。我似乎无法解决问题,这是一个有效的方法:http://pastebin.com/L1YQ20qf

此外,我审查了电子邮件,并在支持PHP的PHP主机上对其进行了测试,因此不会出现问题。任何人吗?

    <?php
if($_POST)
{
    //check if its an ajax request, exit if not
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
        die();
    }

    $to_Email       = "censored@myemail.com"; //Replace with recipient email address
    $subject        = 'Contactformulier'; //Subject line for emails

    //check $_POST vars are set, exit if any missing
    if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userPhone"]) || !isset($_POST["userMessage"]))
    {
        die();
    }

    //Sanitize input data using PHP filter_var().
    $user_Name        = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
    $user_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
    $user_Phone       = filter_var($_POST["userPhone"], FILTER_SANITIZE_STRING);
    $user_Message     = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

    //additional php validation
    if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
    {
        header('HTTP/1.1 500 Naam te kort.');
        exit();
    }
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
    {
        header('HTTP/1.1 500 Controleer email adres.');
        exit();
    }
    if(!is_numeric($user_Phone)) //check entered data is numbers
    {
        header('HTTP/1.1 500 Alleen nummers als telefoonnummer.');
        exit();
    }
    if(strlen($user_Message)<5) //check emtpy message
    {
        header('HTTP/1.1 500 Bericht te kort.');
        exit();
    }

    //proceed with PHP email.
    $headers = 'From: '.$user_Email.'' . "rn" .
    'Reply-To: '.$user_Email.'' . "rn" .
    'X-Mailer: PHP/' . phpversion();

    @$sentMail = mail($to_Email, $subject, $user_Message .'  -'.$user_Name, $headers);

    if(!$sentMail)
    {
        header('HTTP/1.1 500 Kon het bericht niet verzenden, onze excuses!');
        exit();
    }else{
        echo 'Bericht verzonden!';
    }
}
?>

1 个答案:

答案 0 :(得分:1)

你不应该使用邮件()功能

使用mail()手动遵守电子邮件格式RFC非常困难。相反,请更新代码以使用PHPMailer或类似的库。

您的其他标头变量不正确 - 您必须拥有Content-typeMIME-Version标头:

//proceed with PHP email.
$headers = "From: $user_Email\n" .
"Reply-To: $user_Email\n" .
"MIME-Version: 1.0\n" .
"Content-Type: text/html\n" .
"X-Mailer: PHP/" . phpversion();

$sentMail = @mail($to_Email, $subject, $user_Message .'  -'.$user_Name, $headers);