name) * @var array * @access private */ var $to = array(); /** * The subject of the message * @var string * @access private */ var $subject = ''; /** * Body plain-text of the message * @var string * @access private */ var $bodytext = ''; /** * HTML multi-part body of the message * @var string * @access private */ var $bodyhtml = ''; /** * The message sender's email address * @var string * @access private */ var $from = ''; /** * The message sender's display name * @var string * @access private */ var $fromname = ''; /** * Additional message headers * @var string * @access private */ var $headers = ''; /** * Whether to send the message as HTML or plain-text * @var bool * @access private */ var $sendhtml = false; /** * The new line delimiter used in the message * @var string * @access private */ var $delim = "\n"; /** * Character set used to send messages with * @var string * @access public */ var $charset = 'utf-8'; // should we be using iso-8859-1 ? /** * Fields array that is used in this module * @var array * @access private */ var $fields = array( 'subject' => array(REQ_YES, null, false), 'bodytext' => array(REQ_YES, null, false), 'bodyhtml' => array(REQ_NO, null, false), 'from' => array(REQ_YES, null, false), 'fromname' => array(REQ_NO, null, false), 'headers' => array(REQ_NO, null, false), 'sendhtml' => array(REQ_NO, null, false), 'delim' => array(REQ_YES, null, true), 'charset' => array(REQ_YES, null, true) ); // ################################################################### /** * Constructor */ function __construct(&$registry) { $this->registry =& $registry; } // ################################################################### /** * (PHP 4) Constructor */ function Mail(&$registry) { $this->__construct($registry); } // ################################################################### /** * Sets an ISSO field * * @access public * * @param string Field name * @param mixed Value of the field */ function set($name, $value) { if (empty($value) AND $this->fields["$name"][0] == REQ_YES) { trigger_error('Field ' . $name . ' cannot be empty', E_USER_ERROR); } $this->registry->do_set($name, $value, 'mail'); } // ################################################################### /** * Gets an ISSO field * * @access public * * @param string Field name * * @return mixed Value of the field */ function get($fieldname) { return $this->registry->do_get($fieldname, 'mail'); } // ################################################################### /** * Adds a to address * * @access public * * @param string Name to send to * @param string Email address */ function to_add($name, $address) { if (isset($this->to["$address"]) AND $name !== null) { return; } if ($this->registry->modules['functions']->is_valid_email($address)) { $this->to["$address"] = $name; } } // ################################################################### /** * Removes a to address by email; if FALSE, it will clear the array * * @access public * * @param string Email address to remove, or FALSE to clear array */ function to_remove($address) { if ($address === false) { $this->to = array(); } else { unset($this->to["$address"]); } } // ################################################################### /** * Returns the list of "to" addresses * * @access public * * @return array List of to-addresses */ function to_fetch() { return $this->to; } // ################################################################### /** * Sends an email to the specified address with the specified * sender, subject, and body. * * @access public * * @param bool Clear the "to" list after sending? * * @return bool Status of the message */ function send($cleartos = false) { // check the required stuff $this->registry->check_isso_fields(get_class($this)); if (sizeof($this->to) < 1) { trigger_error('You need at least one email address to send to', E_USER_ERROR); return false; } // make sure we have a mailer // #*# add support for SMTP if (!@ini_get('sendmail_path')) { $this->registry->debug("email: no sendmail -> not sending"); return false; } // sort out the to addresses $tolist = array(); foreach ($this->to AS $address => $name) { $address = $this->_fetch_first_line($address); $address = trim($this->registry->unsanitize($address)); $name = $this->_fetch_first_line($name); $name = trim($this->registry->unsanitize($name)); if ($name == null) { $tolist[] = $address; } else { $tolist[] = "\"$name\" <$address>"; } } // sanitize the from field $this->from = $this->_fetch_first_line($this->from); $this->from = trim($this->registry->unsanitize($this->from)); // sanitize the from name $this->fromname = $this->_fetch_first_line($this->fromname); $this->fromname = ($this->fromname == '' ? $this->from : trim($this->registry->unsanitize($this->fromname))); // sanitize the subject $this->subject = $this->_fetch_first_line($this->subject); $this->subject = trim($this->registry->unsanitize($this->subject)); // sanitize the body $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim); $this->bodytext = trim($this->registry->unsanitize($this->bodytext, true)); // attach additional headers $this->headers = $this->registry->modules['functions']->convert_line_breaks($this->headers, $this->delim); $this->headers .= ((!preg_match("#{$this->delim}$#", $this->headers) AND $this->headers != '') ? "\n" : '') . "From: \"{$this->fromname}\" <{$this->from}>" . $this->delim; $this->headers .= "Return-Path: {$this->from}" . $this->delim; $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim; $this->headers .= "MIME-Version: 1.0" . $this->delim; // see if we need to use mime/multipart if ($this->sendhtml AND $this->fields['bodyhtml'][2] == true) { $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10); $this->headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim; $this->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 .= $this->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 .= $this->bodyhtml . $this->delim; $body .= "--$boundary--"; } else { $this->headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim; $body = $this->bodytext; } $this->headers .= "Content-Transfer-Encoding: 8bit" . $this->delim; $this->headers = trim($this->headers); // attempt to send the mail! foreach ($tolist AS $address) { if (mail($address, $this->subject, $body, $this->headers, "-f {$this->from}")) { $this->registry->debug("email: sent to $address"); } else { $this->registry->debug("email: error sending to $address"); } } if ($cleartos) { $this->to = array(); } } // ################################################################### /** * 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]; } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>