home *** CD-ROM | disk | FTP | other *** search
Wrap
Email Template Class Email class that can load templates from file, insert variables, and send. Useful for form emails and dynamic email generation. Supports full names in both to: and from: fields like Bill Clinton <president@whitehouse.gov>. <?php /******************************************************************* Email loading/parsing/sending class By Eric Hanson, eric@cornado.com, http://www.cornado.com/ April 5, 1999 - send () sends the email - load () reads an email template from file, replacing variables with literals - report () prints a simple report of the email - supports full names like Bill Clinton <president@whitehouse.gov> This code can be used for anything you'd like. See examples at http://www.cornado.com/dev/email.inc/ Music of choice while programming: They Might Be Giants, Orb, Propellerheads, Hempilation ][, Phish ---example usage begin--- require "email.inc"; // some variables that have been set $firstname = "Juan"; $lastname = "Sanchez"; $email = "j@phileric.com"; // the list of variables to be parsed out of the email file $globals = "\$firstname, \$lastname, \$email"; // instantiate $e = new Email; // load if (!$e->load ("example.txt",$globals)) print "Could not load."; // report $e->report(); // send if (!$e->send()) print "Could not send."; ?> ---example usage end--- ---example email template begin--- to: <$firstname $lastname> $email from: <Eric Hanson> eric@cornado.com subject: Welcome to the secret society $firstname! Dearest $firstname, Welcome! You are now a member of the secret society. Sincerely, Eric ---example email template end--- ********************************************************************/ if ($EMAIL_INC) return; $EMAIL_INC= "defined"; class Email { var $subject; // string the email's subject var $from_email; // string sender's email address var $from_name; // string sender's name (opt) var $to_email; // string recipient's email var $to_name; // string recipient's name (opt) var $body; // string body copy var $filename; // the filename /* bool load ($filename) loads an email from file, and parses out variables, subject, to, from, etc. */ function load ($filename = "",$globals= "") { if (!$filename) { $filename = $this->filename; } if (!$dump = file ($filename)) { return 0; } if ($globals) { eval ( "global $globals;"); } /* for each line in the file, replace any variables with their values and use lines that start with to:, from:, or subject: to set their respective values. */ while (list($key,$val) = each($dump)) { /* replace variables in the textfile with their actual values. */ if (ereg( "\$",$val)) { $val = addslashes ($val); eval( "\$val = \"$val\";"); $val = stripslashes ($val); } if (eregi( " *to: *<([^>]*)> *([^ ]*) *",$val,$regs)) { $this->to_name = $regs[1]; $this->to_email = chop($regs[2]); } else if (eregi( "^ *to: *([^ ]*).*",$val,$regs)) { $this->to_email = chop($regs[1]); } else if (eregi( " *from: *<([^>]*)> *([^ ]*) *",$val,$regs)) { $this->from_name = $regs[1]; $this->from_email = chop($regs[2]); } else if (eregi( "^ *from: *(.*)",$val,$regs)) { $this->from_email = chop($regs[1]); } else if (eregi( "^ *subject: *(.*)",$val,$regs)) { $this->subject = chop($regs[1]); } else { $this->body=$this->body.$val; } } return 1; } function send () { /* set the from line */ if ($this->from_name) { $from_line = "From: ".$this->from_name. " <".$this->from_email. ">\n"; } else { $from_line = "From: ".$this->from_email. "\n"; } /* set the to line */ if ($this->to_name) { $to_line = $this->to_name. " <".$this->to_email. ">"; } else { $to_line = $this->to_email; } return mail( $to_line, $this->subject, $this->body, $from_line); } function report () { ?> <table> <tr> <td align="right"> <b>To:</b> </td> <td> <?php if ($this->to_name) { print "<".$this->to_name. "> "; } print $this->to_email; ?> </td> </tr> <tr> <td align="right"> <b>From:</b> </td> <td> <?php if ($this->from_name) { print "<".$this->from_name. "> "; } print $this->from_email; ?> </td> </tr> <tr> <td align="right"> <b>Subject:</b> </td> <td> <?php print $this->subject; ?> </td> </tr> <tr> <td align="right" valign="top"> <b>Body:</b> </td> <td><pre> <?php print $this->body; ?> </td></pre> </tr> </table> <?php } } ?>