registry =& $registry; } // ################################################################### /** * (PHP 4) Constructor */ function Mail(&$registry) { $this->__construct($registry); } // ################################################################### /** * Sets the subject * * @access public * * @param string Subject text */ function setSubject($subject) { $this->subject = $subject; } // ################################################################### /** * Sets the body text (required) * * @access public * * @param string Body text */ function setBodyText($body) { $this->bodytext = $body; } // ################################################################### /** * Sets the HTML body (optional) * * @access public * * @param string Body HTML */ function setBodyHtml($body) { $this->bodyhtml = $body; } // ################################################################### /** * Sets the from address * * @access public * * @param string Sending email address */ function setFromAddress($address) { $this->from = $address; } // ################################################################### /** * Sets the from display name * * @access public * * @param string From name */ function setFromName($name) { $this->fromname = $name; } // ################################################################### /** * Sets any additional headers * * @access public * * @param string Additional headers separated by a \n */ function setHeaders($headers) { $this->headers = $headers; } // ################################################################### /** * Sets the character set to send the email in * * @access public * * @param string Charset */ function setCharset($charset) { $this->charset = $charset; } // ################################################################### /** * Sends an email to the specified address with the specified * sender, subject, and body. * * @access public * * @param string Email address to send to * @param string Name of the recipient * @param bool Send an HTML multipart (if HTML body specified)? * * @return bool Status of the message */ function send($address, $name = null, $sendhtml = false) { if (empty($address)) { trigger_error('You need to specify an email address', E_USER_ERROR); return false; } // make sure we have a mailer // TODO - add support for SMTP if (!@ini_get('sendmail_path')) { $this->registry->debug("email: no sendmail -> not sending"); return false; } // sort out the to addresses $address = $this->_fetch_first_line($address); $address = trim($this->registry->unsanitize($address)); $name = $this->_fetch_first_line($name); $name = trim($this->registry->unsanitize($name)); $name = $this->_encodeHeaderValue($name); $tostring = ($name == null ? $address : "\"$name\" <$address>"); // sanitize the from field $from = $this->_fetch_first_line($this->from); $from = trim($this->registry->unsanitize($from)); // sanitize the from name $fromname = $this->_fetch_first_line($this->fromname); $fromname = ($fromname == '' ? $from : trim($this->registry->unsanitize($fromname))); $fromname = $this->_encodeHeaderValue($this->fromname); // sanitize the subject $subject = $this->_fetch_first_line($this->subject); $subject = trim($this->registry->unsanitize($subject)); $subject = $this->_encodeHeaderValue($subject); // sanitize the body $bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim); $bodytext = trim($this->registry->unsanitize($bodytext, true)); // attach additional headers $headers = $this->registry->modules['functions']->convert_line_breaks($this->headers, $this->delim); $headers .= ((!preg_match("#{$this->delim}$#", $headers) AND $headers != '') ? "\n" : '') . "From: \"{$fromname}\" <{$from}>" . $this->delim; $headers .= "Return-Path: {$from}" . $this->delim; $headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim; $headers .= "MIME-Version: 1.0" . $this->delim; // see if we need to use mime/multipart if ($sendhtml AND !empty($bodyhtml) == true) { $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10); $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim; $bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim); // first part of the message (plaintext) $body = "--$boundary" . $this->delim; $body .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim; $body .= "Content-Transfer-Encoding: 8bit" . $this->delim . $this->delim; $body .= $bodytext . $this->delim; // add some space between the parts $body .= $this->delim . $this->delim . $this->delim; // second part (html) $body .= "--$boundary" . $this->delim; $body .= "Content-Type: text/html; charset=\"" . $this->charset . "\"" . $this->delim; $body .= "Content-Transfer-Encoding: 8bit" . $this->delim; $body .= "Content-Disposition: inline" . $this->delim . $this->delim; $body .= $bodyhtml . $this->delim; $body .= "--$boundary--"; } else { $headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim; $body = $bodytext; } $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim; $headers = trim($headers); // attempt to send the mail! if (mail($tostring, $subject, $body, $headers, "-f {$from}")) { $this->registry->debug("email: sent to $address"); } else { $this->registry->debug("email: error sending to $address"); } } // ################################################################### /** * Fetches the first line of a string * * @access private * * @param string A string * * @return string The first line of the string */ function _fetch_first_line($string) { $string = $this->registry->modules['functions']->convert_line_breaks($string); $broken = explode("\n", $string); return $broken[0]; } // ################################################################### /** * Encodes a header value (to name, fron name, subject, etc.) according * to RFC 2047 * * @param string The text to encode * * @return string Encoded text */ function _encodeHeaderValue($text) { if (preg_match('#[^a-zA-Z0-9\+\-\*!/]#', $text) == 0) { return $text; } // perform this on non-ASCII characters; excluding _ and = because we want them to be encoded as they have // different meanings in mail messages $text = preg_replace('#([^a-zA-Z0-9\+\-\*!/])#e', '"=" . strtoupper(dechex(ord("\\1")))', $text); $text = str_replace('=20', '_' , $text); return '=?' . $this->charset . '?q?' . $text . '?='; } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>