Changeset 136 for trunk/lib/Email.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Email.inc.php
r119 r136 8 8 * @author Quinn Comendant <quinn@strangecode.com> 9 9 * @version 1.0 10 ------------------------------------------------------------------------------------- 10 * 11 11 12 // Example. 12 13 $email = new Email(array( 13 14 'to' => array($frm['email'], 'q@lovemachine.local'), 14 'from' => sprintf('%s <%s>', App::getParam('site_name'), App::getParam('site_email')),15 'from' => sprintf('%s <%s>', $app->getParam('site_name'), $app->getParam('site_email')), 15 16 'subject' => 'Your account has been activated', 16 17 )); … … 18 19 // $email->setString('Or you can pass your message body as a string, also with {VARIABLES}.'); 19 20 $email->replace(array( 20 'site_name' => App::getParam('site_name'),21 'site_url' => App::getParam('site_url'),21 'site_name' => $app->getParam('site_name'), 22 'site_url' => $app->getParam('site_url'), 22 23 'username' => $frm['username'], 23 24 'password' => $frm['password1'], 24 25 )); 25 26 if ($email->send()) { 26 App::raiseMsg(sprintf(_("A confirmation email has been sent to %s."), $frm['email']), MSG_SUCCESS, __FILE__, __LINE__);27 $app->raiseMsg(sprintf(_("A confirmation email has been sent to %s."), $frm['email']), MSG_SUCCESS, __FILE__, __LINE__); 27 28 } else { 28 App::logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_NOTICE, __FILE__, __LINE__);29 $app->logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_NOTICE, __FILE__, __LINE__); 29 30 } 30 ------------------------------------------------------------------------------------- 31 32 * 31 33 */ 32 34 class Email { … … 88 90 function setParam($params) 89 91 { 92 $app =& App::getInstance(); 93 90 94 if (isset($params) && is_array($params)) { 91 95 // Enforce valid email addresses. … … 100 104 $this->_params = array_merge($this->_params, $params); 101 105 } else { 102 App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);106 $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); 103 107 } 104 108 } … … 113 117 function getParam($param) 114 118 { 119 $app =& App::getInstance(); 120 115 121 if (isset($this->_params[$param])) { 116 122 return $this->_params[$param]; 117 123 } else { 118 App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);124 $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__); 119 125 return null; 120 126 } … … 131 137 function setTemplate($template) 132 138 { 139 $app =& App::getInstance(); 140 133 141 // Load file, using include_path. 134 142 if (!$this->_template = file_get_contents($template, true)) { 135 App::logMsg(sprintf('Email template file does not exist: %s', $template), LOG_ERR, __FILE__, __LINE__);143 $app->logMsg(sprintf('Email template file does not exist: %s', $template), LOG_ERR, __FILE__, __LINE__); 136 144 $this->_template = null; 137 145 $this->_template_replaced = null; … … 153 161 function setString($string) 154 162 { 163 $app =& App::getInstance(); 164 155 165 // Load file, using include_path. 156 166 if ('' == trim($string)) { 157 App::logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__);167 $app->logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__); 158 168 $this->_template_replaced = null; 159 169 return false; … … 176 186 function replace($replacements) 177 187 { 188 $app =& App::getInstance(); 189 178 190 // Ensure template exists. 179 191 if (!isset($this->_template)) { 180 App::logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__);192 $app->logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__); 181 193 return false; 182 194 } … … 184 196 // Ensure replacements argument is an array. 185 197 if (!is_array($replacements)) { 186 App::logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__);198 $app->logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__); 187 199 return false; 188 200 } … … 211 223 function send($to=null, $from=null, $subject=null, $headers=null) 212 224 { 225 $app =& App::getInstance(); 226 213 227 // Use arguments if provided. 214 228 if (isset($to)) { … … 227 241 // Ensure required values exist. 228 242 if (!isset($this->_params['subject'])) { 229 App::logMsg(sprintf('Cannot send email to %s. SUBJECT not defined.', $this->_params['to']), LOG_ERR, __FILE__, __LINE__);243 $app->logMsg(sprintf('Cannot send email to %s. SUBJECT not defined.', $this->_params['to']), LOG_ERR, __FILE__, __LINE__); 230 244 return false; 231 245 } else if (!isset($this->_template)) { 232 App::logMsg(sprintf('Cannot send email: "%s". Template not set.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);246 $app->logMsg(sprintf('Cannot send email: "%s". Template not set.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__); 233 247 return false; 234 248 } else if (!isset($this->_params['to'])) { 235 App::logMsg(sprintf('Cannot send email: "%s". TO not defined.', $this->_params['subject']), LOG_NOTICE, __FILE__, __LINE__);249 $app->logMsg(sprintf('Cannot send email: "%s". TO not defined.', $this->_params['subject']), LOG_NOTICE, __FILE__, __LINE__); 236 250 return false; 237 251 } else if (!isset($this->_params['from'])) { 238 App::logMsg(sprintf('Cannot send email: "%s". FROM not defined.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);252 $app->logMsg(sprintf('Cannot send email: "%s". FROM not defined.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__); 239 253 return false; 240 254 } … … 245 259 // Ensure all placeholders have been replaced. Find anything with {...} characters. 246 260 if (preg_match('/({[^}]+})/', $final_body, $unreplaced_match)) { 247 App::logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match[1]) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__);261 $app->logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match[1]) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__); 248 262 return false; 249 263 } … … 273 287 $full_mail_content = join("\n", array($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header)); 274 288 if (preg_match("/(Content-Type:|MIME-Version:|Content-Transfer-Encoding:|[\n\r]Bcc:|[\n\r]Cc:)/i", $full_mail_content)) { 275 App::logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__);289 $app->logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__); 276 290 sleep(3); 277 291 return false; … … 280 294 // Ensure message was successfully accepted for delivery. 281 295 if (mail($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header)) { 282 App::logMsg(sprintf('Email successfully sent to %s', $final_to), LOG_DEBUG, __FILE__, __LINE__);296 $app->logMsg(sprintf('Email successfully sent to %s', $final_to), LOG_DEBUG, __FILE__, __LINE__); 283 297 return true; 284 298 } else { 285 App::logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $final_to, $this->_params['subject'], str_replace("\r\n", '\r\n', $final_headers), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__);299 $app->logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $final_to, $this->_params['subject'], str_replace("\r\n", '\r\n', $final_headers), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__); 286 300 return false; 287 301 } … … 305 319 function validEmail($email) 306 320 { 321 $app =& App::getInstance(); 322 307 323 // If an array, check values recursively. 308 324 if (is_array($email)) { … … 318 334 return true; 319 335 } else { 320 App::logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__);336 $app->logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__); 321 337 return false; 322 338 }
Note: See TracChangeset
for help on using the changeset viewer.