Campaign,Mailer,sending,emails,mailing,list,php

Patrick64 9/8/2016 0

Campaign Mailer in php - Allows sending emails to a mailing list with multiple classes

PHP
<?php 
<? 


class massMail 
{ 
     var $subject = array( ); 
     var $body = array( ); 
     var $to = array( ); 
     var $attachment = array( ); 
     var $boundary = ''; 
     var $header = ''; 
     var $sender = '' 
     
     function massMail( $name, $mail) 
     { 
         $this->boundary = md5( uniqid( time( ))); 
         $this->sender = "From: {$name} <{$mail}>\r\n"; 
     } 
     
     function setTo( $mail, $vars) 
     { 
         $this->to[$mail] = $vars; 
     } 
     
     function attachment( $file) 
     { 
         $this->attachment[] = $file; 
     } 
     
     function setSubject( $name, $subject) 
     { 
         $this->subject[$name] = $subject; 
     } 
     
     function setBody( $name, $str) 
     { 
         $this->body[$name] = $str; 
     } // function setBody($type, $name, $str) 
     
     function getBody( $vars) 
     { 
         if ( isset( $vars['class'])) $class = $vars['class']; 
         else $class = 0; 
         
         if ( isset( $this -> body[$class])) $body = $this -> body[$class]; 
         elseif ( count( $this -> body) > 0) $body = $this -> body[0]; 
         else $body = ''; 
         
         foreach( $vars as $k => $v) 
        { 
             if ( $k != 'class') $body = str_replace( $k, $v, $body); 
        } 
         return $body; 
     } //    function getBody($vars) 
     
     function getSubject( $class) 
     { 
         if ( empty( $class)) $class = 0; 
         return $this -> subject[$class]; 
     } //    function getSubject($class) 
     
     function addStyle( $html) 
     { 
         // add any styles that you need. Be aware that they may not 
        // be read by the email client the same as a browser 
        $output = "<html><head> 
        <style> 
        body, table { background-color : #FEFEFE; color : Navy; font-family : Arial, Verdana, Helvetica, sans-serif; font-size : 12px; }   
        .key { font : Verdana; font-size : 12px;} 
        .reverse { background-color : Navy; color : #FEFEFE;} 
        .reverse A{ color: #FEFEFE;} 
        th { text-align : right; font-weight : bold; vertical-align : top;} 
        .floatright { float: right;} 
        </style> 
        </head> 
        <body> 
        {$html} 
        </body></html> 
        "; 
         return $output; 
     } //function addStyle($html) 
     
     function getTextSection( $html) 
     { 
         // plain text version of message 
        $txt = strip_tags( $html); 
         $body = "--{$this->boundary}\r\n" . 
         "Content-Type: text/plain; charset=ISO-8859-1\r\n" . 
         "Content-Transfer-Encoding: base64\r\n\r\n"; 
         $body .= chunk_split( base64_encode( $txt)); 
         return $body; 
     } //getTextSection 
     
     function getAttachments( ) 
     { 
         $tail = ''; 
         $max = count( $this -> attachment); 
         if( $max > 0) 
        { 
             for( $i = 0;$i < $max;$i  ) 
             { 
                 $file = fread( fopen( $this -> attachment[$i], 'r'), filesize( $this -> attachment[$i])); 
                 $tail .= "--{$this->boundary}\n"; 
                 $tail .= "Content-Type: application/x-zip-compressed; name=" . $this -> attachment[$i] . "\n"; 
                 $tail .= "Content-Transfer-Encoding: base64\n"; 
                 $tail .= "Content-Disposition: attachment; filename=" . $this -> attachment[$i] . "\n\n"; 
                 $tail .= chunk_split( base64_encode( $file)) . "\n"; 
                 $file = ""; 
             } 
         } 
         $tail .= "--{$this->boundary}--\n\n"; 
         return $tail; 
     } //function getAttachments() 
     
     function getHTMLSection( $html) 
     { 
         // HTML version of message 
        $body .= "--{$this->boundary}\r\n" . 
         "Content-Type: text/html; charset=ISO-8859-1\r\n" . 
         "Content-Transfer-Encoding: base64\r\n\r\n"; 
         $body .= chunk_split( base64_encode( $html)); 
         return $body; 
     } //function getHTMLSection($html) 
     
     function send( ) 
     { 
        
        $top = $this->sender . "MIME-Version: 1.0\r\n"; 
         // tell e-mail client this e-mail contains//alternate versions 
        $top .= "Content-Type: multipart/alternative" . 
         "; boundary = {$this->boundary}\r\n\r\n"; 
        $top .= "This is a MIME encoded message.\r\n\r\n"; 
         
         // Attachment is always last 
        $tail = $this->getAttachments( ); 
         
        foreach( $this->to as $mail => $vars) 
        { 
             $html = $this -> getBody( $vars); 
             $body = $this->getTextSection( $html); 
             $html = $this -> addStyle( $html); 
             $body .= $this->getHTMLSection( $html); 
             
             $subject = $this -> getSubject( "{$vars['class']}"); 
             mail( $mail, $subject, '', "{$top}{$body}\n{$tail}"); 
        } 
        $this->to = array();  //can build up the to list and send another batch 
     } //function send() 
} //class massMail 


/** 
* massMail Test Script 
* 
**/ 

//dummy script to get the body of each email type. Typically it would query the database 
function getFUBody($type) 
{ 
  if ($type == 1) $output = 'hello world<br>My Id is {LISTID}<br>bye now'; 
  elseif ($type == 2) $output = 'That was fun<br>My Id is {LISTID}<br>bye now'; 
  elseif ($type == 3) $output = 'third time lucky<br>My Id is {LISTID}<br>bye now'; 
  else $output = 'Last campaign message<br>My Id is {LISTID}<br>bye now'; 
   
  $subject = "Email {$type}"; 
  return array($subject, $output);   
} 


    $mail = new massMail('Senders name','support@mysite.com'); 
     
    //preload each email type for the campaign 
    for($i = 1; $i < 5; $i  ) 
    { 
        list($subject, $body) = getFUBody($i); 
        $mail->setSubject("{$i}", $subject); 
        $mail->setBody("{$i}", $body); 
    } 

    //add each address you need to email and each of the variables embedded in the email body 
    //this example includes a database id for the unsubscribe process 
    //but could include any number of personalization variables. 
    $mail->setTo('sarah@mysite.com', array('class' => '2','{LISTID}' => '9049')); 
    $mail->setTo('joe@mysite.com', array('class' => '2','{LISTID}' => '851')); 
    //send 
    $mail->send(); 

    $mail->setTo('webmaster@mysite.com', array('class' => '1','{LISTID}' => '9050')); 
    $mail->setTo('support@mysite.com', array('class' => '3','{LISTID}' => '9067')); 
    //send 
    $mail->send(); 

?>
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments