更新到PHP 7后,邮件脚本停止工作

时间:2018-12-20 11:00:52

标签: php email php-7

我有一封邮件脚本,在更新到PHP 7后停止工作。

但是我不知道为什么它停止工作,所以我使用ajax将其发布到该脚本,因此我看不到任何PHP错误,发布时在我的网络标签中只有500个错误。

这是我的脚本:

<?php
/* ==========================  Define variables ========================== */

#Your e-mail address
define("__TO__", "test@live.nl");

if ($_POST['contact-form'] == 'true') {
    #Message subject
    define("__SUBJECT__", "Contact aanvraag");
    $productpakket = 'Pakket';
}

if ($_POST['contact-form'] == 'false') {
    #Message subject
    define("__SUBJECT__", "Ketel/Informatie aanvraag");
    $productpakket = 'Product';
}

if ($_POST['offerte'] == 'true') {
    #Message subject
    define("__SUBJECT__", "Offerte aanvraag");
}

#Success message
define('__SUCCESS_MESSAGE__', "Uw bericht is verstuurd, bedankt!");

#Error message
define('__ERROR_MESSAGE__', "Error, uw bericht is niet verstuurd");

#Messege when one or more fields are empty
define('__MESSAGE_EMPTY_FILDS__', "Vult u alstublieft alle velden in");

/* ========================  End Define variables ======================== */

//Send mail function
function send_mail($to, $subject, $message, $headers) {
    if (@mail($to, $subject, $message, $headers)) {
        echo json_encode(array('info' => 'success', 'msg' => __SUCCESS_MESSAGE__));
    } else {
        echo json_encode(array('info' => 'error', 'msg' => __ERROR_MESSAGE__));
    }
}

//Check e-mail validation
function check_email($email) {
    if (!@eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
        return false;
    } else {
        return true;
    }
}

//Get post data
if (isset($_POST['name']) and isset($_POST['mail']) and isset($_POST['comment'])) {
    $hidden     = $_POST['hidden'];
    $name       = $_POST['name'];
    $mail       = $_POST['mail'];
    $website    = $_POST['website'];
    $comment    = $_POST['comment'];
    $over       = $_POST['over'];
    $adres      = $_POST['adres'];
    $woonplaats = $_POST['woonplaats'];
    $tel        = $_POST['telefoon'];
    $tijd       = $_POST['tijd'];
    $keteltype  = $_POST['ketel'];
    $offertevoor= $_POST['offertevoor'];
    $product    = $_POST['product'];
    $plaatsnaam = $_POST['plaatsnaam'];

    if ($name == '') {
        echo json_encode(array('info' => 'error', 'msg' => "U heeft geen naam ingevuld."));
        exit();
    } else if ($mail == '' or check_email($mail) == false) {
        echo json_encode(array('info' => 'error', 'msg' => "U heeft geen geldig email adres ingevoerd."));
        exit();
    } else if ($comment == '') {
        echo json_encode(array('info' => 'error', 'msg' => "U heeft geen bericht ingevuld."));
        exit();
    } else {
        //Send Mail

        if ($_POST['offerte'] != 'true') {
            if ($hidden == '') {
                $hidden_field = '';
            } else if ($hidden != '') {
                $hidden_field = '<tr style="height: 32px;">
                                  <th align="right" style="width:150px; padding-right:5px;">'.$productpakket.':</th>
                                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$hidden.'</td>
                                </tr>';
            }

            if ($over == '') {
                $over_field = '';
            } else if ($over != '') {
                $over_field = '<tr style="height: 32px;">
                                  <th align="right" style="width:150px; padding-right:5px;">Onderhouds contract:</th>
                                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$over.'</td>
                                </tr>';
            }

            $to      = __TO__;
            $subject = __SUBJECT__.' '.$name;
            $message = '
            <html>
            <head>
              <title>Mail from '.$name.'</title>
            </head>
            <body>
              <table style="width: 500px; font-family: arial; font-size: 14px;" border="1">
              '.$hidden_field.'
              '.$over_field.'
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Naam:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$name.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">E-mail:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$mail.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Telefoonnummer:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$website.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Plaatsnaam:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$plaatsnaam.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Bericht:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$comment.'</td>
                </tr>
              </table>
            </body>
            </html>
            ';
        } else {
            if ($hidden == '') {
                $hidden_field = '';
            } else if ($hidden != '') {
                $hidden_field = '<tr style="height: 32px;">
                                  <th align="right" style="width:150px; padding-right:5px;">Offerte voor:</th>
                                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$offertevoor.'</td>
                                </tr>';
            }

            if ($over == '') {
                $over_field = '';
            } else if ($over != '') {
                $over_field = '<tr style="height: 32px;">
                                  <th align="right" style="width:150px; padding-right:5px;">Onderhouds contract:</th>
                                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$over.'</td>
                                </tr>';
            }

            $to      = __TO__;
            $subject = __SUBJECT__.' '.$name;
            $message = '
            <html>
            <head>
              <title>Mail from '.$name.'</title>
            </head>
            <body>
              <table style="width: 500px; font-family: arial; font-size: 14px;" border="1">
              '.$hidden_field.'
              '.$over_field.'
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Naam:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$name.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Adres:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$adres.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Woonplaats:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$woonplaats.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Telefoon:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$telefoon.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">E-mail:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$mail.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Tijdsspan:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$tijd.'</td>
                </tr>
                <tr style="height: 32px;">
                  <th align="right" style="width:150px; padding-right:5px;">Ketel:</th>
                  <td align="left" style="padding-left:5px; line-height: 20px;">'.$keteltype.'</td>
                </tr>
              </table>
            </body>
            </html>
            ';
        }

        $headers = 'MIME-Version: 1.0'."\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8'."\r\n";
        $headers .= 'From: '.$mail."\r\n";

        if (send_mail($to, $subject, $message, $headers)) {
            echo json_encode(array('success' => 'error', 'msg' => 'Bedankt voor het opnemen van contact'));
        }
    }
} else {
    echo json_encode(array('info' => 'error', 'msg' => __MESSAGE_EMPTY_FILDS__));
}
?>

设置错误报告以显示所有错误并直接进入浏览器中的php文件也不会显示任何内容。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

问题(或至少第一个问题?)在check_mail函数的第一行代码中。

if (!@eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {

eregi()函数已被弃用,自PHP 7.0起已被删除。您需要将其替换为preg_match()或类似名称。

之所以没有显示错误消息,是因为您使用了@前缀,它告诉PHP抑制错误。正是由于这种原因,如今使用@运算符被认为是不好的做法。

可能还有其他问题;我没有机会进一步讲解代码,但是那个人立即跳了过来。