LoginLogin About

Support » Knowledge Base » General questions » Tips for scripts owners »

How to test the mail() function

In order to test PHP function mail() on your server, create a test PHP file (e.g., testmail.php) with the following contents:

<?php
$message = '';

if (isset($_POST['email']) && !empty($_POST['email'])){
  if (mail($_POST['email'], $_POST['subject'], $_POST['body'], '')){
    $message = "Email has been sent to <b>".$_POST['email']."</b>.<br>";
  }else{
    $message = "Failed sending message to <b>".$_POST['email']."</b>.<br>";
  }
}else{
  if (isset($_POST['submit'])){
    $message = "No email address specified!<br>";
  }
}

if (!empty($message)){
  $message .= "<br><br>n";
}
?>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>
      Mail test
    </title>
  </head>
  <body>
    <?php echo $message; ?>
    <form method="post" action="">
      <table>
        <tr>
          <td>
            e-mail
          </td>
          <td>
            <input name="email" value="<?php if (isset($_POST['email'])
&& !empty($_POST['email'])) echo $_POST['email']; ?>">
          </td>
        </tr>
        <tr>
          <td>
            subject
          </td>
          <td>
            <input name="subject">
          </td>
        </tr>
        <tr>
          <td>
            message
          </td>
          <td>
            <textarea name="body"></textarea>
          </td>
        </tr>
        <tr>
          <td>
            &nbsp;
          </td>
          <td>
            <input type="submit" value="send" name="submit">
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>

Upload the created file to a publicly accessible directory (e.g. public_html, www, htdocs or another depending on the server configuration).

Run the test file by typing its URL in the browser address bar, e.g. http://mydomain.com/testmail.php.

Enter an email address, a message subject and body in the text fields of the page that will appear and click on "send" to send a test email message.
In case of a successful dispatch of the email a message of kind "Email has been sent to …" will appear in the browser. Otherwise "Failed sending message to..." will appear.

If you fail to send a test email message using the suggested test script, contact your system administrator to have the mail() function correctly configured on your server.

Tip: See also article on troubleshooting email notifications in WebAsyst applications.