<?php
 
/**
 
 * This is a derivative of a Smarty (http://smarty.php.net/) template engine
 
 * It is used to send emails using "MIME E-mail message composing and sending" package by Manuel Lemos
 
 * @copyright Grigori Kochanov
 
 * @license: GNU Lesser General Public License
 
 *
 
 **
 
 * Brief manual
 
 * 
 
 * The class fetches the letter from the template using Smarty and sends it using Manuel Lemos's class.
 
 * It is a "decorator" or "wrapper" to use both solutions and send cool letters with 5 lines of code.
 
 * 
 
 * Place the letter templates in the Smarty templates folder.
 
 * 
 
 * You can send the letter in plain text, HTML and multipart (html + plain text).
 
 * The format is determined by which files it will find in the template folder.
 
 * 
 
 * If you have "file_name.html.tpl" in the folder - it will send letter in HTML
 
 * If you have "file_name.txt.tpl" - it will send in plain text
 
 * If you have both - it will send multipart
 
 * You have neither, it will look for "file_name" or "file_name.tpl" and will send email in plain text
 
 * If you have none - it will trigger an error
 
 * You can restrict sending in some format in case you have both templates but don't want to use one.
 
 * 
 
 * If the first line of the template starts from "subject:" - the rest of the line is considered the letter subject
 
 * If you have both .txt. and .html. templates, the subject is searched in .html. at first, then in .txt. template
 
 * 
 
 * 
 
 * You can set most commonly used different options:
 
 * 
 
  $options = array(
 
        'template'=>'file_name', //no extension
 
        'to_address'=>'[email protected]',
 
        'to_name'=>'Grigori Kochanov',
 
        'from_name'=> ' Joe Black',     //default: _Config::mailing_from_name 
 
        'from_address'=> '[email protected]',     //default: _Config::mailing_from_address
 
        'reply_name'=>'',       // optional
 
        'reply_address'=>'',       // optional
 
        'error_delivery_name'=>'',      // optional
 
        'error_delivery_address'=>'',      // optional
 
        'send_html'=>'true',    //optional
 
        'send_plain'=>'true',    //optional
 
        'wrap_text'=>'true',    //optional
 
        'charset'=>'iso-8859-1',    //optional
 
        'precedence'=>'bulk', //to avoid the temporary failure notifications
 
   );
 
  $EML = new myMail();
 
  $EML->setOptions($ptions);
 
  $EML->assign('template_var',$script_data); // generic Smarty assign
 
  $EML->send();
 
 */
 
 
class myMail extends MySmarty {
 
 
public $wrap_text = false;
 
public $options;
 
 
private $Mail;
 
private $template_html;
 
private $template_plain;
 
 
function __construct(){
 
    parent::__construct();
 
    $this->template_dir = $this->config['MAIL_TEMPLATE_DIR'];
 
 
    $this->Mail = new email_message_class;
 
 
    $this->options=array(
 
        'from_name'=> $_SERVER['SERVER_NAME'],
 
        'from_address'=> $_SERVER['SERVER_ADMIN'],
 
        'x_from_name'=>null,
 
        'x_from_address'=>null,
 
        'reply_name'=>null,
 
        'reply_address'=>null,
 
        'error_delivery_name'=>null,
 
        'error_delivery_address'=>null,
 
        'to_address'=>'',
 
        'to_name'=>'',
 
        'template'=>'',
 
        'send_html'=>'true',
 
        'send_plain'=>'true',
 
        'wrap_text'=>'true',
 
        'charset'=>'iso-8859-1',
 
        'precedence'=>'bulk',
 
    );
 
}
 
 
/**
 
 * Set the options for the email
 
 *
 
 * @param array $options
 
 */
 
function setOptions(array $options){
 
    foreach (array_keys($this->options) as $parameter){
 
        if (key_exists($parameter,$options)){
 
            unset($this->options[$parameter]);
 
            $this->options[$parameter] = $options[$parameter];
 
            $this->assign($parameter,$options[$parameter]);
 
        }
 
    }
 
}
 
 
/**
 
 * @return void
 
 * @desc send an email using the template
 
*/
 
function send (){
 
    $op = $this->options;
 
    //check that options are not forgotten
 
    foreach (array('to_address','to_name','template') as $option){
 
        if (empty($this->options[$option])){
 
            trigger_error('Warning: mail parameter '.$option.' not defined',E_USER_WARNING);
 
        }
 
    }
 
    if (!$op['send_html'] && !$op['send_plain']){
 
        trigger_error('Error: neither HTML nor plain text message is to be sent.',E_USER_ERROR);
 
    }
 
 
    //assign options to the template
 
    foreach (array('to_address','to_name','from_address','from_name') as $option){
 
        $this->assign($option,$op[$option]);
 
    }
 
 
    //flag showing whether to send multipatr email or not
 
 
    //check if template(s) exist
 
    $template_html= 
 
        ($op['send_html'] && $this->template_exists($op['template'].'.html.tpl')) 
 
        ? $op['template'].'.html.tpl' 
 
        : null;
 
    $template_plain= 
 
        ($op['send_plain'] && $this->template_exists($op['template'].'.txt.tpl')) 
 
        ? $op['template'].'.txt.tpl' 
 
        : null;
 
 
    $send_multipart = ($template_plain && $template_html);
 
    $template_found = ($template_html || $template_plain);
 
 
    //if the templates not found - check the possible files
 
    if (!$template_found){
 
        //check old-style templates
 
        if ($this->template_exists($op['template'])){
 
            //template found
 
            $template_found = true;
 
            if ($op['send_plain']){
 
                $template_plain = $op['template'];
 
            }else{
 
                $template_html = $op['template'];
 
            }
 
        }
 
    }
 
    if (!$template_found){
 
        //check if the extension is not .tpl and try to add it
 
        if ( substr($op['template'],-4)!='.tpl' && $this->template_exists($op['template'].'.tpl')){ //template can't be found'
 
            //template found as $op['template'].'.tpl'
 
            $template_found=true;
 
            if ($op['send_plain']){
 
                $template_plain = $op['template'].'.tpl';
 
            }else{
 
                $template_html = $op['template'].'.tpl';
 
            }
 
        }
 
    }
 
    
 
    if (!$template_found){
 
        //no template files found
 
        trigger_error('Template "'.$op['template'].' not found',E_USER_ERROR);
 
    }
 
 
    //create object of the email_message_class and assign headers
 
    $email_message=new email_message_class;
 
    $email_message->SetEncodedEmailHeader("To",$op['to_address'],$op['to_name']);
 
    $email_message->SetEncodedEmailHeader("From",$op['from_address'],$op['from_name']);
 
    if ($op['reply_address']){
 
        $email_message->SetEncodedEmailHeader("Reply-To",$op['reply_address'],$op['reply_name']);
 
    }
 
    if ($op['x_from_address']){
 
        $email_message->SetEncodedEmailHeader("X-Sender",$op['x_from_address'],$op['x_from_name']);
 
    }
 
 
    // preparing message subject and body
 
    $subject='';
 
    $html_message='';
 
    if ($template_html){
 
        $tdata=$this->fetch($template_html); // fetching full tpl
 
        //fetch the subject
 
        if(strtolower(substr($tdata,0,8))=="subject:"){
 
            $i=strpos($tdata,"\n");
 
            $subject=substr($tdata,8,$i-8);
 
            $html_message=substr($tdata,$i+1,strlen($tdata)-$i-1);
 
        }else{
 
            $html_message=$tdata;
 
        }
 
        if ($this->wrap_text){
 
            $html_message = $email_message->WrapText($html_message);
 
        }
 
    }
 
 
    $plain_message = '';
 
    if ($template_plain){
 
        $tdata=$this->fetch($template_plain);
 
        //fetch the subject if not defined in HTML template
 
        if(strtolower(substr($tdata,0,9))=="subject: "){
 
            $i=strpos($tdata,"\n");
 
            $subject || $subject=substr($tdata,9,$i-9);
 
            $plain_message=substr($tdata,$i+1,strlen($tdata)-$i-1);
 
        }else{
 
            $plain_message=$tdata;
 
        }
 
        if ($this->wrap_text){
 
            $plain_message = $email_message->WrapText($plain_message);
 
        }
 
    }
 
 
    //set the subject
 
    $email_message->SetEncodedHeader("Subject",$subject);
 
 
    //set the body
 
    if ($send_multipart){
 
        //prepare the multipart mail
 
        $email_message->CreateQuotedPrintableTextPart($plain_message,$op['charset'],$plain_part);
 
        $email_message->CreateQuotedPrintableHTMLPart($html_message,$op['charset'],$html_part);
 
        $alternative_parts=array( //order of elements does matter here
 
            $plain_part,
 
            $html_part
 
        );
 
        $email_message->AddAlternativeMultipart($alternative_parts);
 
    }else{
 
        //send the the HTML letter
 
        if ($html_message){
 
            $email_message->AddQuotedPrintableHTMLPart($html_message,$op['charset']);
 
        }
 
        //send the plain text letter
 
        if ($plain_message){
 
            $email_message->AddQuotedPrintableTextPart($plain_message,$op['charset']);
 
        }
 
    }
 
    if ($op['precedence']){
 
        $email_message->SetHeader('Precedence',$op['precedence']);
 
    }
 
    // sending mail
 
    $error=$email_message->Send();
 
    if ($error){
 
        Operator::log('Error sending email to '.$op['to_address'].': '.$error,false);
 
        throw new pException(510);
 
    }
 
}
 
 
} // end of class bracket
 
 
?>
 
 |