<?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: yoursite.com'; $to = 'contact@yoursite.com'; $subject = 'Customer Inquiry'; $body = "From: $namen E-Mail: $emailn 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 try to create 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
which has the above PHP code embedded in it.
The code submits perfectly, but the email is never sent. How can I solve this problem?
P粉6902008562023-10-12 17:36:57
Although some parts of this answer only apply to use of the mail()
function itself, many of these troubleshooting steps can be applied to any PHP mail system. 嗯>
There are several reasons why your script may not be sending the email. Unless there's an obvious syntax error, it's hard to diagnose these things. If not, you'll want to go through the checklist below to look for any potential pitfalls you might encounter.
Error reporting is crucial for rooting out bugs in your code, as well as general errors encountered with PHP. Error reporting needs to be enabled to receive these errors. Placing the following code at the top of your PHP file (or in the main configuration file) will enable error reporting.
error_reporting(-1); ini_set('display_errors', 'On'); set_error_handler("var_dump");
See How to get useful error messages in PHP? — This answer for more details.
mail()
function This may seem silly, but a common mistake is forgetting to actually place the mail()
function in your code. Make sure it's there and not commented out.
mail()
functionThe mail function takes three required parameters, and optional fourth and fifth parameters. If your call to mail()
does not have at least three arguments, it will fail.
If your call to mail()
does not provide the correct parameters in the correct order, it will also fail.
Your web server should log all attempts to send email through it. The location of these logs will vary (you may need to ask your server administrator where they are), but they can usually be found under logs
in the user's root directory. Inside will be the error message reported by the server (if any) related to your attempt to send the email.
Port blocking is a very common issue that most developers face while integrating code to send emails using SMTP. And, this can be easily traced in the server mail log (the location of the mail log server may vary from server to server, as mentioned above). If you are on a shared hosting server, ports 25 and 587 are still blocked by default. This blocking is done intentionally by your hosting provider. This is true even for some dedicated servers. When these ports are blocked, try connecting using port 2525. If you find that the port is also blocked, then the only solution is to contact your hosting provider to unblock these ports.
Most hosting providers block these email ports to protect their network from any spam being sent.
Use port 25 or 587 for normal/TLS connections and port 465 for SSL connections. For most users, it is recommended to use port 587 to avoid rate limits set by some hosting providers.
When the Error Suppression Operator @
is added before an expression in PHP, any error messages that may be generated by that expression will be ignored. There are situations where this operator is required, but sending mail is not one of .
If your code contains @mail(...)
, you may be hiding important error messages that will help you debug. Remove @
and see if any errors are reported.
Only if you use error_get_last()
immediately following a specific failure.
mail()
Return valuemail()
Function:
This is worth noting because:
FALSE
return value, you know the error is with your server accepting your mail. This may not be a coding issue but a server configuration issue. You need to contact your system administrator to find out why this is happening. TRUE
return value, it does not mean that your email will be sent. This simply means that the email was successfully sent via PHP to the appropriate handler on the server. There are many more failure points beyond PHP's control that can prevent emails from being sent. So FALSE
will help point you in the right direction, while TRUE
doesn't necessarily mean your email was sent successfully. It’s worth noting!
Many shared web hosts, especially free web hosting providers, either do not allow email to be sent from their servers or limit the number that can be sent in any given time period. This is because they work hard to limit spammers from taking advantage of their cheaper services.
If you think your host has email restrictions or is blocking email from being sent, check their FAQ to see if they list any such restrictions. Otherwise, you may need to contact their support to verify if there are any restrictions on sending emails.
Often, emails sent via PHP (and other server-side programming languages) end up in the recipient's spam folder for a variety of reasons. Be sure to check there before troubleshooting your code.
To avoid emails sent via PHP being sent to the recipient's spam folder, there are a number of things you can do in your PHP code or otherwise to minimize the chance of your email being marked as spam. . Michiel de Mare Good tips include:
For more information on this topic, see How to ensure that emails sent programmatically are not automatically marked as spam? .
Some spam software will reject messages if they are missing common headers such as "From" and "Reply":
$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);
Invalid headers are just as bad as no headers at all. One incorrect character can derail your email. Please 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 );
Sender:
Sender Although the message must have From:Sender, you cannot just use any value. In particular, user-supplied sender addresses are a reliable way to block messages:
$headers = array("From: $_POST[contactform_sender_email]"); // No!
Cause: Your network or outgoing mail server is not SPF/DKIM whitelisted and cannot pretend to be responsible for the @hotmail or @gmail address. It may even silently drop messages with an unconfigured From:
sender domain.
Sometimes the problem is as simple as an incorrect value for the email recipient. This may be due to incorrect variables being used.
$to = 'user@example.com'; // other variables .... mail($recipient, $subject, $message, $headers); // $recipient should be $to
Another way to test this issue is to hardcode the recipient value into the mail()
function call:
mail('user@example.com', $subject, $message, $headers);
This can be applied to all mail()
parameters.
To help troubleshoot email account issues, please send your email to multiple email accounts with different email providers. . If your email doesn't reach the user's Gmail account, send the same email to a Yahoo account, a Hotmail account, and a regular POP3 account (such as the email account provided by your ISP).
If the emails arrive at all or some of the other email accounts, you know your code is sending emails, but the email account provider may be blocking them for some reason. If the email doesn't arrive at any email account, the problem is most likely related to your code.
If you have set the form method to POST
, be sure to use $_POST
to find the form value. If you have it set to GET
or not set at all, make sure to use $_GET
to find the form value.
action
value points to the correct location Make sure your form action
attribute contains a value that points to your PHP mail code.