communication
[ class tree: communication ] [ index: communication ] [ all elements ]

Source for file EMail.class.php

Documentation is available at EMail.class.php

  1. <?php
  2. /**
  3.  * @package communication
  4.  */
  5.  
  6. /**
  7.  *
  8.  */
  9.  include_once CLASSPATH."RegExpConstants.inc.php";
  10.  
  11. /**
  12.  *
  13.  */
  14.  require_once CLASSPATH."core/Arrays.class.php";
  15.  
  16. /**
  17.  * This class can be used to create emails of the type text or html. If you
  18.  * want you can add attachments.
  19.  *
  20.  * @package    communication
  21.  *
  22.  * @version    0.1.96
  23.  * @author     Daniel Plücken <daniel@debakel.net>
  24.  * @license    http://www.gnu.org/copyleft/lesser.html
  25.  *              GNU Lesser General Public License
  26.  * @copyright  Copyright (C) 2005 Daniel Plücken <daniel@debakel.net>
  27.  *
  28.  *  This library is free software; you can redistribute it and/or
  29.  *  modify it under the terms of the GNU Lesser General Public
  30.  *  License as published by the Free Software Foundation; either
  31.  *  version 2.1 of the License.
  32.  *
  33.  *  This library is distributed in the hope that it will be useful,
  34.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  35.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36.  *  GNU Lesser General Public License for more details.
  37.  *
  38.  *  You should have received a copy of the GNU Lesser General
  39.  *  Public License along with this library; if not, write to the
  40.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  41.  *  Boston, MA 02111-1307 USA
  42.  */
  43. class EMail
  44. {
  45.     /**
  46.      * Stores the e-mail address of the recipient.
  47.      *
  48.      * @var    string $target_email 
  49.      * @access public
  50.      */
  51.     var $target_email = "";
  52.     /**
  53.      * Stores the e-mail address of the recipient on reply.
  54.      *
  55.      * @var    string $reply_email 
  56.      * @access public
  57.      */
  58.     var $reply_email = "";
  59.     /**
  60.      * Stores the e-mail addresses of the recipients in carbon copy.
  61.      *
  62.      * @var    array  $target_email_copy_arr 
  63.      * @access public
  64.      */
  65.     var $target_email_copy_arr = array();
  66.     /**
  67.      * Stores the e-mail addresses of the recipients in blind carbon copy.
  68.      *
  69.      * @var    array  $target_email_blindcopy_arr 
  70.      * @access public
  71.      */
  72.     var $target_email_blindcopy_arr = array();
  73.     /**
  74.      * Stores the subject of the e-mail.
  75.      *
  76.      * @var    string $subject 
  77.      * @access public
  78.      */
  79.     var $subject = "Contact form";
  80.     /**
  81.      * Stores the content of the e-mail.
  82.      *
  83.      * @var    string $content 
  84.      * @access public
  85.      */
  86.     var $content = "";
  87.     /**
  88.      * Stores the type of content of the e-mail. Possible values are
  89.      * "text/plain" or "text/html" for plain text e-mail or html e-email.
  90.      *
  91.      * @var    string  $content_type 
  92.      * @access private
  93.      */
  94.     var $content_type = "text/plain";
  95.     /**
  96.      * Stores the filenames of attached files.
  97.      *
  98.      * @var    array  $attachment_filename_arr 
  99.      * @access public
  100.      */
  101.     var $attachment_filename_arr = array ();
  102.     /**
  103.      * Stores the content of the attachments.
  104.      *
  105.      * @var    array  $attachment_data_arr 
  106.      * @access public
  107.      */
  108.     var $attachment_data_arr = array ();
  109.     /**
  110.      * Stores the boundary for attachments.
  111.      *
  112.      * @var    string $str_attachment_boundary 
  113.      * @access public
  114.      */
  115.     var $str_attachment_boundary = "";
  116.     /**
  117.      * Stores the chars to seperate header information. It is immediatly adviced
  118.      * to let it the character "\n", because some email-provider can't handle
  119.      * header information separated by "\r\n" correctly!
  120.      *
  121.      * @var    string $str_header_separator 
  122.      * @access public
  123.      */
  124.     var $str_header_separator = "\n";
  125.  
  126.  
  127.  
  128.     /**
  129.      * Constructor
  130.      *
  131.      * @version 1.1
  132.      * @since   0.1.0
  133.      * @author  Daniel Plücken <daniel@debakel.net>
  134.      * @access  public
  135.      * @param   string $target_email 
  136.      */
  137.     function EMail$target_email )
  138.     $this->target_email = $target_email}
  139.  
  140.  
  141.  
  142.     /**
  143.      * Sets the type of content of the e-mail. Possible values are
  144.      * "text/plain" or "text/html" for plain text e-mail or html e-email.
  145.      *
  146.      * @version 1.0
  147.      * @since   0.1.6
  148.      * @author  Daniel Plücken <daniel@debakel.net>
  149.      * @access  public
  150.      * @param   $type 
  151.      * @return  void 
  152.      */
  153.     function setContentType$type )
  154.     {
  155.       if preg_match"!html!i"$type ) )
  156.          $this->content_type = "text/html";
  157.       else
  158.          $this->content_type = "text/plain";
  159.     }
  160.  
  161.  
  162.  
  163.     /**
  164.      * Returns whether the given email is valid.
  165.      *
  166.      * @version 1.0
  167.      * @since   0.1.9
  168.      * @author  Daniel Plücken <daniel@debakel.net>
  169.      * @static
  170.      * @access  public
  171.      * @param   string  $string The email to validate.
  172.      * @return  boolean 
  173.      */
  174.     function is_valid$string )
  175.     return preg_match"!^".EMAIL."$!"$string )}
  176.  
  177.  
  178.  
  179.     /**
  180.      * Returns whether the given email is valid.
  181.      * The function name is a result of my bad english :P - sorry for that!
  182.      *
  183.      * @deprecated
  184.      * @version 1.0
  185.      * @since   0.1.2
  186.      * @author  Daniel Plücken <daniel@debakel.net>
  187.      * @static
  188.      * @access  public
  189.      * @param   string  $string The email to validate.
  190.      * @return  boolean 
  191.      */
  192.     function is_guilty$string )
  193.     return EMail::is_valid$string )}
  194.  
  195.  
  196.  
  197.     /**
  198.      * Returns the stored email.
  199.      *
  200.      * @version 1.0
  201.      * @since   0.1.2
  202.      * @author  Daniel Plücken <daniel@debakel.net>
  203.      * @access  public
  204.      * @return  string 
  205.      */
  206.     function getTargetEmail()
  207.     return $this->target_email}
  208.  
  209.  
  210.  
  211.     /**
  212.      * Adds an attachment to this email.
  213.      *
  214.      * @version 1.0
  215.      * @since   0.1.2
  216.      * @author  Daniel Plücken <daniel@debakel.net>
  217.      * @access  public
  218.      * @param   string $filename 
  219.      * @param   string $data 
  220.      * @return  void 
  221.      */
  222.     function addAttachment$filename$data )
  223.     {
  224.        $this->str_attachment_boundary = strtoupper(md5(uniqid(time())));
  225.  
  226.        $this->attachment_filename_arr[$filename;
  227.        $this->attachment_data_arr[chunk_split(base64_encode($data));
  228.     }
  229.  
  230.  
  231.  
  232.     /**
  233.      * Stores the email where the message should be send.
  234.      *
  235.      * @version 1.1
  236.      * @since   0.1.2
  237.      * @author  Daniel Plücken <daniel@debakel.net>
  238.      * @access  public
  239.      * @param   string $string 
  240.      * @return  void 
  241.      */
  242.     function setTargetEmail$string )
  243.     $this->target_email = $string}
  244.  
  245.  
  246.  
  247.     /**
  248.      * Stores the email where the message should be send on reply.
  249.      *
  250.      * @version 1.0
  251.      * @since   0.1.8
  252.      * @author  Daniel Plücken <daniel@debakel.net>
  253.      * @access  public
  254.      * @param   string $string 
  255.      * @return  void 
  256.      */
  257.     function setReplyEmail$string )
  258.     $this->reply_email = $string}
  259.  
  260.  
  261.  
  262.     /**
  263.      * Adds an email to the array of emails where the message should be send in
  264.      * copy.
  265.      *
  266.      * @version 1.0
  267.      * @since   0.1.2
  268.      * @author  Daniel Plücken <daniel@debakel.net>
  269.      * @access  public
  270.      * @param   string $string 
  271.      * @return  void 
  272.      */
  273.     function addTargetEmailCopy$string )
  274.     $this->target_email_copy_arr[$string}
  275.  
  276.  
  277.  
  278.     /**
  279.      * Stores the emails where the message should be send in copy.
  280.      *
  281.      * @version 1.0
  282.      * @since   0.1.2
  283.      * @author  Daniel Plücken <daniel@debakel.net>
  284.      * @access  public
  285.      * @param   array  $array 
  286.      * @return  void 
  287.      */
  288.     function setTargetEmailCopyArray$array )
  289.     $this->target_email_copy_arr = $array}
  290.  
  291.  
  292.  
  293.     /**
  294.      * Adds an email to the array of emails where the message should be send in
  295.      * blind copy.
  296.      *
  297.      * @version 1.0
  298.      * @since   0.1.2
  299.      * @author  Daniel Plücken <daniel@debakel.net>
  300.      * @access  public
  301.      * @param   string $string 
  302.      * @return  void 
  303.      */
  304.     function addTargetEmailBlindCopy$string )
  305.     $this->target_email_blindcopy_arr[$string}
  306.  
  307.  
  308.  
  309.     /**
  310.      * Stores the emails where the message should be send in blind copy.
  311.      *
  312.      * @version 1.0
  313.      * @since   0.1.2
  314.      * @author  Daniel Plücken <daniel@debakel.net>
  315.      * @access  public
  316.      * @param   array  $array 
  317.      * @return  void 
  318.      */
  319.     function setTargetEmailBlindCopyArray$array )
  320.     $this->target_email_blindcopy_arr = $array}
  321.  
  322.  
  323.  
  324.     /**
  325.      * Returns the generated attachment data as header information in mime
  326.      * type.
  327.      *
  328.      * @version 1.0
  329.      * @since   0.1.52
  330.      * @author  Daniel Plücken <daniel@debakel.net>
  331.      * @access  public
  332.      * @return  string 
  333.      */
  334.     function getAttachmentContent()
  335.     {
  336.       $out "";
  337.       for $i 0$i count$this->attachment_filename_arr )$i++ )
  338.       {
  339.           $out .= "--".$this->str_attachment_boundary
  340.                       .$this->str_header_separator;
  341.           $out .= "Content-Type: application/octetstream; "
  342.                  ."name=\"".$this->attachment_filename_arr[$i]."\""
  343.                  .$this->str_header_separator;
  344.           $out .= "Content-Transfer-Encoding: base64"
  345.                  .$this->str_header_separator;
  346.           $out .= "Content-Disposition: attachment; "
  347.                  ."filename=\"".$this->attachment_filename_arr[$i]."\""
  348.                  .$this->str_header_separator.$this->str_header_separator;
  349.           $out .= $this->attachment_data_arr[$i].$this->str_header_separator;
  350.       }
  351.  
  352.       return $out;
  353.     }
  354.  
  355.  
  356.  
  357.     /**
  358.      * Sends the email with values of the form to the addressee stored in
  359.      * $this->target_email.
  360.      *
  361.      * @version 1.32
  362.      * @since   0.1.2
  363.      * @author  Daniel Plücken <daniel@debakel.net>
  364.      * @access  public
  365.      * @param   string  $sender_email 
  366.      * @param   string  $subject 
  367.      * @param   string  $content 
  368.      * @return  boolean 
  369.      */
  370.     function send$sender_email$subject ""$content "" )
  371.     {
  372.       ifempty$subject ) )
  373.         $subject $this->subject;
  374.  
  375.       ifempty$content ) )
  376.         $content $this->content;
  377.  
  378.       /* DEBUG
  379.       echo "<pre>";
  380.         echo "Header:".
  381.                    "From: ".$sender_email."\r\n"
  382.                   ."Reply-To: ".$sender_email."\r\n"
  383.                   .(
  384.                      !Arrays::is_empty( $this->target_email_copy_arr )
  385.                      ? "Cc: ".implode(
  386.                                  ", ", $this->target_email_copy_arr
  387.                                      )."\r\n"
  388.                      : ""
  389.                    )
  390.                   .(
  391.                      !Arrays::is_empty( $this->target_email_blindcopy_arr )
  392.                      ? "Bcc: ".implode(
  393.                                   ", ", $this->target_email_blindcopy_arr
  394.                                       )."\r\n"
  395.                      : ""
  396.                    )
  397.                   .(
  398.                      count( $this->attachment_filename_arr ) > 0
  399.                      ? "MIME-Version: 1.0\r\n"
  400.                       ."Content-Type: multipart/mixed; boundary="
  401.                                      .$this->str_attachment_boundary."\r\n\r\n"
  402.                       ."--".$this->str_attachment_boundary."\r\n"
  403.                       ."Content-Type: ".$this->content_type."\r\n"
  404.                       ."Content-Transfer-Encoding: 8bit\r\n\r\n"
  405.                       .$content."\r\n"
  406.                       .$this->getAttachmentContent()
  407.                       ."--".$this->str_attachment_boundary."--"
  408.                      : "Content-Type: ".$this->content_type."\r\n"
  409.                    );
  410.       echo "--------------------------------\r\n";
  411.       echo "Sender: ".$sender_email."\r\n";
  412.       echo "--------------------------------\r\n";
  413.       echo "Receiver: ".preg_replace(
  414.                                  "!^.+?\b(".EMAIL.")\b.+?$!i",
  415.                                  "$1", $this->target_email
  416.                                     )."\r\n";
  417.         echo "--------------------------------\r\n";
  418.       echo "Subject: ".$subject."\r\n";
  419.       echo "--------------------------------\r\n";
  420.       echo "Content: ".$content."\r\n";
  421.       echo "</pre>";
  422.       // DEBUG */
  423.  
  424.       return mail(
  425.                    preg_replace(
  426.                                  "!^.+?\b(".EMAIL.")\b.+?$!i",
  427.                                  "$1"$this->target_email
  428.                                ),
  429.                    $subject$content,
  430.                    "From: ".$sender_email.$this->str_header_separator
  431.                   ."Reply-To: ".(
  432.                         empty$this->reply_email )
  433.                         ? $sender_email
  434.                         : $this->reply_email
  435.                                 ).$this->str_header_separator
  436.                   .(
  437.                      !Arrays::is_empty$this->target_email_copy_arr )
  438.                      ? "Cc: ".implode(
  439.                                  ", "$this->target_email_copy_arr
  440.                                      ).$this->str_header_separator
  441.                      : ""
  442.                    )
  443.                   .(
  444.                      !Arrays::is_empty$this->target_email_blindcopy_arr )
  445.                      ? "Bcc: ".implode(
  446.                                   ", "$this->target_email_blindcopy_arr
  447.                                       ).$this->str_header_separator
  448.                      : ""
  449.                    )
  450.                   .(
  451.                      count$this->attachment_filename_arr 0
  452.                      ? "MIME-Version: 1.0".$this->str_header_separator
  453.                       ."Content-Type: multipart/mixed; boundary="
  454.                                      .$this->str_attachment_boundary
  455.                                      .$this->str_header_separator
  456.                                      .$this->str_header_separator
  457.                       ."--".$this->str_attachment_boundary
  458.                            .$this->str_header_separator
  459.                       ."Content-Type: ".$this->content_type
  460.                                        .$this->str_header_separator
  461.                       ."Content-Transfer-Encoding: 8bit"
  462.                       .$this->str_header_separator.$this->str_header_separator
  463.                       .$content.$this->str_header_separator
  464.                       .$this->getAttachmentContent()
  465.                       ."--".$this->str_attachment_boundary."--"
  466.                      : "Content-Type: ".$this->content_type
  467.                                        .$this->str_header_separator
  468.                    )
  469.                  );
  470.     }
  471. // END of class EMail
  472. ?>

Documentation generated on Thu, 05 Jun 2008 19:11:18 +0200 by phpDocumentor 1.4.1