簡體   English   中英

輸入某些文本后,PHP Mail表單不起作用

[英]PHP Mail form doesn't work when certain text is entered

我正在處理PHP郵件表單,由於某些原因,只有某些文本無法發送。 它讓我感到困惑,甚至想不出如何用Google回答。

例如,如果我輸入表格:

 Name: Indiana Jones
 Number: 000
 Email: indiana@jones.com

郵件功能不起作用,但是如果我將000更改為999 ,它將起作用。 或者,如果我將indiana@jones.com更改為indiana@jones.net ,它也會突然indiana@jones.net

這是從我的HTML中獲取的片段:

<form method="post" action="mail.php">

Name<br />
<input style="width:878px;" type="text" name="name" id="name" required>
Best Contact Number<br />
<input style="width:415px;" type="text" name="number" id="number" required>
Email<br />
<input style="width:415px;" type="email" name="email" id="email" required>

</form>

從我的PHP(mail.php):(個人電子郵件地址已刪除)

<?php
ini_set("SMTP","mail.email.com.au");
ini_set('sendmail_from', 'email@email.com.au'); 

$to = "email@email.com.au";
$subject = "Mail Form";

$message = "Name:" . "\n" . $_POST['name'] . "\n\n";
$message .= "Best Contact Number:" . "\n" . $_POST['number'] . "\n\n";
$message .= "Email:" . "\n" . $_POST['email'] . "\n\n";
$headers = "From: Removed <email@email.com.au> \n";

if(@mail($to, $subject, $message, $headers))
{
echo "<script type='text/javascript'>alert('Thank you for your submission'); window.location.href = 'index.html';</script>";} 
else 
{
    echo "<script type='text/javascript'>alert('There was an error - please check all fields and try again'); window.location.href = 'index.html';</script>";
}
?>

好吧,我剛剛得到了一個代碼,可以使它運行起來很棒。

    <?php

    if(isset($_POST['email'])) {

        // EDIT THE 2 LINES BELOW AS REQUIRED

        $email_to = "email@email.com.au";

        $email_subject = "Mail Form";
        function died($error) {

            // your error code can go here

            echo "<script type='text/javascript'>alert('There was an error because '); window.location.href = 'index.html';</script>";
            die();
        }
        // validation expected data exists

        if(!isset($_POST['name']) ||

            !isset($_POST['number']) ||

            !isset($_POST['email'])) {

            died('<script type='text/javascript'>alert('There was an error - please check all fields and try again'); window.location.href = 'index.html';</script>');       

        }
        $name = $_POST['name']; // required

        $email = $_POST['email']; // required

        $number = $_POST['number']; // required

        $error_message = "";

        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

      if(!preg_match($email_exp,$email_from)) {

        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

      }

        $string_exp = "/^[A-Za-z .'-]+$/";

      if(!preg_match($string_exp,$first_name)) {

        $error_message .= 'The Name you entered does not appear to be valid.<br />';

      }

      if(strlen($comments) < 0) {

        $error_message .= 'The Number you entered do not appear to be valid.<br />';

      }

      if(strlen($error_message) > 0) {

        died($error_message);

      }

        $email_message = "Form details below.\n\n";

        function clean_string($string) {

          $bad = array("content-type","bcc:","to:","cc:","href");

          return str_replace($bad,"",$string);

        }
        $email_message .= "Name: ".clean_string($name)."\n";

        $email_message .= "Email: ".clean_string($email)."\n";

        $email_message .= "Number: ".clean_string($number)."\n";

    // create email headers

    $headers = 'From: '.$email_from."\r\n".

    'Reply-To: '.$email_from."\r\n" .

    'X-Mailer: PHP/' . phpversion();

    @mail($email_to, $email_subject, $email_message, $headers);  

    ?>
<!-- if sent successfuly -->
    <script type='text/javascript'>alert('Thank you for your submission'); window.location.href = 'index.html';</script>

    <?php

    }

    ?>

您可以添加此代碼為用戶顯示錯誤

echo $error."<br /><br />";

抱歉,事實證明這是Exchange服務器問題。 我不知道為什么要讓某些消息通過並過濾其他消息,但是我們的IT團隊對其進行了整理,不得不允許網站的IP通過。

表單現在可以正常使用了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM