输入特定文本时,PHP Mail表单不起作用

时间:2014-06-30 05:33:41

标签: php html email post

我正在处理PHP邮件表单,由于某些原因,只有某些文本无法发送。让我感到困惑,我甚至无法想到谷歌如何回答。

例如,如果我输入表格:

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

邮件功能无法正常工作,但如果我将000更改为999,则可以正常运行。 或者如果我将indiana@jones.com更改为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>";
}
?>

2 个答案:

答案 0 :(得分:0)

好吧,我刚刚得到了一个能让它运行良好的代码。

    <?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 />";

答案 1 :(得分:0)

很抱歉,事实证明这是Exchange服务器问题。我不知道为什么它会让一些消息通过并过滤其他消息,但我们的IT团队将其分类并且必须允许网站的IP通过。

表格现在已经完全正常运作了!