<?php

$module["mail"]["name"]="Mail Class";
$module["mail"]["ver"]="0.7.2";

class mail {
  var $errstr;
  var $headers;
  var $textbody;
  var $htmlbody;
  var $attachments;
  var $boundary;
  var $semi_rand;
 
  // Default constructor, sets up default header and boundary.
  function __construct() {
    $this->attachments = array();
    $this->semi_rand = md5(time());
    $this->boundary = '==becast_'.$this->semi_rand ;
    $this->headers = array(
         'From' => 'BeCast WebEngine Mail Class <noreply@becast.at>',
         'MIME-Version' => '1.0',
         'Content-Type' => "multipart/mixed; boundary=\"".$this->boundary."\""
    );
    $this->bodytext("Body");
  }
 
  // For debugging purposes you can display the body you are about
  // to send.
  function get_body() {
    $retval = $textbody;
    $retval .= $htmlbody;
    foreach($this->attachments as $tblck)
      $retval .= $tblck;
 
    return $retval;
  }
 
  // Convert the values in the header array into the correct format.
  function get_header() {
    $retval = "";
    foreach($this->headers as $key => $value)
      $retval .= "$key: $value\n";
 
    return $retval;
  }
 
  // Add your own header entry or modify a header.
  function set_header($name, $value) {
    $this->headers[$name] = $value;
  }
 
  // Attach a file to the message.
  function attachfile($file, $type = "application/octetstream")  {
    if(!($fd = fopen($file, "r"))) {
      $this->errstr = "Error opening ".$file." for reading.";
      return 0;
    }
    $_buf = fread($fd, filesize($file));
    fclose($fd);
 
    $fname = $file;
    for($x = strlen($file); $x > 0; $x--)
      if($file[$x] == "/")
        $fname = substr($file, $x, strlen($file) - $x);
 
    // Convert to base64 becuase mail attachments are not binary safe.
    $_buf = chunk_split(base64_encode($_buf));
 
    $this->attachments[$file] = "--" . $this->boundary . "\n";
    $this->attachments[$file] .= "Content-Type: ".$type."; name=\"".$fname."\"\n";
    $this->attachments[$file] .= "Content-Transfer-Encoding: base64\n";
    $this->attachments[$file] .= "Content-Disposition: attachment; " .
                                     "filename=\"".$fname."\"\n\n";
    $this->attachments[$file] .= $_buf;
 
    return 1;
  }
 
  function bodytext($text) {
    $this->textbody = "--" . $this->boundary . "\n";
    $this->textbody .= "Content-Type: text/plain; charset=utf-8\n";
    $this->textbody .= "Content-Transfer-Encoding: base64\n\n";
    $this->textbody .= base64_encode($text);
  }
 
  function htmltext($text) {
    $this->htmlbody = "\n--" . $this->boundary . "\n";
    $this->htmlbody .= "Content-Type: text/html; charset=utf-8\n";
    $this->htmlbody .= "Content-Transfer-Encoding: base64\n\n";
    $this->htmlbody .= base64_encode($text);
  }
 
  function clear_bodytext() { $this->textbody = ""; }
  function clear_htmltext() { $this->htmlbody = ""; }
  function get_error() { return $this->errstr; }
 
  // Send the headers and body using php's built in mail.
  function sendmail($to = "root@localhost", $subject = "kein Betreff") {
    if(isset($this->textbody)) $_body .= $this->textbody;
    if(isset($this->htmlbody)) $_body .= $this->htmlbody;
 
    foreach($this->attachments as $tblck)
      $_body .= $tblck;
 
    $_body .= "\n--$this->boundary--";
 
    mail($to, '=?utf-8?B?'.base64_encode($subject).'?=', $_body, $this->get_header());
  }
}
?>
