<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">&lt;?php 

/**
* A PHP class for sending email messages which includes capabilities for sending plaintext, html and multipart messages, including support for attachments.
* 
* While none of the capabilities of this class exceed those of more established packages such as PEARMail, my hope is that this class offers the advantage of
* portability, since not every setup has the ability to install PEAR packages.  Also, this class is ideal for embedding in other systems, which I have done in
* the case of my MVC framework, DirtyMVC.
*
* @author Steve Lewis &lt;steve@thoughtsandrambles.com&gt;
* @version 0.1.0
* @package Mail
*
**/
  class Mail{
    /**
    * @var string $to The email address, (or addresses, if comma separated), of the person to which this email should be sent.
    **/
    public $to;

    /**
    * @var string $from The email address that this mail will be delivered from. Bear in mind that this can be anything, but that if the email 
    *                   domain doesn't match the actual domain the message was sent from, some email clients will reject the message as spam.
    **/ 
    public $from;
    
    /**
    * @var string $subject The subject line of the email
    **/ 
    public $subject;

    /**
    * @var string $text_content The plaintext version of the message to be sent.
    **/ 
    public $text_content;

    /**
    * @var string $html_content The HTML version of the message to be sent.
    **/
    public $html_content;
    
    /**
    * @var string $body The complete body of the email that will be sent, including all mixed content.
    **/ 
    private $body;

    /**
    * @var array $attachments  An array of file paths pointing to the attachments that should be included with this email.
    **/
    private $attachments;
    
    /**
    * @var array $headers An array of the headers that will be included in this email.
    **/ 
    private $headers;

    /**
    * @var string $header_string The string, (and therefore final), representation of the headers for this email message.
    **/ 
    private $header_string;

    /**
    * @var string $boundary_hash The string that acts as a separator between the various mixed parts of the email message.
    **/  
    private $boundary_hash;

    /**
    * @var boolean $sent Whether or not this email message was successfully sent.
    **/
    private $sent;

    
    /**
    * Upon initialization of a Mail object, you have to pass it certain vital pieces of information.
    *
    * At a minimum, an email must consist of a receiver address, a sender address, and a subject.
    * The body can be left blank.
    **/
    public function __construct($to, $from, $subject, $text_content = "", $html_content = ""){
      $this-&gt;to            = $to;
      $this-&gt;from          = $from;
      $this-&gt;subject       = $this-&gt;convert_utf8($subject);
      $this-&gt;text_content  = $text_content;
      $this-&gt;html_content  = $html_content;
      $this-&gt;body          = "";
      $this-&gt;attachments   = array();
	  $this-&gt;base64_attachments   = array();
      $this-&gt;headers       = array();
      $this-&gt;boundary_hash = md5(date('r', time()));
    }

    /**
    * The send() method processes all headers, body elements and attachments and then actually sends the resulting final email.
    **/
    public function send(){
      $this-&gt;prepare_headers();      
      $this-&gt;prepare_body();
      if(!empty($this-&gt;attachments)){
        $this-&gt;prepare_attachments();  
      }
      if(!empty($this-&gt;base64_attachments)){
        $this-&gt;prepare_base64_attachments();  
      }
      $this-&gt;sent = mail($this-&gt;to, $this-&gt;subject, $this-&gt;body, $this-&gt;header_string);
      return $this-&gt;sent;
    }

    /**
    * This method allows the user to add a new header to the message
    * @param string $header The text for the header the user wants to add. Note that this string must be a properly formatted email header.
    **/
    public function add_header($header){
      $this-&gt;headers[] = $header;
    }

    /**
    * Add a filepath to the list of files to be sent with this email.
    * @param string $file The path to the file that should be sent.
    **/
    public function add_attachment($file){
      $this-&gt;attachments[] = $file;
    }
    public function add_base64_attachment($name,$data){
      $this-&gt;base64_attachments[] = array('name'=&gt;$name, 'data'=&gt;$data);
    }
    private function prepare_body(){
      $this-&gt;body .= "--PHP-mixed-{$this-&gt;boundary_hash}\n";
      $this-&gt;body .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$this-&gt;boundary_hash}\"\n\n";
      if(!empty($this-&gt;text_content)) $this-&gt;prepare_text();
      if(!empty($this-&gt;html_content)) $this-&gt;prepare_html();
      $this-&gt;body .= "--PHP-alt-{$this-&gt;boundary_hash}--\n\n";
    }

    private function prepare_headers(){
      $this-&gt;set_default_headers();
      $this-&gt;header_string = implode(PHP_EOL, $this-&gt;headers).PHP_EOL;
    }

    private function set_default_headers(){
      $this-&gt;headers[] = 'MIME-Version: 1.0';
      $this-&gt;headers[] = "From: {$this-&gt;from}";
      # We'll assume a multi-part message so that we can include an HTML and a text version of the email at the
      # very least. If there are attachments, we'll be doing the same thing.
      $this-&gt;headers[] = "Content-type: multipart/mixed; boundary=\"PHP-mixed-{$this-&gt;boundary_hash}\"";
    }

    private function prepare_base64_attachments(){
      foreach($this-&gt;base64_attachments as $attachment){

        $this-&gt;body .= "--PHP-mixed-{$this-&gt;boundary_hash}\n";
        $this-&gt;body .= "Content-Type: application/octet-stream; name=\"{$attachment['name']}\"\n";
        $this-&gt;body .= "Content-Transfer-Encoding: base64\n";
        $this-&gt;body .= "Content-Disposition: attachment\n\n";
        $this-&gt;body .= chunk_split($attachment['data']);
        $this-&gt;body .= "\n\n";
      }
      $this-&gt;body .= "--PHP-mixed-{$this-&gt;boundary_hash}--\n\n";
    }
	
    private function prepare_attachments(){
      foreach($this-&gt;attachments as $attachment){
        $file_name  = basename($attachment);

        $file_name  = $this-&gt;convert_utf8($file_name);
        $this-&gt;body .= "--PHP-mixed-{$this-&gt;boundary_hash}\n";
        $this-&gt;body .= "Content-Type: application/octet-stream; name=\"{$file_name}\"\n";
        $this-&gt;body .= "Content-Transfer-Encoding: base64\n";
        $this-&gt;body .= "Content-Disposition: attachment\n\n";
        $this-&gt;body .= chunk_split(base64_encode(file_get_contents($attachment)));
        $this-&gt;body .= "\n\n";
      }
      $this-&gt;body .= "--PHP-mixed-{$this-&gt;boundary_hash}--\n\n";
    }
	

    private function prepare_text(){
      $this-&gt;body .= "--PHP-alt-{$this-&gt;boundary_hash}\n";
      $this-&gt;body .= "Content-Type: text/plain; charset=\"utf-8\"\n";
      $this-&gt;body .= "Content-Transfer-Encoding: 8bit\n\n";
      $this-&gt;body .= $this-&gt;text_content."\n\n";
    }

    private function prepare_html(){
      $this-&gt;body .= "--PHP-alt-{$this-&gt;boundary_hash}\n";
      $this-&gt;body .= "Content-Type: text/html; charset=\"utf-8\"\n";
      $this-&gt;body .= "Content-Transfer-Encoding: 8bit\n\n";
      $this-&gt;body .= $this-&gt;html_content."\n\n";
    }
    //convert to  utf8 
    private function convert_utf8($subject){
        return '=?UTF-8?B?'.base64_encode($subject).'?=';
    }

  }</pre></body></html>