PHP Classes

File: fmail.class.php

Recommend this page to a friend!
  Classes of Sam S   fmail   fmail.class.php   Download  
File: fmail.class.php
Role: Class source
Content type: text/plain
Description: Class file
Class: fmail
Send email messages with content read from files
Author: By
Last change: small change to documentation
Date: 15 years ago
Size: 4,068 bytes
 

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", $mail, escapeshellcmd($parameters)) : $mail;
       
       
$WIN = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
       
       
//open the program - use binary for windows
       
if ($sendmail = popen($mail_command, $WIN ? "wb" : "w"))
        {
           
fwrite($sendmail, sprintf("To: %s\n", $to));
           
fwrite($sendmail, sprintf("Subject: %s\n", $subject));
           
            if (
$headers != null)
            {
               
fwrite($sendmail, sprintf("%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($sendmail, fgets($stream));
            }
           
           
//end with a new line
           
fwrite($sendmail, "\n");
           
            return
pclose($sendmail) == ($WIN ? -1 : 0);
        }
        else
        {
           
trigger_error(sprintf("Could not execute mail delivery program '%s'", $mail_command), E_USER_WARNING);
        }
       
        return
false;
    }
}



?>