WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : tinySendMail and tinySockMail functions for generating SMTP mail within PHP
Categories : PHP, Mail, SMTP Click here to Update Your Picture
Dave Silvia
Date : Jan 19th 2006
Grade : 4 of 5 (graded 2 times)
Viewed : 5584
File : 4295.php
Images : No Images for this code example.
Search : More code by Dave Silvia
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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){

    if(!
$sndhost)$sndhost=php_uname('n');
    if(!
$rcvsmtp)$rcvsmtp="mail.".substr(strstr($receiver,"@"),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 tinySockMail()",__LINE__,$vboseloc);
    return(
tinySockMail($sender,$receiver,$subject,$message
       
,$rcvsmtp,$rcvport,$sndhost,$verbose,$vboseloc));
}

function
tinySockMail($sender,$receiver,$subject,$message
   
,$rcvsmtp=0,$rcvport=25,$sndhost=0,$verbose=0,$vboseloc=0)
{
   
infoPrint($verbose,__FILE__."::".__FUNCTION__.": Starting",__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 == "-")
                {
                   
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 $sndhost\r\n";
                       
$curMailState=$mailState_ehlo;
                        break;
                    default:
                       
$snd="QUIT\r\n";
                       
$returncode=0;
                       
$curMailState=$mailState_return;
                }
                break;
            case
250:
                switch(
$curMailState)
                {
                    case
$mailState_ehlo:
                       
$snd="MAIL FROM:<$sender>\r\n";
                       
$curMailState=$mailState_rcpt;
                        break;
                    case
$mailState_rcpt:
                       
$snd="RCPT TO:<$receiver>\r\n";
                       
$curMailState=$mailState_data;
                        break;
                    case
$mailState_data:
                       
$snd="DATA\r\n";
                       
$curMailState=$mailState_send;
                        break;
                    case
$mailState_quit:
                       
$snd="QUIT\r\n";
                       
$curMailState=$mailState_return;
                        break;
                    default:
                       
$snd="QUIT\r\n";
                       
$returncode=0;
                       
$curMailState=$mailState_return;
                }
                break;
            case
354:
                switch(
$curMailState)
                {
                    case
$mailState_send;
                       
$snd="From: $sender\n";
                       
$snd.="To: $receiver\n";
                       
$snd.="Subject: $subject\n";
                       
$snd.="Date: ".date('r')."\n";
                       
$snd.="Message-ID: <".
                           
date('YmdHis').":".
                           
php_uname('n').":".
                           
getmypid().
                           
":$sender>\n\n";
                       
$snd.=wordwrap($message,$textLineLimit,"\n",1);
                       
$sndArray=explode("\n",$snd);
                       
$numLines=count($sndArray);
                       
infoPrint($verbose,"Sending header and message lines",__LINE__,$vboseloc);
                        for(
$i=0; $i < $numLines; $i++)
                        {
                            if(
$verbose)print((($i+1)%10)." ");
                           
fputs($fp,$sndArray[$i]."\r\n");
                        }
                        if(
$verbose)print("\r\n<br>");
                       
infoPrint($verbose,"Done",__LINE__,$vboseloc);
                       
$snd="\r\n.\r\n";
                       
$curMailState=$mailState_quit;
                        break;
                    default:
                       
$snd="QUIT\r\n";
                       
$returncode=0;
                       
$curMailState=$mailState_return;
                }
                break;
            default:
               
$snd="QUIT\r\n";
               
$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))."\r\n<br>");
    }
}
?>



XPertMailer - Sends TRUE Mails
Categories : PHP, Mail, SMTP, PHP Classes
Using this script anyone can easily get a form result to his/her mailbox. You can use this script for any form 2 mail purpose.
Categories : PHP, Mail, Form Processing
Email Class
Categories : PHP, Mail, PHP Classes
Protect your email links from being spidered by spam email robots!
Categories : PHP, Security, Mail, Email
Tell a friend script :)
Categories : PHP, Mail
Data Retrieve from mailbox and generate the SQL Syntax
Categories : PHP, IMAP, Mail
mail -- send mail
Categories : PHP, PHP Functions, Mail
Simple Maiing list with newsletter support
Categories : PHP, PHP Classes, Mail
Convert text to 'quoted printable' without the IMAP package installed.
Categories : PHP, Mail, IMAP
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Sending mail to a mailing list and showing progress
Categories : PHP, Mail, Beginner Guides
send php mail with form data and attachment.
Categories : PHP, Email, Mail, Form Processing
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
Retrieve text from table and email to your e- address in pipe delimited format.
Categories : PHP, MySQL
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs