PHP mail function failed to complete sending email
P粉257342166
2023-08-27 14:03:20
<p><br /></p>
<pre class="brush:php;toolbar:false;"><?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>';
}
}
?></pre>
<p>I'm trying to create a simple email form. The form itself is on my <code>index.html</code> page, but it submits to a separate "Thank you for your submission" page, <code>thankyou.php</code>, which has the above PHP embedded in it code.
The code submits perfectly, but the email is never sent. How can I solve this problem? </p>
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.
Make sure error reporting is enabled and set to report all errors
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.
See How to get useful error messages in PHP? — This answer for more details.
Make sure to call the
mail()
functionThis 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.Make sure to call
correctlymail()
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.Check the server’s mail log
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.Check whether the port connection fails
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.
Do not use error suppression operators
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.Check
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, whileTRUE
doesn't necessarily mean your email was sent successfully. It’s worth noting!Make sure your hosting provider allows you to send email and does not restrict mail sending
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.
Check spam folder; prevent emails from being marked as spam
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? .
Make sure all email headers are provided
Some spam software will reject messages if they are missing common headers such as "From" and "Reply":
Make sure there are no syntax errors in the email header
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.
Do not use fake
Sender:
SenderAlthough 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:
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.Make sure the recipient value is correct
Sometimes the problem is as simple as an incorrect value for the email recipient. This may be due to incorrect variables being used.
Another way to test this issue is to hardcode the recipient value into the
mail()
function call:This can be applied to all
mail()
parameters.Send to multiple accounts
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.
Make sure the code matches the form method
If you have set the form method to
POST
, be sure to use$_POST
to find the form value. If you have set it toGET
or not set it at all, make sure to use$_GET
to find the form value.Make sure your form
action
value points to the correct locationMake sure your form
action
attribute contains a value that points to your PHP mail code.