Changeset 24 for trunk/lib/Email.inc.php
- Timestamp:
- Dec 1, 2005 9:09:45 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Email.inc.php
r23 r24 8 8 * @author Quinn Comendant <quinn@strangecode.com> 9 9 * @version 1.0 10 11 10 ------------------------------------------------------------------------------------- 12 11 // Example. … … 14 13 'to' => array($frm['email'], 'q@lovemachine.local'), 15 14 'from' => sprintf('%s <%s>', App::getParam('site_name'), App::getParam('site_email')), 16 'subject' => 'Your Golbonaccount has been activated',15 'subject' => 'Your account has been activated', 17 16 )); 18 17 $email->setTemplate('email_registration_confirm.ihtml'); … … 22 21 'username' => $frm['username'], 23 22 'password' => $frm['password1'], 24 'inviting_member_business_name' => $frm['inviting_member_business_name'],25 23 )); 26 24 if ($email->send()) { … … 28 26 } else { 29 27 App::logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_DEBUG, __FILE__, __LINE__); 30 App::raiseMsg(sprintf('We tried sending you a confirmation email to address %s but there was an error. After logging in, please visit the "My Account" page to confirm that this address is correct.', $frm['email']), MSG_NOTICE, __FILE__, __LINE__);31 28 } 32 29 ------------------------------------------------------------------------------------- … … 34 31 class Email { 35 32 33 // Default parameters, to be overwritten by setParam() and read with getParam() 36 34 var $_params = array( 37 35 'to' => null, … … 40 38 'regex' => null 41 39 ); 40 41 // String that contains the email body. 42 42 var $_template; 43 44 // String that contains the email body after replacements. 43 45 var $_template_replaced; 44 var $regex;45 46 46 47 /** … … 54 55 function Email($params=null) 55 56 { 57 // The regex used in validEmail(). Set here instead of in the default _params above so we can use the concatination . dot. 58 // This matches an email address as complex as: 59 // Bob Smith <bob&smith's/dep=sales!@smith-wick.ca.us> (Sales department) 60 // ...and something as simple as: 61 // x@x.com 56 62 $this->setParam(array('regex' => '/^(?:[^,@]*\s+|[^,@]*(<)|)' // Display name 57 63 . '((?:[^.<>\s@\",\[\]]+[^<>\s@\",\[\]])*[^.<>\s@\",\[\]]+)' // Local-part … … 79 85 { 80 86 if (isset($params) && is_array($params)) { 81 82 87 // Enforce valid email addresses. 83 88 if (isset($params['to']) && !$this->validEmail($params['to'])) { … … 129 134 return false; 130 135 } 136 // This could be a new template, so reset the _template_replaced. 131 137 $this->_template_replaced = null; 132 138 return true; … … 150 156 } else { 151 157 $_template = $string; 158 // This could be a new template, so reset the _template_replaced. 152 159 $this->_template_replaced = null; 153 160 return true; … … 165 172 function replace($replacements) 166 173 { 174 // Ensure template exists. 167 175 if (!isset($this->_template)) { 168 176 App::logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__); 169 177 return false; 170 178 } 171 179 180 // Ensure replacements argument is an array. 172 181 if (!is_array($replacements)) { 173 182 App::logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__); … … 181 190 // Replace values. 182 191 $replace = array_values($replacements); 183 192 193 // Search and replace all values at once. 184 194 $this->_template_replaced = preg_replace($search, $replace, $this->_template); 185 195 } … … 197 207 function send($to=null, $from=null, $subject=null) 198 208 { 209 // Use arguments if provided. 199 210 if (isset($to)) { 200 211 $this->setParam(array('to' => $to)); … … 206 217 $this->setParam(array('subject' => $subject)); 207 218 } 208 219 220 221 // Ensure required values exist. 209 222 if (!isset($this->_template)) { 210 223 App::logMsg(sprintf('Cannot send email. Template not set.', null), LOG_ERR, __FILE__, __LINE__); … … 221 234 } 222 235 236 // Wrap email text body, using _template_replaced if replacements have been used, or just a fresh _template if not. 223 237 $final_body = wordwrap(isset($this->_template_replaced) ? $this->_template_replaced : $this->_template); 224 // Test that all placeholders have been replaced. 238 239 // Ensure all placeholders have been replaced. Find anything with {...} characters. 225 240 if (preg_match('/({[^}]+})/', $final_body, $unreplaced_match)) { 226 241 App::logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__); … … 228 243 } 229 244 245 // Final "to" header can have multiple addresses if in an array. 230 246 $final_to = is_array($this->_params['to']) ? join(', ', $this->_params['to']) : $this->_params['to']; 231 $final_from = sprintf("From: %s\r\n", $this->_params['from']); 247 248 // From headers are custom headers. 249 $headers = sprintf("From: %s\r\n", $this->_params['from']); 250 251 // This is the address where delivery problems are sent to. We must strip off everything except the local@domain part. 232 252 $envelope_sender_header = sprintf('-f %s', preg_replace('/^.*<?([^\s@\[\]<>()]+\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5})>?$/iU', '$1', $this->_params['from'])); 233 if (!mail($final_to, $this->_params['subject'], $final_body, $final_from, $envelope_sender_header)) { 234 App::logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $this->_params['to'], $this->_params['subject'], str_replace("\r\n", '', $final_from), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__); 253 254 // Ensure message was successfully accepted for delivery. 255 if (!mail($final_to, $this->_params['subject'], $final_body, $headers, $envelope_sender_header)) { 256 App::logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $this->_params['to'], $this->_params['subject'], str_replace("\r\n", '', $headers), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__); 235 257 return false; 236 258 } … … 256 278 function validEmail($email) 257 279 { 280 // If an array, check values recursively. 258 281 if (is_array($email)) { 259 282 foreach ($email as $e) { … … 264 287 return true; 265 288 } else { 266 if (!preg_match($this->getParam('regex'), $email)) { 289 // To be valid email address must match regex and fit within the lenth constraints. 290 if (preg_match($this->getParam('regex'), $email, $e_parts) && strlen($e_parts[2]) < 64 && strlen($e_parts[3]) < 255) { 291 return true; 292 } else { 267 293 App::logMsg(sprintf('Invalid email: %s', $email), LOG_DEBUG, __FILE__, __LINE__); 268 294 return false; 269 295 } 270 return true;271 296 } 272 297 }
Note: See TracChangeset
for help on using the changeset viewer.