PHP Mail() function is not working on Hostgator | Hostinger | Online

PHP mail function is not working

Loading

I myself have faced this problem many times, when email or contact form is not working on my clients’ website, today in this blog I am going to tell you how you can configure the contact form on your php website. There are many reasons why PHP Mail function is not working, some of which include:

  1. Check File Permissions: Ensure that the file containing your PHP code has the correct permissions set. It should be readable and executable by the server.
  2. Server Configuration: Make sure that your server is configured to support PHP. Sometimes, servers may not be configured properly to handle PHP scripts.
  3. Error Reporting: Enable error reporting in your PHP script by adding error_reporting(E_ALL); and ini_set('display_errors', 1); at the top of your PHP file. This will help you identify any errors that may be occurring.
  4. Check PHP Syntax: Verify that your PHP code does not contain any syntax errors. Even small mistakes can cause the script to fail.
  5. Check Form Action: Ensure that the form action attribute points to the correct PHP file that processes the form data.
  6. Check Form Method: Verify that the form method (GET or POST) matches the method expected by your PHP script.
  7. Test PHP Script: Try accessing the PHP script directly in your browser to see if it executes without any errors. This can help isolate whether the issue is with the form or the PHP script itself.
  8. Server Logs: Check your server logs for any error messages or warnings related to the PHP script. This can provide valuable information about what might be going wrong.

You can check why your mail function is not working by using any of the listed methods.

You can test with a single PHP Mail function that it is working on your website or not

Test PHP Mail() on your server, Create a file on your server with name test.php

    <?php
    $to_address = "test@somedomain.com";
    $subject = "This goes in the subject line of the email!";
    $message = "This is the body of the email.\n\n";
    $message .= "More body: probably a variable.\n";
    $headers = "From: test@somedomain.com\r\n";
    
    if (mail($to_address,$subject,$message,$headers))
    {
     echo "Message accepted";
    }
    else
    {
    echo "Error: Message not accepted";
    }
    ?>

    Change the $sender and $recipient with your email id in the code. so that you can receive responses

    Upload the php file to your hosting provider or server

    Open the uploaded php file in your browser to execute the php script.

    The output show either “Message accepted” or “Error: Message not accepted”.

    Open your email to check, whether you received email or not? using PHP Mail Function

    If Message Delivered

    • Everything Working Fine

    If Not Delivered ? Then you can check following things

    Some providers restrict external recipients for PHP mail. Update the recipient ($recipient) in your code to a local recipient, using an email address from your server’s domain. For instance, if your server domain is www.yourdomain.tld, the recipient’s email should be someone@yourdomain.tld. Upload the modified PHP file and try again.

    If the issue persists, adjust the sender ($sender) to a local email (using the same email as the recipient). Upload the modified PHP file and retry.

    About Post Author