Php emailing script

I wrote this a couple of years backup, I put a lot of effort into it back then.
(it really seems kinda stupid now)

#!/usr/bin/php
<?php
/*                                              Disclaimer                                                      */
/*The one who wrote this script is not responsible for the action of others who use this script,
even if that implies, but not restricted to: injury, theft, death of a loved one ;), or anything of that sort.
DONATIONS ARE WELCOMED! not only money but cigarettes, spare change, pizza leftovers, old clothes, just about anything :)*/
/* ======  Please install php-cli to use this script ====== */

echo "                                    Php mail sender  \n";
echo "                                        ver. 0.1     \n";
echo "                                                     \n";

check();

if ($argc == 5){

$to = "$argv[1]";

$subject = "$argv[3]";

$body = "$argv[4]";


$mailheaders = "X-Priority: 1\n";
$mailheaders .= "X-Mailer: Outlook Express\n";
$mailheaders .= "Reply-To: $argv[2]\n";
$mailheaders .= "Mime-Version: 1.0\n";
$mailheaders .= "Content-Transfer-Encoding: 8bit\n";
$mailheaders .= "Content-Type: text/plain; charset=iso-8859-1\n";
$mailheaders .= "Return-Path: <$argv[2]>\n";


if (preg_match("/@/",$to) && preg_match("/\./",$to)){
$sys="Email format seems to be ok";
}else {
echo "Your email address seems to be wrong, mail sender canceled\n";
echo "Exiting....\n";
sleep(1);
exit();
}

if (preg_match("/@/",$argv[2]) && preg_match("/\./",$argv[2])){
$sys="Email format seems to be ok";
} else {
echo "The email address you are sending TO seems to be wrong, mail sender canceled\n";
echo "Exiting....\n";
sleep(1);
exit();
}


echo "                   What is your name?\n";
echo "                   NB: This will appear in the mail header\n";
echo " \n";
echo "# "; $name = fgets(STDIN);
$mailheaders .= "From: $name <$argv[2]>\n";

echo "------------------------------------------------------------------------------------------------------------------------------------------\n";

smail($to,$mailheaders,$body,$subject);
}

elseif ($argc == 2  &&  $argv[1] == "--interactive"){

echo "                  Running interactive\n";
echo "                                          \n";
interactive();
}



else{
        echo "          *  Usage: $argv[0] [mailto] [mailfrom] ['subject'] ['Mail Body'] \n";
        echo "                                *  The '' are mandatory ! \n";
        echo "               *  Example: $argv[0] a@a.ro a@a.ro 'Test' 'Wazzzup Test'\n";
        echo "                     *  You can also use this script interactively\n";
        echo "                          *  Example: $argv[0] --interactive\n";
        echo "           N.B.: If you want to use this script you need php-cli compiled and a MTA\n";
        echo "                        If you can see this you already have php ;)\n";
}
function interactive(){
check();
        echo "                  Enter your email address:\n";
echo " \n";
echo "# "; $header =fgets(STDIN);
echo " \n";


if (preg_match("/@/",$header) && preg_match("/\./",$header)){
$sys="Email format seems to be ok";
} else {
echo "The email address you are sending TO seems to be wrong, mail sender canceled\n";
echo "Exiting....\n";
sleep(1);
exit();


}

        echo "                  Who do you want to send mail to:\n";
echo " \n";
echo "# "; $to = fgets(STDIN);
echo " \n";
if (preg_match("/@/",$to) && preg_match("/\./",$to)){
$sys="Email format seems to be ok";
} else {
echo "Your email address seems to be wrong, mail sender canceled\n";
echo "Exiting....\n";
sleep(1);
exit();
}

        echo "                  Enter the email subject:\n";
echo " \n";
echo "# "; $subject = fgets(STDIN);
echo " \n";

        echo "                  Enter the email text:\n";
        echo "                  Pressing enter will end the text\n";
echo " \n";
echo "# "; $body = fgets(STDIN);
echo " \n";


echo "                   What is your name?\n";
echo "                                   NB: This will appear in the mail header\n";
echo " \n";
echo "# "; $name = fgets(STDIN);
echo " \n";

echo "------------------------------------------------------------------------------------------------------------------------------------------\n";
$mailheaders = "X-Mailer: Outlook Express\n";
$mailheaders .= "From: $header";

smail($to,$mailheaders,$body,$subject);
}

function smail($to,$mailheaders,$body,$subject){
        if (mail("$to", "$subject", "$body", "$mailheaders"))
                {echo   "                                       Message sent!\n";}

        else
                { echo  "                                    Message delivery failed..\n.";}

echo "------------------------------------------------------------------------------------------------------------------------------------------\n";
}


function check(){

$path='/usr/sbin/sendmail';
if (!file_exists($path)){
echo "      It seems you don`t have any MTA installed on your system\n";
echo "              I am going to try to install one for you\n";

echo "  Please select your Linux Distribution, if non apply press any key and install manually\n";
echo "          Enter -1- for RedHat/CentOS, Enter -2- for Debian/Ubuntu, Enter -3- for Mandrake/Mandriva\n";
echo "          Type: 1 , 2 , or 3\n";
echo "              : "; $command = fgets(STDIN);

$message="           Installation seems to have worked\n            For more details please read: /var/log/mail-install.log\n";

switch($command){

        case 1:
        if (!(system("yum install postfix >> /var/log/mail-install.log 2>&1"))) {echo "         Installation might have failed\n";} else {echo
$message; return;}
        break;

        case 2:
        if (!(system("apt-get install postfix >> /var/log/mail-install.log 2>&1"))) {echo "             Installation might have failed\n";} else {
echo $message; return;}
        break;

        case 3:
        if (!(system("urpmi install postfix >> /var/log/mail-install.log 2>&1"))) {echo "               Installation might have failed\n";} else {
echo $message; return;}
        break;


        default :
        echo "          For more details please read: /var/log/mail-install.log\n";
        echo "          Your system is not compatible, please install MTA manually\n";
        echo "                Install : postfix, qmail, sendmail or exim\n";
        echo "                             Exiting........\n";
        sleep(1);
        exit();
        break;
                }


echo "          For more details please read: /var/log/mail-install.log\n";
echo "          Install : postfix, qmail, sendmail or exim\n";
echo "          Application closing...\n";
sleep(1);
exit();

}
else {return;}
}
?>

Hope it helps somebody.
See attachments section for download (use save link as and wget away).

AttachmentSize
mail.6.55 KB
Tags:

Comments