From 041b1619bbb8b1387f5e31b236b7ab5edd168303 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 26 Nov 2006 00:41:45 +0000 Subject: [PATCH] Updated the mail sending framework --- mail.php | 196 +++++++++++++++++++++++-------------------------------- 1 file changed, 80 insertions(+), 116 deletions(-) diff --git a/mail.php b/mail.php index 638e853..993abbc 100644 --- a/mail.php +++ b/mail.php @@ -20,12 +20,13 @@ \*=====================================================================*/ /** -* Mail Sender -* mail.php +* Mail Sender (mail.php) * * @package ISSO */ +require_once('ISSO/Functions.php'); + /** * Mail Sender * @@ -38,105 +39,65 @@ * @package ISSO * */ -class Mail +class BSMail { - /** - * Framework registry object - * @var object - * @access private - */ - var $registry = null; - /** * The subject of the message * @var string - * @access private */ - var $subject = ''; + private $subject = ''; /** * Body plain-text of the message * @var string - * @access private */ - var $bodytext = ''; + private $bodyText = ''; /** * HTML multi-part body of the message * @var string - * @access private */ - var $bodyhtml = ''; + private $bodyHtml = ''; /** * The message sender's email address * @var string - * @access private */ - var $from = ''; + private $from = ''; /** * The message sender's display name * @var string - * @access private */ - var $fromname = ''; + private $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; - + private $headers = ''; + /** * The new line delimiter used in the message * @var string * @access private */ - var $delim = "\n"; + private $delim = "\n"; /** * Character set used to send messages with * @var string * @access public */ - var $charset = 'utf-8'; // should we be using iso-8859-1 ? - - // ################################################################### - /** - * Constructor - */ - function __construct(&$registry) - { - $this->registry =& $registry; - } - - // ################################################################### - /** - * (PHP 4) Constructor - */ - function Mail(&$registry) - { - $this->__construct($registry); - } - + private $charset = 'utf-8'; // should we be using iso-8859-1 ? + // ################################################################### /** * Sets the subject * - * @access public - * * @param string Subject text */ - function setSubject($subject) + public function setSubject($subject) { $this->subject = $subject; } @@ -145,37 +106,31 @@ class Mail /** * Sets the body text (required) * - * @access public - * * @param string Body text */ - function setBodyText($body) + public function setBodyText($body) { - $this->bodytext = $body; + $this->bodyText = $body; } // ################################################################### /** * Sets the HTML body (optional) * - * @access public - * * @param string Body HTML */ - function setBodyHtml($body) + public function setBodyHtml($body) { - $this->bodyhtml = $body; + $this->bodyHtml = $body; } // ################################################################### /** * Sets the from address * - * @access public - * * @param string Sending email address */ - function setFromAddress($address) + public function setFromAddress($address) { $this->from = $address; } @@ -184,24 +139,20 @@ class Mail /** * Sets the from display name * - * @access public - * * @param string From name */ - function setFromName($name) + public function setFromName($name) { - $this->fromname = $name; + $this->fromName = $name; } // ################################################################### /** * Sets any additional headers * - * @access public - * * @param string Additional headers separated by a \n */ - function setHeaders($headers) + public function setHeaders($headers) { $this->headers = $headers; } @@ -210,11 +161,9 @@ class Mail /** * Sets the character set to send the email in * - * @access public - * * @param string Charset */ - function setCharset($charset) + public function setCharset($charset) { $this->charset = $charset; } @@ -224,73 +173,90 @@ class Mail * 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) + public function send($address, $name = null, $sendHtml = false) { if (empty($address)) { - trigger_error('You need to specify an email address', E_USER_ERROR); - return false; + trigger_error('You need to specify an email address'); + } + + // load the input sanitizer + $input = BSRegister::GetType('BSInput'); + if ($input == null) + { + BSRegister::Debug('ISSO/Input not loaded, so manually doing so'); + $input = BSRegister::LoadModule('Input'); } // make sure we have a mailer // TODO - add support for SMTP if (!@ini_get('sendmail_path')) { - $this->registry->debug("email: no sendmail -> not sending"); + BSRegister::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)); + $address = $this->_fetchFirstLine($address); + $address = trim($input->unsanitize($address)); + $name = $this->_fetchFirstLine($name); + $name = trim($input->unsanitize($name)); $tostring = ($name == null ? $address : "\"$name\" <$address>"); // sanitize the from field - $this->from = $this->_fetch_first_line($this->from); - $this->from = trim($this->registry->unsanitize($this->from)); + $this->from = $this->_fetchFirstLine($this->from); + $this->from = trim($input->unsanitize($this->from)); + if (empty($this->from)) + { + trigger_error('You need to specify a from email address'); + } // sanitize the from name - $this->fromname = $this->_fetch_first_line($this->fromname); - $this->fromname = ($this->fromname == '' ? $this->from : trim($this->registry->unsanitize($this->fromname))); + $this->fromName = $this->_fetchFirstLine($this->fromName); + $this->fromName = ($this->fromName == '' ? $this->from : trim($input->unsanitize($this->fromName))); // sanitize the subject - $this->subject = $this->_fetch_first_line($this->subject); - $this->subject = trim($this->registry->unsanitize($this->subject)); + $this->subject = $this->_fetchFirstLine($this->subject); + $this->subject = trim($input->unsanitize($this->subject)); + if (empty($this->subject)) + { + trigger_error('You need to specify a subject for the message'); + } // 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)); + $this->bodyText = BSFunctions::ConvertLineBreaks($this->bodyText, $this->delim); + $this->bodyText = trim($input->unsanitize($this->bodyText, true)); + if (empty($this->bodyText)) + { + trigger_error('You need to specify body text before sending the message'); + } // 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; + $headers = BSFunctions::ConvertLineBreaks($this->headers, $this->delim); + $headers .= ((!preg_match("#{$this->delim}$#", $headers) AND $headers != '') ? "\n" : '') . "From: \"{$this->fromName}\" <{$this->from}>" . $this->delim; + $headers .= "Return-Path: {$this->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($this->bodyhtml) == 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; + $boundary = 'ISSO-MULTIPART-' . BSFunctions::Rand(10); + $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim; - $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim); + $this->bodyHtml = BSFunctions::ConvertLineBreaks($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; + $body .= $this->bodyText . $this->delim; // add some space between the parts $body .= $this->delim . $this->delim . $this->delim; @@ -300,26 +266,26 @@ class Mail $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 .= $this->bodyHtml . $this->delim; $body .= "--$boundary--"; } else { - $this->headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim; - $body = $this->bodytext; + $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); + $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim; + $headers = trim($headers); + var_dump( "mail($tostring, {$this->subject}, $body, $headers, \"-f {$this->from}\")"); return; // attempt to send the mail! - if (mail($tostring, $this->subject, $body, $this->headers, "-f {$this->from}")) + if (mail($tostring, $this->subject, $body, $headers, "-f {$this->from}")) { - $this->registry->debug("email: sent to $address"); + BSRegister::Debug("email: sent to $address"); } else { - $this->registry->debug("email: error sending to $address"); + BSRegister::Debug("email: error sending to $address"); } } @@ -327,15 +293,13 @@ class Mail /** * 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) + private function _fetchFirstLine($string) { - $string = $this->registry->modules['functions']->convert_line_breaks($string); + $string = BSFunctions::ConvertLineBreaks($string); $broken = explode("\n", $string); return $broken[0]; } -- 2.22.5