英文:
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,或者有些地方不匹配。
...
有谁知道可能出了什么问题吗?
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
```<?php
session_cache_limiter( 'nocache' );
header( 'Expires: ' . gmdate( 'r', 0 ) );
header( 'Content-type: application/json' );
$to = 'myemail@gmail.com'; // put your email here
$email_template = 'simple.html';
$subject = strip_tags($_POST['subject']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$name = strip_tags($_POST['name']);
$message = nl2br( htmlspecialchars($_POST['message'], ENT_QUOTES) );
$result = array();
if(empty($name)){
$result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong>&nbsp; Name is empty.' );
echo json_encode($result );
die;
}
if(empty($email)){
$result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong>&nbsp; Email is empty.' );
echo json_encode($result );
die;
}
if(empty($message)){
$result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Message body is empty.' );
echo json_encode($result );
die;
}
$headers = "From: " . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$templateTags = array(
'{{subject}}' => $subject,
'{{email}}'=>$email,
'{{phone}}'=>$phone,
'{{name}}'=>$name,
'{{message}}'=>$message
);
$templateContents = file_get_contents( dirname(__FILE__) . '/email-templates/'.$email_template);
$contents = strtr($templateContents, $templateTags);
if ( mail( $to, $subject, $contents, $headers ) ) {
$result = array( 'response' => 'success', 'message'=>'<strong>Thank You!</strong>&nbsp; Your email has been delivered.' );
} else {
$result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.' );
}
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.
<!-- Contact Section -->
<section id="contact" class="contact-section section-padding">
<div class="container">
<h2 class="section-title wow fadeInUp">Get in touch</h2>
<div class="row">
<div class="col-md-6">
<div class="contact-form">
<strong>Send me a message</strong>
<form name="contact-form" id="contactForm" action="sendemail.php" method="POST">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" required="">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" class="form-control" id="email" required="">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="phone" name="phone" class="form-control" id="phone" required="">
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input type="text" name="subject" class="form-control" id="subject">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" class="form-control" id="message" rows="5" required=""></textarea>
</div>
<button type="submit" name="submit" class="btn btn-primary">Send Message</button>
</form>
</div><!-- /.contact-form -->
</div><!-- /.col-md-6 -->
<div class="col-md-6">
<div class="row center-xs">
<div class="col-sm-6">
<i class="fa fa-map-marker"></i>
<address>
<strong>Address/Street</strong>
Find me online,<br>
Anywhere, Really<br>
</address>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-12">
<!--div class="location-map">
<div id="mapCanvas" class="map-canvas"></div>
</div-->
</div>
</div>
</div>
</div><!-- /.row -->
</div><!-- /.container -->
</section><!-- End Contact Section -->
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["SERVER_PROTOCOL"] . ' 500 Internal Server Error', true, 500);
exit;
To debug the error from the mail function itself, this post explains:
$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
答案2
得分: 0
你的脚本返回 HTTP 200,但邮件功能未成功。尝试将以下行替换:
$result = array( 'response' => 'error', 'message' => '<strong>Error!</strong> Can\'t Send Mail.' );
改为:
$result = array( 'response' => 'error', 'message' => '<strong>Error!</strong> Can\'t Send Mail.', 'reason' => error_get_last()['message'] );
将其添加到数组中,这将显示错误信息:
'reason' => error_get_last()['message']
这样你就能看到为什么邮件未发送。
希望这对你有所帮助
英文:
Your script respond HTTP 200, with error cause mail function doesn't success.
Try replace line:
$result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.' );
with:
$result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.', 'reason' => error_get_last()['message'] );
added in to array, what will show the error
'reason' => error_get_last()['message']
and you will see why your mail doesn't sent.
I hope it help to you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论