PHP Classes
Icontem

File: fmail.class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Sam Shull  >  fmail  >  fmail.class.php  
File: fmail.class.php
Role: Class source
Content type: text/plain
Description: Class file
Class: fmail
Send email messages with content read from files
 

Contents

Class file image Download
<?php
/**
 *    This class has one method used for sending a MIME email the same way that
 *    PHP's standard mail function does, except the 3rd argument must be a stream
 *    thereby reducing the memory overhead need to send large attachments
 *
 *    These functions are best when used with large files that cause 
 *    memory capacity to be exceeded
 *
 *    @author Sam Shull <sam.shull@jhspecialty.com>
 *    @version 1.0
 *
 *    @copyright Copyright (c) 2009 Sam Shull <sam.shull@jhspeicalty.com>
 *    @license <http://www.opensource.org/licenses/mit-license.html>
 *
 *    Permission is hereby granted, free of charge, to any person obtaining a copy
 *    of this software and associated documentation files (the "Software"), to deal
 *    in the Software without restriction, including without limitation the rights
 *    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *    copies of the Software, and to permit persons to whom the Software is
 *    furnished to do so, subject to the following conditions:
 *    
 *    The above copyright notice and this permission notice shall be included in
 *    all copies or substantial portions of the Software.
 *    
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *    THE SOFTWARE.
 *
 */

class fmail
{
    
/**
     *    A function that does what the standard mail function does, 
     *    except this function takes a stream as the third argument
     *
     *    @param string $to
     *    @param string $subject
     *    @param resource $stream
     *    @param string $headers = null
     *    @param string $parameters = null
     *
     *    @return boolean
     *
     *    <code>
     *        print (int)mail("sam.shull@jhspecialty.com", "Mail Test", "This is the mail function", "FROM: Tester ");
     *
     *        error_reporting(E_ALL);
     *        
     *        $message = tmpfile();
     *        
     *        fwrite($message, "This is the fmail function");
     *        
     *        print (int)fmail::send("sam.shull@jhspecialty.com", "FMail Test", $message, "FROM: Tester ");
     *        
     *        fclose($message);
     *    </code>
     *
     */
    
static function send ($to$subject$stream$headers=null$parameters=null)
    {
        
//otherwise if the $stream is not a resource fail
        
if (!is_resource($stream))
        {
            
trigger_error(sprintf("fmail expects parameter 3 to be a resource, '%s' given"gettype($stream)), E_USER_WARNING);
            return 
false;
        }
        
        
//$to = preg_replace('/\r?\n(\S)/' "\r\n \\1", $to);
        //$subject = preg_replace('/\r?\n(\S)/', "\r\n \\1", $subject);
        
        //the path to the application that will be handling the message
        
$mail ini_get("sendmail_path");
        
        
//print $mail;
        
        
$mail_command $parameters sprintf("%s %s"$mailescapeshellcmd($parameters)) : $mail;
        
        
$WIN strtoupper(substr(PHP_OS03)) === 'WIN';
        
        
//open the program - use binary for windows
        
if ($sendmail popen($mail_command$WIN "wb" "w"))
        {
            
fwrite($sendmailsprintf("To: %s\n"$to));
            
fwrite($sendmailsprintf("Subject: %s\n"$subject));
            
            if (
$headers != null)
            {
                
fwrite($sendmailsprintf("%s\n"$headers));
            }
            
//write the message
            //begin with a new line
            
fwrite($sendmail"\n");
            
            
//rewind to beginning of stream
            
rewind($stream);
            
            
//write the stream to the program
            //stream_copy_to_stream($stream, $sendmail);
            
while (!feof($stream))
            {
                
fwrite($sendmailfgets($stream));
            }
            
            
//end with a new line
            
fwrite($sendmail"\n");
            
            return 
pclose($sendmail) == ($WIN ? -0);
        }
        else
        {
            
trigger_error(sprintf("Could not execute mail delivery program '%s'"$mail_command), E_USER_WARNING);
        }
        
        return 
false;
    }
}



?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products