How to Test the PHP mail() Function Using a Simple Script
On this page
If you want to check whether the PHP mail() function works correctly, you can upload a simple test file to your server using FTP (for example FileZilla).
Step 1: Create the test file
Upload a file to your /public_html directory.
Example filename:
phpmail.php
Step 2: Add the test script
Paste the following code into the file and replace the sender and recipient email addresses with your own values: <?php $sender = ‘sender@example.com’; $recipient = ‘recipient@example.com’; $subject = “Test message”; $message = “PHP mail function test”; $headers = ‘From:’ . $sender; if (mail($recipient, $subject, $message, $headers)) { echo “Message sent successfully”; } else { echo “Error: Message could not be sent”; } ?>
Step 3: Run the test
Open the file in your browser:
http://yourdomain.com/phpmail.php
Expected result
If everything is working correctly:
- Message sent successfully
If there is an issue:
- Error: Message could not be sent
Notes
- Ensure the PHP
mail()function is enabled on the server - Some hosting providers require valid SMTP configuration even if
mail()is available - Always include
<?phpat the beginning and?>at the end of standalone PHP files - Make sure sender and recipient addresses are valid to avoid delivery issues
Didn't find an answer?
Our support team is here for you 24/7 – write to us and we will help you sort it out.