home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / emailclasstempl.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  5.2 KB  |  213 lines

  1. Email Template Class 
  2.  
  3.  
  4. 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>. 
  5.  
  6.  
  7.  
  8. <?php
  9. /*******************************************************************
  10. Email loading/parsing/sending class
  11. By Eric Hanson, eric@cornado.com, http://www.cornado.com/
  12. April 5, 1999
  13.  
  14. - send () sends the email
  15. - load () reads an email template from file, replacing variables with literals
  16. - report () prints a simple report of the email
  17. - supports full names like 
  18.   Bill Clinton <president@whitehouse.gov>
  19.  
  20. This code can be used for anything you'd like.  See examples at
  21. http://www.cornado.com/dev/email.inc/
  22.  
  23. Music of choice while programming:
  24.   They Might Be Giants, Orb, Propellerheads, Hempilation ][, Phish
  25.  
  26. ---example usage begin---
  27. require "email.inc";
  28.  
  29. // some variables that have been set
  30. $firstname = "Juan";
  31. $lastname  = "Sanchez";
  32. $email     = "j@phileric.com";
  33.  
  34. // the list of variables to be parsed out of the email file 
  35. $globals = "\$firstname, \$lastname, \$email";
  36.  
  37. // instantiate
  38. $e = new Email;
  39.  
  40. // load 
  41. if (!$e->load ("example.txt",$globals)) 
  42.   print "Could not load.";
  43.  
  44. // report
  45. $e->report();
  46.  
  47. // send
  48. if (!$e->send()) 
  49.   print "Could not send.";
  50. ?>
  51. ---example usage end---
  52.  
  53. ---example email template begin---
  54. to: <$firstname $lastname> $email
  55. from: <Eric Hanson> eric@cornado.com
  56. subject: Welcome to the secret society $firstname!
  57. Dearest $firstname,
  58.  
  59. Welcome!  You are now a member of the secret society.
  60.  
  61. Sincerely,
  62. Eric
  63. ---example email template end---
  64.   
  65. ********************************************************************/ 
  66.  
  67. if ($EMAIL_INC) return;
  68. $EMAIL_INC= "defined";
  69.  
  70. class Email {
  71.   var $subject;                         // string the email's subject 
  72.   var $from_email;                      // string sender's email address 
  73.   var $from_name;                       // string sender's name (opt) 
  74.   var $to_email;                        // string recipient's email 
  75.   var $to_name;                         // string recipient's name (opt) 
  76.   var $body;                            // string body copy 
  77.   var $filename;                        // the filename 
  78.  
  79.    /*
  80.   bool load ($filename)
  81.   loads an email from file, and parses out variables, subject, to, from, etc.
  82.   */ 
  83.   
  84.   function load ($filename =  "",$globals= "") {
  85.     if (!$filename) { $filename = $this->filename; }
  86.     
  87.     if (!$dump = file ($filename)) {
  88.       return 0;
  89.     }
  90.     
  91.     if ($globals) {
  92.       eval ( "global $globals;");
  93.     }
  94.     
  95.      /* for each line in the file, replace any variables with their values 
  96.     and use lines that start with to:, from:, or subject: to set their
  97.     respective values. */ 
  98.     
  99.     while (list($key,$val) = each($dump)) {
  100.        /*
  101.       replace variables in the textfile with their actual values.
  102.       */ 
  103.       if (ereg( "\$",$val)) {
  104.         $val = addslashes ($val);      
  105.         eval(  "\$val = \"$val\";");
  106.         $val = stripslashes ($val);
  107.       }
  108.       
  109.       if (eregi( " *to: *<([^>]*)> *([^ ]*) *",$val,$regs)) {
  110.         $this->to_name = $regs[1];
  111.         $this->to_email = chop($regs[2]);
  112.       }
  113.       else if (eregi( "^ *to: *([^ ]*).*",$val,$regs)) {
  114.         $this->to_email = chop($regs[1]);
  115.       }
  116.       else if (eregi( " *from: *<([^>]*)> *([^ ]*) *",$val,$regs)) {
  117.         $this->from_name = $regs[1];
  118.         $this->from_email = chop($regs[2]);
  119.       }
  120.       else if (eregi( "^ *from: *(.*)",$val,$regs)) {
  121.         $this->from_email = chop($regs[1]);
  122.       }
  123.       else if (eregi( "^ *subject: *(.*)",$val,$regs)) {
  124.         $this->subject = chop($regs[1]);
  125.       }
  126.       else {
  127.         $this->body=$this->body.$val;
  128.       }
  129.     }
  130.     return 1;
  131.   }
  132.   
  133.   function send () {
  134.      /* set the from line */ 
  135.     if ($this->from_name) {
  136.       $from_line =  "From: ".$this->from_name. " <".$this->from_email. ">\n";
  137.     }
  138.     else {
  139.       $from_line =  "From: ".$this->from_email. "\n";
  140.     }
  141.     
  142.      /* set the to line */ 
  143.     if ($this->to_name) {
  144.       $to_line = $this->to_name. " <".$this->to_email. ">";
  145.     }
  146.     else {
  147.       $to_line = $this->to_email;
  148.     }
  149.     
  150.     return mail(
  151.       $to_line,
  152.       $this->subject,
  153.       $this->body,
  154.       $from_line);
  155.   }
  156.   
  157.   function report () {
  158. ?>  
  159.     <table>
  160.       <tr>
  161.         <td align="right">
  162.       <b>To:</b>
  163.     </td>
  164.     <td>
  165. <?php
  166.       if ($this->to_name) {
  167.         print   "<".$this->to_name. "> ";
  168.       }
  169.       print $this->to_email;
  170. ?>
  171.     </td>
  172.       </tr>
  173.       <tr>
  174.         <td align="right">
  175.       <b>From:</b>
  176.     </td>
  177.     <td>
  178. <?php 
  179.       if ($this->from_name) { 
  180.         print   "<".$this->from_name. "> ";
  181.       }
  182.       print $this->from_email; 
  183. ?>
  184.     </td>
  185.       </tr>
  186.       <tr>
  187.         <td align="right">
  188.       <b>Subject:</b>
  189.     </td>
  190.     <td>
  191. <?php 
  192.       print $this->subject; 
  193. ?>
  194.     </td>
  195.       </tr>
  196.       <tr>
  197.         <td align="right" valign="top">
  198.       <b>Body:</b>
  199.     </td>
  200.     <td><pre>
  201. <?php 
  202.       print $this->body; 
  203. ?>
  204.     </td></pre>
  205.       </tr>
  206.     </table>
  207. <?php      
  208.   }
  209. }
  210. ?>
  211.  
  212.  
  213.