search

Home  >  Q&A  >  body text

PHP mail function did not complete sending email

<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>
P粉563831052P粉563831052454 days ago488

reply all(1)I'll reply

  • P粉253800312

    P粉2538003122023-08-30 00:37:52

    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.

    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.

    Make sure to call the 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.

    Make sure to call mail()function

    correctly

    The 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.

    Checkmail()Return value

    mail() Function:

    This is worth noting because:

    • If you receive a 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.
    • If you receive a 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!

    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":

    $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);

    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.

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

    Do not use fake 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.

    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.

    $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.

    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 to GET or not set it at all, make sure to use $_GET to find the form value.

    Make sure your formactionvalue points to the correct location

    Make sure your form action attribute contains a value that points to your PHP mail code.

    Make sure your web host supports sending email

    Some web hosting providers do not allow or enable email sending through their servers. The reasons for this may vary, but if they block the sending of emails, you will need to use a third-party alternative that sends these emails to you.

    An email sent to their technical support (after visiting their online support or FAQ) should clarify whether the email feature is available on your server.

    Make sure localhost Mail Server

    is configured

    If you develop on a local workstation using WAMP, MAMP, or XAMPP, you may not have an email server installed on your workstation. If not, PHP won't be able to send emails by default.

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

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

    Enable PHP customization

    mail.log

    In addition to the MTA and PHP log files, you can also enable

    specific logging of the mail() function . It does not log the complete SMTP interaction, but at least it logs the function call parameters and the calling script.

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

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

    Check Mail Test Service

    You can use various delivery and spam checking services to test your MTA/webserver setup. Typically you send mail probes to: their address and then get delivery reports and more specific failures or analysis later:

    Use a different email program

    PHP's built-in mail() function is convenient and usually gets the job done, but it has its drawbacks. Fortunately, there are alternatives that offer more power and flexibility, including handling many of the issues mentioned above:

    All of these can be used in conjunction with a professional SMTP server/service provider. (Because typical 08/15 shared web hosting plans are hit or miss with email setup/configurability.)

    reply
    0
  • Cancelreply