From 9d103dcf804fcdb5c92f71e691564e1a36bc6fb5 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 26 Nov 2006 00:14:20 +0000 Subject: [PATCH] Merging the changes made to the ISSO/Mail framework on the 2.1.x branch back to trunk --- mail.php | 223 +++++++++++++++++++++++++++---------------------------- 1 file changed, 108 insertions(+), 115 deletions(-) diff --git a/mail.php b/mail.php index 84ff139..638e853 100644 --- a/mail.php +++ b/mail.php @@ -1,7 +1,7 @@ name) - * @var array - */ - private $to = array(); + var $registry = null; /** * The subject of the message * @var string + * @access private */ - private $subject = ''; + var $subject = ''; /** * Body plain-text of the message * @var string + * @access private */ - private $bodytext = ''; + var $bodytext = ''; /** * HTML multi-part body of the message * @var string + * @access private */ - private $bodyhtml = ''; + var $bodyhtml = ''; /** * The message sender's email address * @var string + * @access private */ - private $from = ''; + var $from = ''; /** * The message sender's display name * @var string + * @access private */ - private $fromname = ''; + var $fromname = ''; /** * Additional message headers * @var string + * @access private */ - private $headers = ''; + var $headers = ''; /** * Whether to send the message as HTML or plain-text * @var bool + * @access private */ - private $sendhtml = false; + var $sendhtml = false; /** * The new line delimiter used in the message * @var string + * @access private */ - private $delim = "\n"; + var $delim = "\n"; /** * Character set used to send messages with * @var string + * @access public */ - public $charset = 'utf-8'; // should we be using iso-8859-1 ? + var $charset = 'utf-8'; // should we be using iso-8859-1 ? + // ################################################################### /** - * Fields array that is used in this module - * @var array + * Constructor */ - private $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) - ); + function __construct(&$registry) + { + $this->registry =& $registry; + } // ################################################################### /** - * Constructor + * (PHP 4) Constructor */ - public function __construct(&$registry) + function Mail(&$registry) { - $this->registry =& $registry; + $this->__construct($registry); } // ################################################################### /** - * Sets an ISSO field + * Sets the subject * - * @param string Field name - * @param mixed Value of the field + * @access public + * + * @param string Subject text */ - public function set($name, $value) + function setSubject($subject) { - if (empty($value) AND $this->fields["$name"][0] == REQ_YES) - { - trigger_error('Field ' . $name . ' cannot be empty'); - } - - $this->registry->do_set($name, $value, 'mail'); + $this->subject = $subject; } // ################################################################### /** - * Gets an ISSO field + * Sets the body text (required) * - * @param string Field name + * @access public * - * @return mixed Value of the field + * @param string Body text */ - public function get($fieldname) + function setBodyText($body) { - return $this->registry->do_get($fieldname, 'mail'); + $this->bodytext = $body; } // ################################################################### /** - * Adds a to address + * Sets the HTML body (optional) + * + * @access public * - * @param string Name to send to - * @param string Email address + * @param string Body HTML */ - public function to_add($name, $address) + function setBodyHtml($body) { - if (isset($this->to["$address"]) AND $name !== null) - { - return; - } - - if ($this->registry->modules['functions']->is_valid_email($address)) - { - $this->to["$address"] = $name; - } + $this->bodyhtml = $body; } // ################################################################### /** - * Removes a to address by email; if FALSE, it will clear the array + * Sets the from address * - * @param string Email address to remove, or FALSE to clear array + * @access public + * + * @param string Sending email address */ - public function to_remove($address) + function setFromAddress($address) { - if ($address === false) - { - $this->to = array(); - } - else - { - unset($this->to["$address"]); - } + $this->from = $address; } // ################################################################### /** - * Returns the list of "to" addresses + * Sets the from display name * - * @return array List of to-addresses + * @access public + * + * @param string From name */ - public function to_fetch() - { - return $this->to; + 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. * - * @param bool Clear the "to" list after sending? + * @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 */ - public function send($cleartos = false) + function send($address, $name = null, $sendhtml = false) { - // check the required stuff - $this->registry->check_isso_fields(get_class($this)); - if (sizeof($this->to) < 1) + if (empty($address)) { - trigger_error('You need at least one email address to send to'); + trigger_error('You need to specify an email address', E_USER_ERROR); return false; } // make sure we have a mailer - // #*# add support for SMTP + // TODO - add support for SMTP if (!@ini_get('sendmail_path')) { $this->registry->debug("email: no sendmail -> not sending"); @@ -238,23 +249,11 @@ class Mail } // 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>"; - } - } + $address = $this->_fetch_first_line($address); + $address = trim($this->registry->unsanitize($address)); + $name = $this->_fetch_first_line($name); + $name = trim($this->registry->unsanitize($name)); + $tostring = ($name == null ? $address : "\"$name\" <$address>"); // sanitize the from field $this->from = $this->_fetch_first_line($this->from); @@ -280,7 +279,7 @@ class Mail $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) + if ($sendhtml AND !empty($this->bodyhtml) == true) { $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10); $this->headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim; @@ -314,21 +313,13 @@ class Mail $this->headers = trim($this->headers); // attempt to send the mail! - foreach ($tolist AS $address) + if (mail($tostring, $this->subject, $body, $this->headers, "-f {$this->from}")) { - 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"); - } + $this->registry->debug("email: sent to $address"); } - - if ($cleartos) + else { - $this->to = array(); + $this->registry->debug("email: error sending to $address"); } } @@ -336,11 +327,13 @@ class Mail /** * Fetches the first line of a string * + * @access private + * * @param string A string * * @return string The first line of the string */ - private function _fetch_first_line($string) + function _fetch_first_line($string) { $string = $this->registry->modules['functions']->convert_line_breaks($string); $broken = explode("\n", $string); -- 2.22.5