网络响应 200,但收到错误消息;PHP 发送电子邮件脚本

huangapple go评论89阅读模式
英文:

Network response 200, but getting Error Message; PHP Send Email Script

问题

Based on your request, here's the translated part of your content:

我在某处找到了这个PHP代码,一切似乎都没问题,但每次尝试发送测试邮件时都会收到错误消息;然而,网络消息显示它已发送。请查看图片和代码片段。请帮助。谢谢

PHP脚本

...
HTML表单 - 这是我的HTML代码。我的表单可能有问题吗?但我认为问题出在PHP,或者有些地方不匹配。

...

网络响应 200,但收到错误消息;PHP 发送电子邮件脚本

网络响应 200,但收到错误消息;PHP 发送电子邮件脚本

有谁知道可能出了什么问题吗?


Please note that I have provided the translated text for the code and HTML portions as per your request. If you have any specific questions or need further assistance, please let me know.

<details>
<summary>英文:</summary>

I got this PHP code somewhere and everything seems ok, but I keep getting an error message every-time I try send a test email; However, the network message says that it did send. Please see the images and code snippets. Please help. Thanks

PHP SCRIPT 

```&lt;?php
   

    session_cache_limiter( &#39;nocache&#39; );
    header( &#39;Expires: &#39; . gmdate( &#39;r&#39;, 0 ) );
    header( &#39;Content-type: application/json&#39; );

    $to         = &#39;myemail@gmail.com&#39;;  // put your email here

    $email_template = &#39;simple.html&#39;;

    $subject    = strip_tags($_POST[&#39;subject&#39;]);
    $email       = strip_tags($_POST[&#39;email&#39;]);
    $phone      = strip_tags($_POST[&#39;phone&#39;]);
    $name       = strip_tags($_POST[&#39;name&#39;]);
    $message    = nl2br( htmlspecialchars($_POST[&#39;message&#39;], ENT_QUOTES) );
    $result     = array();


    if(empty($name)){

        $result = array( &#39;response&#39; =&gt; &#39;error&#39;, &#39;empty&#39;=&gt;&#39;name&#39;, &#39;message&#39;=&gt;&#39;&lt;strong&gt;Error!&lt;/strong&gt;&amp;nbsp; Name is empty.&#39; );
        echo json_encode($result );
        die;
    } 

    if(empty($email)){

        $result = array( &#39;response&#39; =&gt; &#39;error&#39;, &#39;empty&#39;=&gt;&#39;email&#39;, &#39;message&#39;=&gt;&#39;&lt;strong&gt;Error!&lt;/strong&gt;&amp;nbsp; Email is empty.&#39; );
        echo json_encode($result );
        die;
    } 

    if(empty($message)){

         $result = array( &#39;response&#39; =&gt; &#39;error&#39;, &#39;empty&#39;=&gt;&#39;message&#39;, &#39;message&#39;=&gt;&#39;&lt;strong&gt;Error!&lt;/strong&gt;&amp;nbsp; Message body is empty.&#39; );
         echo json_encode($result );
         die;
    }
    


    $headers  = &quot;From: &quot; . $name . &#39; &lt;&#39; . $email . &#39;&gt;&#39; . &quot;\r\n&quot;;
    $headers .= &quot;Reply-To: &quot;. $email . &quot;\r\n&quot;;
    $headers .= &quot;MIME-Version: 1.0\r\n&quot;;
    $headers .= &quot;Content-Type: text/html; charset=UTF-8\r\n&quot;;


    $templateTags =  array(
        &#39;{{subject}}&#39; =&gt; $subject,
        &#39;{{email}}&#39;=&gt;$email,
        &#39;{{phone}}&#39;=&gt;$phone,
        &#39;{{name}}&#39;=&gt;$name,
        &#39;{{message}}&#39;=&gt;$message
        );


    $templateContents = file_get_contents( dirname(__FILE__) . &#39;/email-templates/&#39;.$email_template);

    $contents =  strtr($templateContents, $templateTags);

    if ( mail( $to, $subject, $contents, $headers ) ) {
        $result = array( &#39;response&#39; =&gt; &#39;success&#39;, &#39;message&#39;=&gt;&#39;&lt;strong&gt;Thank You!&lt;/strong&gt;&amp;nbsp; Your email has been delivered.&#39; );
    } else {
        $result = array( &#39;response&#39; =&gt; &#39;error&#39;, &#39;message&#39;=&gt;&#39;&lt;strong&gt;Error!&lt;/strong&gt;&amp;nbsp; Cann\&#39;t Send Mail.&#39;  );
}


    echo json_encode( $result );

    die;

HTML FORM - Here is my HTML CODE. Could there be something wrong with my form? I however think it's the PHP, or something doesn't match.

&lt;!-- Contact Section --&gt;
    &lt;section id=&quot;contact&quot; class=&quot;contact-section section-padding&quot;&gt;
      &lt;div class=&quot;container&quot;&gt;
        &lt;h2 class=&quot;section-title wow fadeInUp&quot;&gt;Get in touch&lt;/h2&gt;

        &lt;div class=&quot;row&quot;&gt;
          &lt;div class=&quot;col-md-6&quot;&gt;
            &lt;div class=&quot;contact-form&quot;&gt;
              &lt;strong&gt;Send me a message&lt;/strong&gt;
           
              &lt;form name=&quot;contact-form&quot; id=&quot;contactForm&quot; action=&quot;sendemail.php&quot; method=&quot;POST&quot;&gt;

                &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label for=&quot;name&quot;&gt;Name&lt;/label&gt;
                  &lt;input type=&quot;text&quot; name=&quot;name&quot; class=&quot;form-control&quot; id=&quot;name&quot; required=&quot;&quot;&gt;
                &lt;/div&gt;

                &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
                  &lt;input type=&quot;email&quot; name=&quot;email&quot; class=&quot;form-control&quot; id=&quot;email&quot; required=&quot;&quot;&gt;
                &lt;/div&gt;
                
                &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label for=&quot;phone&quot;&gt;Phone&lt;/label&gt;
                  &lt;input type=&quot;phone&quot; name=&quot;phone&quot; class=&quot;form-control&quot; id=&quot;phone&quot; required=&quot;&quot;&gt;
                &lt;/div&gt;

                &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label for=&quot;subject&quot;&gt;Subject&lt;/label&gt;
                  &lt;input type=&quot;text&quot; name=&quot;subject&quot; class=&quot;form-control&quot; id=&quot;subject&quot;&gt;
                &lt;/div&gt;

                &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label for=&quot;message&quot;&gt;Message&lt;/label&gt;
                  &lt;textarea name=&quot;message&quot; class=&quot;form-control&quot; id=&quot;message&quot; rows=&quot;5&quot; required=&quot;&quot;&gt;&lt;/textarea&gt;
                &lt;/div&gt;

                &lt;button type=&quot;submit&quot; name=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Send Message&lt;/button&gt;
              &lt;/form&gt;
            &lt;/div&gt;&lt;!-- /.contact-form --&gt;
          &lt;/div&gt;&lt;!-- /.col-md-6 --&gt;

          &lt;div class=&quot;col-md-6&quot;&gt;
            &lt;div class=&quot;row center-xs&quot;&gt;
              &lt;div class=&quot;col-sm-6&quot;&gt;
                &lt;i class=&quot;fa fa-map-marker&quot;&gt;&lt;/i&gt;
                &lt;address&gt;
                  &lt;strong&gt;Address/Street&lt;/strong&gt;
                  Find me online,&lt;br&gt;
                  Anywhere, Really&lt;br&gt;
                &lt;/address&gt;
              &lt;/div&gt;

              &lt;div class=&quot;col-sm-6&quot;&gt;
                
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;div class=&quot;row&quot;&gt;
            &lt;div class=&quot;col-sm-12&quot;&gt;
              &lt;!--div class=&quot;location-map&quot;&gt;
                &lt;div id=&quot;mapCanvas&quot; class=&quot;map-canvas&quot;&gt;&lt;/div&gt;
              &lt;/div--&gt;
            &lt;/div&gt;
          &lt;/div&gt;

          &lt;/div&gt;
        &lt;/div&gt;&lt;!-- /.row --&gt;
      &lt;/div&gt;&lt;!-- /.container --&gt;
    &lt;/section&gt;&lt;!-- End Contact Section --&gt;

网络响应 200,但收到错误消息;PHP 发送电子邮件脚本

网络响应 200,但收到错误消息;PHP 发送电子邮件脚本

Anyone with a clue what could be wrong?

答案1

得分: 1

你得到200错误的原因是因为你自己捕获了错误并回显了输出。如果你希望将错误显示为500,也许可以尝试在错误的if/else块中添加以下代码:

header($_SERVER["SERVER_PROTOCOL"] . ' 500 Internal Server Error', true, 500);
exit;

要从邮件函数本身调试错误,可以参考这篇文章:

$success = mail('example@example.com', 'My Subject', $message);
if (!$success) {
    $errorMessage = error_get_last()['message'];
}

https://stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function

英文:

The reason you are getting a 200 error is due to you catching the error yourself and echoing the output. If you want the error to be as a 500, maybe try adding this in the error if/else:

header($_SERVER[&quot;SERVER_PROTOCOL&quot;] . &#39; 500 Internal Server Error&#39;, true, 500);  
exit;

To debug the error from the mail function itself, this post explains:

$success = mail(&#39;example@example.com&#39;, &#39;My Subject&#39;, $message);
if (!$success) {
    $errorMessage = error_get_last()[&#39;message&#39;];
}

https://stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function

答案2

得分: 0

你的脚本返回 HTTP 200,但邮件功能未成功。尝试将以下行替换:

$result = array( 'response' => 'error', 'message' => '<strong>Error!</strong>&nbsp; Can\'t Send Mail.' );

改为:

$result = array( 'response' => 'error', 'message' => '<strong>Error!</strong>&nbsp; Can\'t Send Mail.', 'reason' => error_get_last()['message'] );

将其添加到数组中,这将显示错误信息:

'reason' => error_get_last()['message']

这样你就能看到为什么邮件未发送。

希望这对你有所帮助 网络响应 200,但收到错误消息;PHP 发送电子邮件脚本

英文:

Your script respond HTTP 200, with error cause mail function doesn't success.
Try replace line:

$result = array( &#39;response&#39; =&gt; &#39;error&#39;, &#39;message&#39;=&gt;&#39;&lt;strong&gt;Error!&lt;/strong&gt;&amp;nbsp; Cann\&#39;t Send Mail.&#39;  );

with:

$result = array( &#39;response&#39; =&gt; &#39;error&#39;, &#39;message&#39;=&gt;&#39;&lt;strong&gt;Error!&lt;/strong&gt;&amp;nbsp; Cann\&#39;t Send Mail.&#39;, &#39;reason&#39; =&gt; error_get_last()[&#39;message&#39;] );

added in to array, what will show the error

&#39;reason&#39; =&gt; error_get_last()[&#39;message&#39;]

and you will see why your mail doesn't sent.

I hope it help to you 网络响应 200,但收到错误消息;PHP 发送电子邮件脚本

huangapple
  • 本文由 发表于 2020年1月3日 22:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定