Script to test mail via PHP on server
This is a script you can use when you want to test if the server can send e-mail. First version will send a simple text mail but if you add the headers argument it will send it as html.
The call could look like this:
/mail_test.php?pwd=sIHa8kO52ZiceZItU2448jAxi&to=my.name(at)mydomain.com
/mail_test.php?pwd=sIHa8kO52ZiceZItU2448jAxi&to=my.name(at)mydomain.com&headers=1
<?php
$time = date('Y-m-d H:i:s');
if($_GET['pwd'] == 'sIHa8kO52ZiceZItU2448jAxi') {
if($_GET['to']){
$to = $_GET['to'];
}else{
$to = "my.name(at)mydomain.com";
}
$subject = "Test ".$time;
if($_GET['headers']){
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: '.$to.' <'.$to.'>' . "\r\n";
$headers .= 'From: Test Script <noreply@'.$_SERVER['HTTP_HOST'].'>' . "\r\n";
//$headers .= 'Cc: my.name(at)mydomain.com' . "\r\n";
//$headers .= 'Bcc: my.name(at)mydomain.com' . "\r\n";
$lb = "<br>";
}else{
$headers = NULL;
$lb = "\n";
}
$server = str_replace(array(".", "/"), "-", "host: ".$_SERVER['HTTP_HOST']. $lb."remote: ".$_SERVER['REMOTE_ADDR']. $lb."server: ".$_SERVER['SERVER_ADDR']. $lb."request: ".$_SERVER['REQUEST_URI']. $lb."file:".__FILE__);
$body = "Hi,". $lb.$lb ."This is a test sent at ".$time. $lb.$lb .$server;
if (mail($to, $subject, $body, $headers)) {
echo "<p>Message sent to $to! $time</p>";
} else {
echo "<p>Message failed to $to! $time</p>";
}
}else{
echo "<p>Not allowed! $time</p>";
}
?>I filter the the data in the mail to make it not look like an URL to avoid the mail servers or spamfilters to detect it as a possible spam. The mail will look something like this:
Hi,
This is a test sent at 2016-01-06 15:08:26
host: your-host-com
remote: nnn-nnn-nnn-nnn
server: nnn-nnn-nnn-nnn
request: -mail_test-php?pwd=sIHa8kO52ZiceZItU2448jAxi&to=my-name(at)mydomain-com&headers=1
file:-var-www-your-host-com-mail_test-phpKnowledge keywords:
