Many times the sendmail executable or equivalent is unavailable on a given server. This code tries to use the standard PHP mail functions and failing that opens a socket for SMTP transfer. The socket interface may be accessed directly, bypassing the PHP mail() and imap_mail() functions. Highly configurable interface with several optional arguments.
<?php
/* Dave Silvia (c) 2006, dsilvia@mchsi.com.
Any usage permitted.
Credit where credit is due is appreciated!;)
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?");
$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");
if(!$ret)
{
print("Oh, pshaw! It didn't work!<br>");
}
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){