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 : Campaign Mailer - Allows sending emails to a mailing list with multiple classes of emails handled. Suitable for mailing campaigns.
Categories : PHP, PHP Classes, Email Click here to Update Your Picture
Sarah King
Date : Jun 18th 2004
Grade : 3 of 5 (graded 6 times)
Viewed : 5859
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Sarah King
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 




Name: massMail
Description: Allows sending emails to a mailing list with multiple classes of emails handled
Suitable for mailing campaigns

Version: 1.0
last modified: 2004-06-18

Developer: Sarah King
Homepage:                 http://sarah.pcpropertymanager.com


<?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( )
     {
         
// LINK2http://www.weberforums.com/viewtopic.php?p=13833#13833LINK2
       
$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();

?>



Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes
Validator - A PHP class that can can be used for validating Email IDs and Dates
Categories : PHP, PHP Classes, Data Validation, Email, Date Time
Three Cool Classes and One Trick
Categories : PHP, PHP Classes, Graphics, Email
PHP MIME Decoder. This class decodes Mime Encoded email message. Attachments are stored in a director. Works with Multipart/alternative, multipart/mixed etc. see http://p3mail.com for example.
Categories : PHP, PHP Classes, Email
cPanel Email Accounts Creator
Categories : PHP, PHP Classes, Email, Form Processing, Web Services
Class that allows the PHP developer to establish connections with a POP3 mail server amd be able to list, retrieve and delete mail messages from a given mail box.
Categories : Network, Email, PHP, PHP Classes
A class for sending email; it has support for To:, Cc:, Bcc: and Reply-To: headers. It requires that you have sendmail installed.
Categories : Email, PHP Classes, PHP
POP3 Class
Categories : PHP Classes, PHP, Email
very simple ftp class
Categories : PHP, PHP Classes, FTP
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
PHP based Contact email form with multiple recipients, text file based, supports departments.
Categories : PHP, Email, Beginner Guides, Filesystem
 matthew waygood wrote : 1133
When you initialise the class, you are setting the from email address. When you send() the email you are also adding a second FROM.
I liked the personalising of the emails by doing a mail-merge with some data, but it would be nice to set some default values incase you omit them. This would then remove the {mailmerge value} codes too.

ie 
Dear Bob, Dear Claire, Dear {customer_name}
 with a default customer_name as customer you would get
Dear Bob, Dear Claire, Dear customer
 you could also set defaults to empty for other values, such as passwords.

It would also be nice to see a clear send list function. This is to prevent the dataset growing too big before sending, allowing us to repeatedly load list, send and clear. We wouldnt need to recontruct the email again for a second set.

ie
$mail=new massMail("sender","sender@somewhere.com");
// contruct here
$counter=0;
while($row=mysql_fetch_assoc($query_pointer))
{
  $mail-&gt;setTo($row[`email`], array(`class` =&gt; $row[`class`],`{LISTID}` =&gt; $row[`9049`]));
  $counter++;
  if($counter&gt;50)
  {
     $counter=0;
     $mail-&gt;send();
     $mail-&gt;clear_send_list();
  } 
}
if($counter&gt;0)
{
  $mail-&gt;send();
}

This example used on a dataset of 100000000 people wouldn`t overload the memory too much. I know execution time is a consideration, but if you use this in part of a cron job, you would have no worries of a system crash.
 
 Jose Santos wrote :1134
Hello !

I`ve develloped an system to send mass mail, using an csv file to subscribe / unsubscribe, ...
And with csv files you made to have more information (example: "name","email","from","city","phone" ...)
It dont have limit, and you can optimize the csv class to accept any parameters.

To send an mass mail, only get the "email" column of all lines of this file (csv).

I think this is more practice and best !!