PHP邮件功能不完成电子邮件的发送 [英] PHP mail function doesn't complete sending of e-mail

查看:171
本文介绍了PHP邮件功能不完成电子邮件的发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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>'; 
        }
    }
?>

我尝试创建一个简单的邮件表单。表单本身在我的 index.html 页面上,但提交到单独的感谢您的提交页面, thankyou.php ,其中嵌入了上述php代码。
代码提交完美,但不会发送电子邮件。请帮助。

I've tried creating a simple mail form. The form itself is on my index.html page, but submits to a separate "thank you for your submission" page, thankyou.php, where the above php code is embedded. The code submits perfectly, but never sends an email. please help.

推荐答案

您的脚本似乎没有发送电子邮件有多种原因。除非有明显的语法错误,否则很难诊断这些事情。没有一个,您需要通过下面的清单来查找您可能遇到的任何潜在的陷阱。

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.

错误报告对于根除您的代码中的错误和PHP遇到的一般错误至关重要。需要启用错误报告以接收这些错误。将以下代码放置在PHP文件(或主配置文件)的顶部将启用错误报告。

Error reporting is essential to rooting out bugs in your code and general errors that PHP encounters. 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.

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

请参阅

See this Stack Overflow answer for more details on this.

这可能看起来很愚蠢,但常见的错误是忘记将 mail()函数放在你的码。确保它在那里,没有注释掉。

It may seem silly but a common error is to forget to actually place the mail() function in your code. Make sure it is there and not commented out.

您的Web服务器应该是记录通过它发送电子邮件的所有尝试。这些日志的位置将有所不同(您可能需要询问您的服务器管理员位于何处),但通常可以在 logs 下的用户根目录中找到。

Your web server should be logging all attempts to send emails through it. 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. Inside will be error messages the server reported, if any, related to your attempts to send emails.

端口块是大多数开发人员在将其代码集成到使用SMTP发送电子邮件时面临的常见问题。而且,这可以很容易地在服务器邮件日志中跟踪(如上所述,邮件日志服务器的位置可能因服务器而异)。如果您处于共享托管服务器,默认情况下端口25和587将保持阻止。这个程序段是由您的主机提供商进行的。即使对于某些专用服务器也是如此。当这些端口被阻止时,尝试使用端口2525进行连接。如果您发现该端口也被阻止,则唯一的解决方案是联系您的主机提供商以解除这些端口。

Port block is a very common problem which most developers face while integrating their code to deliver emails using 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. 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.

大多数主机提供商阻止这些电子邮件端口来保护他们的网络不发送任何垃圾邮件。

Most of the hosting providers block these email ports to protect their network from sending any spam emails.

使用端口25或587进行普通/ TLS连接,端口465用于SSL连接。对于大多数用户,建议使用端口587来避免一些主机提供商设置的速率限制。

Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections. For most users, it is suggested to use port 587 to avoid rate limits set by some hosting providers.

错误抑制操作符 @ 是PHP中的表达式,可能会忽略该表达式生成的任何错误消息。有些情况下使用此运算符是必需的,但发送邮件不是其中之一。

When the error suppression operator @ is prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. There are circumstances where using this operator is necessary but sending mail is not one of them.

如果您的代码包含 @mail(...)那么你可能会隐藏重要的错误消息,这将帮助你调试这个。删除 @ ,看看是否有任何错误报告。

If your code contains @mail(...) then you may be hiding important error messages that will help you debug this. Remove the @ and see if any errors are reported.

只有当您检查与 error_get_last()

邮件() 功能:


返回 TRUE 如果邮件成功接受送达, FALSE 否则。重要的是要注意,只是因为邮件被接受送达,这并不意味着邮件实际上会达到预定的目的地。

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

这很重要,因为:


  • 如果您收到 FALSE 返回值你知道您的服务器接受邮件的错误。这可能不是编码问题,而是服务器配置问题。您需要与系统管理员联系,以了解为什么会发生这种情况。

  • 如果您收到 TRUE 返回值,则不会意味着您的电子邮件一定会被发送。这只是意味着邮件已成功通过PHP发送到服务器上的相应处理程序。在PHP控制之外还有更多的失败点可以导致不发送电子邮件。

  • If you receive a FALSE return value you know the error lies with your server accepting your mail. This probably isn't a coding issue but a server configuration issue. You need to speak to your system administrator to find out why this is happening.
  • If your receive a TRUE return value it does not mean your email will definitely be sent. It just means the email was sent to its respective handler on the server successfully by PHP. There are still more points of failure outside of PHP's control that can cause the email to not be sent.

所以 FALSE 将帮助您指向正确的方向,而 TRUE 不一定意味着您的电子邮件已成功发送。这很重要!

So FALSE will help point you in the right direction whereas TRUE does not necessarily mean your email was sent successfully. This is important to note!

许多共享的webhosts,特别是免费的webhosting提供商,不允许从他们的服务器发送电子邮件,或限制在任何给定的时间段内可以发送的数量。这是因为他们努力限制垃圾邮件发送者利用便宜的服务。

Many shared webhosts, especially free webhosting providers, either do not allow emails to be sent from their servers or limit the amount that can be sent during any given time period. This is due to their efforts to limit spammers from taking advantage of their cheaper services.

如果您认为您的主机有电子邮件限制或阻止发送电子邮件,请查看他们的常见问题,看看是否列出了这些限制。否则,您可能需要联系他们的支持,以验证邮件发送是否有任何限制。

If you think your host has emailing limits or blocks the sending of emails, check their FAQs to see if they list any such limitations. Otherwise, you may need to reach out to their support to verify if there are any restrictions in place around the sending of emails.

由于各种原因,通常通过PHP(和其他服务器端编程语言)发送的电子邮件最终在收件人的垃圾邮件文件夹中。在对代码进行故障排除之前,请务必检查。

Oftentimes, for various reasons, emails sent through PHP (and other server-side programming languages) end up in a recipient's spam folder. Always check there before troubleshooting your code.

为了避免通过PHP发送的邮件被发送到收件人的垃圾邮件文件夹,您可以在PHP中执行各种操作代码和其他方式,以尽量减少电子邮件标记为垃圾邮件的机会。 Michiel de Mare 的好提示包括:

To avoid mail sent through PHP from being sent to a recipient's spam folder, there are various things you can do, both in your PHP code and otherwise, to minimize the chances your emails are marked as spam. Good tips from Michiel de Mare include:



  • 使用电子邮件身份验证方法,例如 SPF DKIM ,以证明您的电子邮件和您的域名属于一起,并防止欺骗您的域名。 SPF网站包含一个向导,以生成您网站的DNS信息。

  • 检查您的反向DNS ,以确保邮件服务器的IP地址指向用于发送邮件的域名。

  • 确保您使用的IP地址是不在黑名单上

  • 确保回覆地址是有效的现有地址。

  • 在收件人字段中使用收件人的完整真实姓名,而不仅仅是电子邮件地址(例如John Smith< john@blacksmiths-international.com> )。

  • 监控您的滥用帐户,例如abuse@yourdomain.com和postmaster@yourdomain.com。这意味着 - 确保这些帐户存在,阅读发送给他们的信息,并对投诉采取行动。

  • 最后,使其真的容易取消订阅。否则,您的用户将通过按垃圾邮件按钮取消订阅,这将影响您的声誉。

  • Use email authentication methods, such as SPF, and DKIM to prove that your emails and your domain name belong together, and to prevent spoofing of your domain name. The SPF website includes a wizard to generate the DNS information for your site.
  • Check your reverse DNS to make sure the IP address of your mail server points to the domain name that you use for sending mail.
  • Make sure that the IP-address that you're using is not on a blacklist
  • Make sure that the reply-to address is a valid, existing address.
  • Use the full, real name of the addressee in the To field, not just the email-address (e.g. "John Smith" <john@blacksmiths-international.com> ).
  • Monitor your abuse accounts, such as abuse@yourdomain.com and postmaster@yourdomain.com. That means - make sure that these accounts exist, read what's sent to them, and act on complaints.
  • Finally, make it really easy to unsubscribe. Otherwise, your users will unsubscribe by pressing the spam button, and that will affect your reputation.

请参阅如何确保以编程方式发送的电子邮件不会自动标记为垃圾邮件?了解更多

某些垃圾邮件软件会拒绝邮件缺少常见的标题,例如From和Reply-to:

Some spam software will reject mail if it is missing common headers such as "From" and "Reply-to":

$headers = array("From: from@example.com",
    "Reply-To: replyto@example.com",
    "X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
mail($to, $subject, $message, $headers);



确保邮件头没有语法错误



无效的标头与没有标题一样糟糕。一个不正确的字符可能是破坏你的电子邮件所需要的。仔细检查以确保您的语法正确,因为PHP将不会为您捕获这些错误。

Make sure mail headers have no syntax errors

Invalid headers are just as bad as having no headers. One incorrect character could be all it takes to derail your email. Double-check to make sure your syntax is correct as PHP will not catch these errors for you.

$headers = array("From from@example.com", // missing colon
    "Reply To: replyto@example.com",      // missing hyphen
    "X-Mailer: "PHP"/" . PHP_VERSION      // bad quotes
);



确保收件人的值正确



有时,问题就像收到邮件的收件人的值不正确一样简单。这可能是由于使用不正确的变量。

Make sure the recipient value is correct

Sometimes the problem is as simple as having an incorrect value for the recipient of the email. This can be due to using an incorrect variable.

$to = 'user@example.com';
// other variables ....
mail($recipient, $subject, $message, $headers); // $recipient should be $to

另一种测试方法是将收件人值进行硬编码 mail()函数调用:

Another way to test this is to hard code the recipient value into the mail() function call:

mail('user@example.com', $subject, $message, $headers); 

这可以适用于所有的 mail()参数。

This can apply to all of the mail() parameters.

为了帮助排除电子邮件帐户问题,请发送电子邮件至不同电子邮件提供商的多个电子邮件帐户。如果您的电子邮件未到达用户的Gmail帐户,请将相同的电子邮件发送到Yahoo帐户,Hotmail帐户和常规POP3帐户(如ISP提供的电子邮件帐户)。

To help rule out email account issues, send your email to multiple email accounts at different email providers. If your emails are not arriving at a user's Gmail account, send the same emails to a Yahoo account, a Hotmail account, and a regular POP3 account (like your ISP-provided email account).

如果电子邮件到达所有或其他电子邮件帐户,您知道您的代码正在发送电子邮件,但是由于某种原因,电子邮件帐户提供商可能会阻止他们。如果电子邮件没有到达任何电子邮件帐户,则问题更可能与您的代码相关。

If the emails arrive at all or some of the other email accounts, you know your code is sending emails but it is likely that the email account provider is blocking them for some reason. If the email does not arrive at any email account, the problem is more likely to be related to your code.

如果您将表单方法设置为 POST ,请确保使用 $ _ POST 来寻找你的表单值。如果您设置为 GET 或根本没有设置,请确保使用 $ _ GET 查看对于您的表单值。

If you have set your form method to POST, make sure you are using $_POST to look for your form values. If you have set it to GET or didn't set it at all, make sure you using $_GET to look for your form values.

某些Web托管服务提供商不允许或启用通过其服务器发送电子邮件。原因可能会有所不同,但是如果他们禁用发送邮件,您将需要使用另一种方法,使用第三方为您发送这些电子邮件。

Some Web hosting providers do not allow or enable the sending of emails through their servers. The reasons for this may vary but if they have disabled the sending of mail you will need to use an alternative method that uses a third party to send those emails for you.

向他们的技术支持发送电子邮件(在访问他们的在线支持或FAQ后)应该澄清您的服务器上是否有电子邮件功能。

An email to their technical support (after a trip to their online support or FAQ) should clarify if email capabilities are available on your server.

如果您使用WAMP,MAMP或XAMPP在本地工作站上开发,则可能是电子邮件服务器没有安装在你的工作站上。没有一个,PHP不能默认发送邮件。

If you are developing on your local workstation using WAMP, MAMP, or XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.

您可以通过安装基本的邮件服务器来克服这个问题。对于Windows,您可以使用免费的 Mercury Mail

You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.

您还可以使用SMTP发送您的电子邮件。请参阅这个伟大的答案,从 Vikas Dwivedi 了解如何做到这一点。

You can also use SMTP to send your emails. See this great answer from Vikas Dwivedi to learn how to do this.

除了您的MTA和PHP的日志文件,您可以启用 mail()功能记录日志。它不记录完整的SMTP交互,但至少是函数调用参数和调用脚本。

In addition to your MTA's and PHP's log file, you can enable logging for the mail() function specifically. It doesn't record the complete SMTP interaction, but at least function call parameters and invocation script.

ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);

请参阅 http://php.net/manual/en/mail.configuration.php 了解详情。 (最好在 php.ini .user.ini 中启用这些选项.htaccess 也许。)

See http://php.net/manual/en/mail.configuration.php for details. (It's best to enable these options in the php.ini or .user.ini or .htaccess perhaps.)

PHP内置 mail()函数很方便,经常完成工作,但它有其缺点。幸运的是,有其他替代方案可以提供更多的功能和灵活性,包括处理上述许多问题。可能会考虑使用的是流行的 PHPMailer SwiftMailer ,甚至是较旧的 PEAR :: Mail

PHP's built in mail() function is handy and often gets the job done but it has its shortcomings. Fortunately there are alternatives that offer more power and flexibility including handling a lot of the issues outlined above. One that would might consider using is the popular PHPMailer or SwiftMailer, or even the older PEAR::Mail.

这篇关于PHP邮件功能不完成电子邮件的发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆