PHP邮件功能无法完成电子邮件的发送

<?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message'];
摘要由CSDN通过智能技术生成

本文翻译自:PHP mail function doesn't complete sending of e-mail

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: yoursite.com';
    $to = 'contact@yoursite.com';
    $subject = 'Customer Inquiry';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) {
            echo '<p>Your message has been sent!</p>';
        } else {
            echo '<p>Something went wrong, go back and try again!</p>';
        }
    }
?>

I've tried creating a simple mail form. 我尝试创建一个简单的邮件表单。 The form itself is on my index.html page, but it submits to a separate "thank you for your submission" page, thankyou.php , where the above PHP code is embedded. 表单本身在我的index.html页面上,但是它提交到单独的“感谢您提交”页面, thankyou.php ,上面的PHP代码嵌入其中。 The code submits perfectly, but never sends an email. 该代码可以完美提交,但从不发送电子邮件。 How can I fix this? 我怎样才能解决这个问题?


#1楼

参考:https://stackoom.com/question/1fP8u/PHP邮件功能无法完成电子邮件的发送


#2楼

Although there are portions of this answer that apply to only to the usage of the mail() function itself, many of these troubleshooting steps can be applied to any PHP mailing system. 尽管此答案的某些部分仅适用于使用mail()函数本身,但是许多故障排除步骤都可以应用于任何PHP邮件系统。

There are a variety of reasons your script appears to not be sending emails. 有多种原因导致您的脚本似乎不发送电子邮件。 It's difficult to diagnose these things unless there is an obvious syntax error. 除非存在明显的语法错误,否则很难诊断这些问题。 Without one you need to run through the checklist below to find any potential pitfalls you may be encountering. 如果没有,您需要仔细阅读以下清单,以查找您可能遇到的潜在陷阱。

Make sure error reporting is enabled and set to report all errors 确保已启用错误报告并设置为报告所有错误

Error reporting is essential to rooting out bugs in your code and general errors that PHP encounters. 错误报告对于根除代码中的错误以及PHP遇到的一般错误至关重要。 Error reporting needs to be enabled to receive these errors. 需要启用错误报告以接收这些错误。 Placing the following code at the top of your PHP files (or in a master configuration file) will enable error reporting. 将以下代码放在PHP文件的顶部(或主配置文件中)将启用错误报告。

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

See this Stack Overflow answer for more details on this. 有关更多详细信息,请参见此堆栈溢出答案

Make sure the mail() function is called 确保已调用mail()函数

It may seem silly but a common error is to forget to actually place the mail() function in your code. 看起来很愚蠢,但是一个常见的错误是忘记将mail()函数实际放在代码中。 Make sure it is there and not commented out. 确保它在那里并且没有被注释掉。

Make sure the mail() function is called correctly 确保正确调用了mail()函数

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) 布尔邮件(字符串$ to,字符串$ subject,字符串$ message [,字符串$ additional_headers [,字符串$ additional_parameters]])

The mail function takes three required parameters and optionally a fourth and fifth one. 邮件功能需要三个必需的参数,还可以选择第四个和第五个参数。 If your call to mail() does not have at least three parameters it will fail. 如果您对mail()调用没有至少三个参数,它将失败。

If your call to mail() does not have the correct parameters in the correct order it will also fail. 如果您对mail()调用没有正确顺序的正确参数,则也会失败。

Check the server's mail logs 检查服务器的邮件日志

Your web server should be logging all attempts to send emails through it. 您的Web服务器应该记录所有通过它发送电子邮件的尝试。 The location of these logs will vary (you may need to ask your server administrator where they are located) but they can commonly be found in a user's root directory under logs . 这些日志的位置会有所不同(您可能需要询问服务器管理员,它们位于何处),但通常可以在用户根目录下的logs下找到它们。 Inside will be error messages the server reported, if any, related to your attempts to send emails. 服务器中报告的错误消息(如果有)与您发送电子邮件的尝试有关。

Check for Port connection failure 检查端口连接失败

Port block is a very common problem which most developers face while integrating their code to deliver emails using SMTP. 端口阻塞是大多数开发人员在集成其代码以使用SMTP传递电子邮件时面临的一个非常普遍的问题。 And, this can be easily traced at the server maillogs (the location of server of mail log can vary from server to server, as explained above). 并且,可以轻松地在服务器邮件日志中找到该邮件(如上所述,邮件日志的服务器位置可能因服务器而异)。 In case you are on a shared hosting server, the ports 25 and 587 remain blocked by default. 如果您在共享主机服务器上,则默认情况下端口25和587仍处于阻止状态。 This block is been purposely done by your hosting provider. 此阻止是由您的托管服务提供商有意完成的。 This is true even for some of the dedicated servers. 即使对于某些专用服务器也是如此。 When these ports are blocked, try to connect using port 2525. If you find that port is also blocked, then the only solution is to contact your hosting provider to unblock these ports. 当这些端口被阻止时,请尝试使用端口2525进行连接。如果您发现该端口也被阻止,则唯一的解决方案是与主机提供商联系以解除对这些端口的阻止。

Most of the hosting providers block these email ports to protect their network from sending any spam emails. 大多数托管服务提供商会阻止这些

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值