Today I wrote a command line PHP mail script. You are able to do this manually with programs such as Netcat or Telnet, but this just makes it easier and quicker. Once you run the script from the command line it will ask you for the host, recipient, email to mail from, subject, and message. This can be used quickly to test out a mail server.
<?php
if ($argc > 1) {
echo "This script does not take any arguments.n";
exit;
}
function read ($length='255') {
if (!isset ($GLOBALS['StdinPointer'])) {
$GLOBALS['StdinPointer'] = fopen ("php://stdin","r");
}
$line = fgets ($GLOBALS['StdinPointer'],$length);
return trim ($line);
}
echo "n";
echo "Enter Mail Server IP Address: ";
$host = read(30) . "n";
echo "RCPT: ";
$rcpt = read(40) . "n";
echo "Mail From: ";
$from = read(40) . "n";
echo "Subject: ";
$subject = read(25) . "n";
echo "Message: ";
$message = read(500) . "n";
if ( (strlen($host) < 5) || (strlen($rcpt) < 10) ||
(strlen($from) < 10) || (strlen($subject) < 2) ||
(strlen($message) < 5) ) {
echo "You need to enter all fields properly.n";
exit;
}
$fp = fsockopen($host,25,$errstr,$errno,100);
if (!$fp) {
echo "$errstr ($errno)";
} else {
$out = "helo h4x0rrn";
$out .= "mail from:$fromrn";
$out .= "rcpt to:$rcptrn";
$out .= "datarn";
$out .= "Subject:$subjectrnrn";
$out .= "$messagern.rn";
fwrite($fp, $out);
fclose($fp);
}
?>