This code will let you check that the PHP sendmail() function is working on either an Apache or cPanel server.
Here’s the code involved:
<?php
//*********************************************************************************
//***** EMAIL TEST SCRIPT TO CHECK THE FUNCTIONALITY OF PHP'S MAIL() FUNCTION *****
//*********************************************************************************
/**
* @name MailTest
* @package testscripts.localdev
* @version 0.3.3
* @since 25-Jun-2021
* @author Chris Appleton
* @abstract Test PHP's mail() function is working
*/
// PHP Mail() FUNCTION TESTING
// THIS SCRIPT TESTS THAT PHP'S SENDMAIL() FUNCTION IS WORKING ON THE SERVER
// CHANGE THE RECIPIENT ADDRESS TO YOUR OWN (MOSTLY SO YOU DON'T SPAM ME!) BEFORE USING - YOU SHOULD NOT NEED TO CHANGE ANYTHING ELSE.
//THE SCRIPT WILL IDENTIFY THE SERVER, TIME/DATE AND PROVIDE CORRECT HEADER INFORMATION FOR ANALYSIS IF NEEDED.
//***** RECIPIENT DETAILS *********************************************************
$to = "email@example.com";
$cc = "";
$bcc = "";
//***** NO NEED TO EDIT BELOW THIS LINE *******************************************
//***** VARS FOR DEBUGGING ********************************************************
$host = getenv('HTTP_HOST');
$domain = getenv('HTTP_HOST');
$version = phpversion();
$date = date("Y-m-d H:i:s");
//***** BUILD SUBJECT AND MESSAGE FOR TEST MAIL ***********************************
$subject = "Testing messages sent from scripts";
$message .= "This is a test message to check that PHP's mail() function is working correctly.\n";
$message .= "If you have received this, then your server is set up to send mail and is working as intended\n\n";
$message .= "Please do not reply to this email\n\n";
$message .= "Domain: $domain\n";
$message .= "PHP version in use: $version\n\n";
$message .= "This message was sent at: $date\n\n";
//***** ADDITIONAL HEADERS FOR COMPLETENESS ***************************************
$headers = "From: $domain\r\n";
if($cc != "") $headers .= "Cc: $cc\r\n";
if($bcc != "") $headers .= "Bcc: $bcc\r\n";
$headers .= "Reply-To: noreply@$domain\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
//***** SEND THE MAIL AND REPORT ANY ERRORS ***************************************
if(mail($to, $subject, $message, $headers)) echo "Mail sent successfully from $host";
else echo "Error sending message from $host";
?>
Copy this code, – making sure that you change the $to variable to an email address of your own, save it inside a file named mailtest.php, for example and then upload this to the default web folder of your webserver.
Visiting domain.com/mailtest.php will trigger the script.