Programming :  Student Freelance Forum For Work Experience Builders' (CertificationPoint) The fastest message board... ever.
 
PHP-tinySendMail
Posted by: adcertpoint (Moderator)
Date: April 29, 2020 04:28PM

By Dave Silvia

on January 15, 2006

-
Version: 1.2

Type: Function

Category: Networking

License: Other

Description: Often the mail functions of php cannot be used because they need access to sendmail or an equivalent binary and/or specific settings in php.ini on a given server and that access and/or configuration is not present. This code provides for sending mail in most all cases without any special requirements aside from being able to open a socket.





?


<?php
/* Dave Silvia (c) 2006, dsilvia@mchsi.com. Any usage permitted. Credit
where credit is due is appreciated!winking smiley
*/
/*
Synopsis:

int tinySendMail(from,to,subject,message)

Return of non-zero indicates success.

Examples:

tinySendMail("me@myisp.com","you@yourisp.com","Sent with tinySendMail",
"This function does pretty well for a little fella! What do you think?"winking smiley;

$ret=tinySendMail("me@myisp.com","you@yourisp.com","Sent with tinySendMail",
"This function does pretty well for a little fella! What do you think?",
"yourmailer.yourisp.com",24,"mywork.com"winking smiley;
if(!$ret)
{
print("Oh, pshaw! It didn't work!<br>"winking smiley;
}

Description:
EXTREMELY simple and tiny send mail. Only 7 states and addresses only 3
SMTP return codes:
220 Service Ready
250 Requested mail action okay, completed
354 Start mail input; end with <CRLF>.<CRLF>
Any other response is assumed to be an error, a QUIT is sent, and
tinySendMail returns with a zero return. Non-zero return indicates success.
Arguments:
$sender:
traditional 'From:', e.g., you@yourdomain.com
$receiver:
traditional 'To:', e.g., them@theirdomain.com
$subject:
traditional 'Subject:'
$message:
body text. Folds at 990 characters. RFC2821 suggests a limit of 1000
including CRLF. This is compliant, but you can change the variable
$textLineLimit to suit your purposes or any specific server.
$rcvsmtp:
smtp server of the receiver, e.g., mail.theirdomain.com.
optional, set to the domain in the receiver's email address with
'mail.' prepended by default. If you don't know the receiver's
smtp server and/or the default doesn't work, you may specify a
server you know will accept the email for transfer, i.e., your own smtp
server will probably accommodate.
$rcvport:
port to connect to on $rcvsmtp. default is 25 (SMTP), you may
optionally set it to another if the receiver uses a special port.
$sndhost:
domain of the sending host, e.g., yourdomain.com. Not necessarily
the same as the one in $sender, as you may be sending from a different
host than the one your email is on.
optional, set to the local host as determined by php_uname('n').
$usePHPmailFunc:
use bool mail() php function that calls sendmail or equivalent binary
optional. default off;
$usePHPimapFunc:
use bool imap_mail() php function that calls sendmail or equivalent.
optional. default off;
$tryAll:
try all 3 methods to send the mail.
optional. default on;
$verbose:
optional argument to turn on informational messages. Off by default.
$vboseloc:
optional argument to turn on informational message location. Off by default.
*/
function tinySendMail($sender,$receiver,$subject,$message
,$rcvsmtp=0,$rcvport=25,$sndhost=0,$usePHPmailFunc=0,$usePHPimapFunc=0,$trySock=0,$tryAll=1,$verbose=0,$vboseloc=0)
{
infoPrint($verbose,__FILE__."::".__FUNCTION__.": Starting",__LINE__,$vboseloc);
if(!$sndhost)$sndhost=php_uname('n');
if(!$rcvsmtp)$rcvsmtp="mail.".substr(strstr($receiver,"@"winking smiley,1);
if($tryAll)$usePHPmailFunc=$usePHPimapFunc=$trySock=1;
if($usePHPmailFunc || $usePHPimapFunc)
{
ini_set("SMTP",$rcvsmtp);
$addtlHdr="From: $sender";
}
$ret=0;
if($usePHPmailFunc)
{
infoPrint($verbose,"trying mail()",__LINE__,$vboseloc);
$ret=mail($receiver,$subject,$message,$addtlHdr);
}
if(!$ret && $usePHPmailFunc)
{
infoPrint($verbose,"trying imap_mail()",__LINE__,$vboseloc);
$ret=imap_mail($receiver,$subject,$message,$addtlHdr);
}
if($ret || !$trySock)
{
infoPrint($verbose,"ret: $ret<br>tryAll: $tryAll",__LINE__,$vboseloc);
infoPrint($verbose,__FILE__."::".__FUNCTION__.": Finishing: returning: $ret",__LINE__,$vboseloc);
return($ret);
}
infoPrint($verbose,"trying fsockopen()",__LINE__,$vboseloc);
/* RFC2821 says 1000 including CRLF, so this should be compliant */
$textLineLimit=990;
$mailState_open=0;
$mailState_ehlo=1;
$mailState_rcpt=2;
$mailState_data=3;
$mailState_send=4;
$mailState_quit=5;
$mailState_return=6;
$curMailState=$mailState_open;
infoPrint($verbose,"fsockopen($rcvsmtp,$rcvport,$errno,$errstr)",__LINE__,$vboseloc);
if(!($fp=fsockopen($rcvsmtp,$rcvport,$errno,$errstr)))
{
infoPrint($verbose,"Could not open a socket to $rcvsmtp on port $rcvport",__LINE__,$vboseloc);
infoPrint($verbose,"Error#: $errno: $errstr",__LINE__,$vboseloc);
infoPrint($verbose,__FILE__."::".__FUNCTION__.": Finishing: returning: 0",__LINE__,$vboseloc);
return(0);
}
infoPrint($verbose,"Opened connection to $rcvsmtp:$rcvport",__LINE__,$vboseloc);
$returncode=1;
while($curMailState != $mailState_return)
{
$ret=fgets($fp);
$cod=substr($ret,0,3);
if($curMailState == $mailState_ehlo)
{ /* drain the pipe of all EHLO informational lines */
if($cod == 250)
{
$ret=fgets($fp);
$contChar=substr($ret,3,1);
infoPrint($verbose,"ret: $ret",__LINE__,$vboseloc);
while($contChar == "-"winking smiley
{
infoPrint($verbose,"ret: $ret",__LINE__,$vboseloc);
$ret=fgets($fp);
$contChar=substr($ret,3,1);
}
}
}
infoPrint($verbose,"ret: $ret",__LINE__,$vboseloc);
infoPrint($verbose,"cod: $cod",__LINE__,$vboseloc);
switch($cod)
{
case 220:
switch($curMailState)
{
case $mailState_open:
$snd="EHLO $sndhostrn";
$curMailState=$mailState_ehlo;
break;
default:
$snd="QUITrn";
$returncode=0;
$curMailState=$mailState_return;
}
break;
case 250:
switch($curMailState)
{
case $mailState_ehlo:
$snd="MAIL FROM:<$sender>rn";
$curMailState=$mailState_rcpt;
break;
case $mailState_rcpt:
$snd="RCPT TO:<$receiver>rn";
$curMailState=$mailState_data;
break;
case $mailState_data:
$snd="DATArn";
$curMailState=$mailState_send;
break;
case $mailState_quit:
$snd="QUITrn";
$curMailState=$mailState_return;
break;
default:
$snd="QUITrn";
$returncode=0;
$curMailState=$mailState_return;
}
break;
case 354:
switch($curMailState)
{
case $mailState_send;
$snd="From: $sendern";
$snd.="To: $receivern";
$snd.="Subject: $subjectn";
$snd.="Date: ".date('r')."n";
$snd.="Message-ID: <".
date('YmdHis').":".
php_uname('n').":".
getmypid().
":$sender>nn";
$snd.=wordwrap($message,$textLineLimit,"n",1);
$sndArray=explode("n",$snd);
$numLines=count($sndArray);
for($i=0; $i < $numLines; $i++)
{
fputs($fp,$sndArray[$i]."rn"winking smiley;
}
$snd="rn.rn";
$curMailState=$mailState_quit;
break;
default:
$snd="QUITrn";
$returncode=0;
$curMailState=$mailState_return;
}
break;
default:
$snd="QUITrn";
$returncode=0;
$curMailState=$mailState_return;
}
infoPrint($verbose,"snd: $snd",__LINE__,$vboseloc);
fputs($fp,$snd);
}
$ret=fgets($fp);
infoPrint($verbose,"ret: $ret",__LINE__,$vboseloc);
fclose($fp);
infoPrint($verbose,__FILE__."::".__FUNCTION__.": Finishing: returning: $returncode",__LINE__,$vboseloc);
return($returncode);
}


/* Print informational messages if the $verbose argument is non-zero
*/
function infoPrint($verbose,$msg,$loc,$showloc)
{
if($verbose)
{
if($showloc)$locStr=$loc.": "; else $locStr="";
print($locStr.htmlentities(addslashes($msg))."rn<br>"winking smiley;
}
}
?>

Options: ReplyQuote


Sorry, only registered users may post in this forum.
This forum powered by Phorum.