[23] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Email.inc.php |
---|
| 4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
| 5 | * |
---|
| 6 | * Easy email template usage. |
---|
| 7 | * |
---|
| 8 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 9 | * @version 1.0 |
---|
[135] | 10 | * |
---|
| 11 | |
---|
[23] | 12 | // Example. |
---|
| 13 | $email = new Email(array( |
---|
| 14 | 'to' => array($frm['email'], 'q@lovemachine.local'), |
---|
[127] | 15 | 'from' => sprintf('%s <%s>', $app->getParam('site_name'), $app->getParam('site_email')), |
---|
[24] | 16 | 'subject' => 'Your account has been activated', |
---|
[23] | 17 | )); |
---|
| 18 | $email->setTemplate('email_registration_confirm.ihtml'); |
---|
[41] | 19 | // $email->setString('Or you can pass your message body as a string, also with {VARIABLES}.'); |
---|
[23] | 20 | $email->replace(array( |
---|
[127] | 21 | 'site_name' => $app->getParam('site_name'), |
---|
| 22 | 'site_url' => $app->getParam('site_url'), |
---|
[23] | 23 | 'username' => $frm['username'], |
---|
| 24 | 'password' => $frm['password1'], |
---|
| 25 | )); |
---|
| 26 | if ($email->send()) { |
---|
[127] | 27 | $app->raiseMsg(sprintf(_("A confirmation email has been sent to %s."), $frm['email']), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[23] | 28 | } else { |
---|
[127] | 29 | $app->logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_NOTICE, __FILE__, __LINE__); |
---|
[23] | 30 | } |
---|
[135] | 31 | |
---|
| 32 | * |
---|
[23] | 33 | */ |
---|
| 34 | class Email { |
---|
| 35 | |
---|
[24] | 36 | // Default parameters, to be overwritten by setParam() and read with getParam() |
---|
[23] | 37 | var $_params = array( |
---|
| 38 | 'to' => null, |
---|
| 39 | 'from' => null, |
---|
| 40 | 'subject' => null, |
---|
[38] | 41 | 'headers' => null, |
---|
[23] | 42 | 'regex' => null |
---|
| 43 | ); |
---|
[42] | 44 | |
---|
[24] | 45 | // String that contains the email body. |
---|
[23] | 46 | var $_template; |
---|
[42] | 47 | |
---|
[24] | 48 | // String that contains the email body after replacements. |
---|
[23] | 49 | var $_template_replaced; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Constructor. |
---|
| 53 | * |
---|
| 54 | * @access public |
---|
| 55 | * @param array $params Array of object parameters. |
---|
| 56 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 57 | * @since 28 Nov 2005 12:59:41 |
---|
| 58 | */ |
---|
| 59 | function Email($params=null) |
---|
| 60 | { |
---|
[24] | 61 | // The regex used in validEmail(). Set here instead of in the default _params above so we can use the concatination . dot. |
---|
[42] | 62 | // This matches an email address as complex as: |
---|
[24] | 63 | // Bob Smith <bob&smith's/dep=sales!@smith-wick.ca.us> (Sales department) |
---|
| 64 | // ...and something as simple as: |
---|
| 65 | // x@x.com |
---|
[23] | 66 | $this->setParam(array('regex' => '/^(?:[^,@]*\s+|[^,@]*(<)|)' // Display name |
---|
[42] | 67 | . '((?:[^.<>\s@\",\[\]]+[^<>\s@\",\[\]])*[^.<>\s@\",\[\]]+)' // Local-part |
---|
[23] | 68 | . '@' // @ |
---|
| 69 | . '((?:(\[)|[A-Z0-9]?)' // Domain, first char |
---|
| 70 | . '(?(4)' // Domain conditional for if first domain char is [ |
---|
| 71 | . '(?:[0-9]{1,3}\.){3}[0-9]{1,3}\]' // TRUE, matches IP address |
---|
| 72 | . '|' |
---|
| 73 | . '[.-]?(?:[A-Z0-9]+[-.])*(?:[A-Z0-9]+\.)+[A-Z]{2,6}))' // FALSE, matches domain name |
---|
| 74 | . '(?(1)' // Comment conditional for if initial < exists |
---|
| 75 | . '(?:>\s*|>\s+\([^,@]+\)\s*)' // TRUE, ensure ending > |
---|
| 76 | . '|' |
---|
| 77 | . '(?:|\s*|\s+\([^,@]+\)\s*))$/i')); // FALSE ensure there is no ending > |
---|
| 78 | |
---|
[25] | 79 | if (isset($params)) { |
---|
| 80 | $this->setParam($params); |
---|
| 81 | } |
---|
[23] | 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 86 | * |
---|
| 87 | * @access public |
---|
| 88 | * @param array $params Array of parameters (key => val pairs). |
---|
| 89 | */ |
---|
| 90 | function setParam($params) |
---|
| 91 | { |
---|
[127] | 92 | $app =& App::getInstance(); |
---|
| 93 | |
---|
[23] | 94 | if (isset($params) && is_array($params)) { |
---|
| 95 | // Enforce valid email addresses. |
---|
| 96 | if (isset($params['to']) && !$this->validEmail($params['to'])) { |
---|
| 97 | $params['to'] = null; |
---|
| 98 | } |
---|
| 99 | if (isset($params['from']) && !$this->validEmail($params['from'])) { |
---|
| 100 | $params['from'] = null; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | // Merge new parameters with old overriding only those passed. |
---|
| 104 | $this->_params = array_merge($this->_params, $params); |
---|
| 105 | } else { |
---|
[127] | 106 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
[23] | 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * Return the value of a parameter, if it exists. |
---|
| 112 | * |
---|
| 113 | * @access public |
---|
| 114 | * @param string $param Which parameter to return. |
---|
| 115 | * @return mixed Configured parameter value. |
---|
| 116 | */ |
---|
| 117 | function getParam($param) |
---|
| 118 | { |
---|
[127] | 119 | $app =& App::getInstance(); |
---|
| 120 | |
---|
[23] | 121 | if (isset($this->_params[$param])) { |
---|
| 122 | return $this->_params[$param]; |
---|
| 123 | } else { |
---|
[127] | 124 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__); |
---|
[23] | 125 | return null; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * Loads template from file to generate email body. |
---|
| 131 | * |
---|
| 132 | * @access public |
---|
| 133 | * @param string $template Filename of email template. |
---|
| 134 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 135 | * @since 28 Nov 2005 12:56:23 |
---|
| 136 | */ |
---|
| 137 | function setTemplate($template) |
---|
| 138 | { |
---|
[127] | 139 | $app =& App::getInstance(); |
---|
| 140 | |
---|
[23] | 141 | // Load file, using include_path. |
---|
| 142 | if (!$this->_template = file_get_contents($template, true)) { |
---|
[127] | 143 | $app->logMsg(sprintf('Email template file does not exist: %s', $template), LOG_ERR, __FILE__, __LINE__); |
---|
[23] | 144 | $this->_template = null; |
---|
| 145 | $this->_template_replaced = null; |
---|
| 146 | return false; |
---|
| 147 | } |
---|
[24] | 148 | // This could be a new template, so reset the _template_replaced. |
---|
[23] | 149 | $this->_template_replaced = null; |
---|
| 150 | return true; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * Loads template from string to generate email body. |
---|
| 155 | * |
---|
| 156 | * @access public |
---|
| 157 | * @param string $template Filename of email template. |
---|
| 158 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 159 | * @since 28 Nov 2005 12:56:23 |
---|
| 160 | */ |
---|
| 161 | function setString($string) |
---|
| 162 | { |
---|
[127] | 163 | $app =& App::getInstance(); |
---|
| 164 | |
---|
[23] | 165 | // Load file, using include_path. |
---|
[41] | 166 | if ('' == trim($string)) { |
---|
[127] | 167 | $app->logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[23] | 168 | $this->_template_replaced = null; |
---|
| 169 | return false; |
---|
| 170 | } else { |
---|
[41] | 171 | $this->_template = $string; |
---|
[24] | 172 | // This could be a new template, so reset the _template_replaced. |
---|
[23] | 173 | $this->_template_replaced = null; |
---|
| 174 | return true; |
---|
| 175 | } |
---|
| 176 | } |
---|
[42] | 177 | |
---|
[23] | 178 | /** |
---|
| 179 | * Replace variables in template with argument data. |
---|
| 180 | * |
---|
| 181 | * @access public |
---|
| 182 | * @param array $replacements Array keys are the values to search for, array vales are the replacement values. |
---|
| 183 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 184 | * @since 28 Nov 2005 13:08:51 |
---|
| 185 | */ |
---|
| 186 | function replace($replacements) |
---|
| 187 | { |
---|
[127] | 188 | $app =& App::getInstance(); |
---|
| 189 | |
---|
[24] | 190 | // Ensure template exists. |
---|
[23] | 191 | if (!isset($this->_template)) { |
---|
[127] | 192 | $app->logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[42] | 193 | return false; |
---|
[23] | 194 | } |
---|
[42] | 195 | |
---|
[24] | 196 | // Ensure replacements argument is an array. |
---|
[23] | 197 | if (!is_array($replacements)) { |
---|
[127] | 198 | $app->logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[42] | 199 | return false; |
---|
[23] | 200 | } |
---|
[42] | 201 | |
---|
[23] | 202 | // Apply regex pattern to search elements. |
---|
| 203 | $search = array_keys($replacements); |
---|
| 204 | array_walk($search, create_function('&$v', '$v = "/{" . preg_quote($v) . "}/i";')); |
---|
| 205 | |
---|
[35] | 206 | // Replacement values. |
---|
[23] | 207 | $replace = array_values($replacements); |
---|
[42] | 208 | |
---|
[24] | 209 | // Search and replace all values at once. |
---|
[23] | 210 | $this->_template_replaced = preg_replace($search, $replace, $this->_template); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * Send email using PHP's mail() function. |
---|
| 215 | * |
---|
| 216 | * @access public |
---|
| 217 | * @param string $to |
---|
| 218 | * @param string $from |
---|
| 219 | * @param string $subject |
---|
| 220 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 221 | * @since 28 Nov 2005 12:56:09 |
---|
| 222 | */ |
---|
[38] | 223 | function send($to=null, $from=null, $subject=null, $headers=null) |
---|
[23] | 224 | { |
---|
[127] | 225 | $app =& App::getInstance(); |
---|
| 226 | |
---|
[24] | 227 | // Use arguments if provided. |
---|
[23] | 228 | if (isset($to)) { |
---|
[42] | 229 | $this->setParam(array('to' => $to)); |
---|
[23] | 230 | } |
---|
| 231 | if (isset($from)) { |
---|
[42] | 232 | $this->setParam(array('from' => $from)); |
---|
[23] | 233 | } |
---|
| 234 | if (isset($subject)) { |
---|
[42] | 235 | $this->setParam(array('subject' => $subject)); |
---|
[23] | 236 | } |
---|
[38] | 237 | if (isset($headers)) { |
---|
[42] | 238 | $this->setParam(array('headers' => $headers)); |
---|
[38] | 239 | } |
---|
[36] | 240 | |
---|
[24] | 241 | // Ensure required values exist. |
---|
[43] | 242 | if (!isset($this->_params['subject'])) { |
---|
[127] | 243 | $app->logMsg(sprintf('Cannot send email to %s. SUBJECT not defined.', $this->_params['to']), LOG_ERR, __FILE__, __LINE__); |
---|
[23] | 244 | return false; |
---|
[43] | 245 | } else if (!isset($this->_template)) { |
---|
[127] | 246 | $app->logMsg(sprintf('Cannot send email: "%s". Template not set.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__); |
---|
[43] | 247 | return false; |
---|
[23] | 248 | } else if (!isset($this->_params['to'])) { |
---|
[127] | 249 | $app->logMsg(sprintf('Cannot send email: "%s". TO not defined.', $this->_params['subject']), LOG_NOTICE, __FILE__, __LINE__); |
---|
[23] | 250 | return false; |
---|
| 251 | } else if (!isset($this->_params['from'])) { |
---|
[127] | 252 | $app->logMsg(sprintf('Cannot send email: "%s". FROM not defined.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__); |
---|
[23] | 253 | return false; |
---|
| 254 | } |
---|
| 255 | |
---|
[24] | 256 | // Wrap email text body, using _template_replaced if replacements have been used, or just a fresh _template if not. |
---|
[23] | 257 | $final_body = wordwrap(isset($this->_template_replaced) ? $this->_template_replaced : $this->_template); |
---|
[24] | 258 | |
---|
| 259 | // Ensure all placeholders have been replaced. Find anything with {...} characters. |
---|
[23] | 260 | if (preg_match('/({[^}]+})/', $final_body, $unreplaced_match)) { |
---|
[127] | 261 | $app->logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match[1]) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__); |
---|
[23] | 262 | return false; |
---|
| 263 | } |
---|
[42] | 264 | |
---|
[24] | 265 | // Final "to" header can have multiple addresses if in an array. |
---|
[23] | 266 | $final_to = is_array($this->_params['to']) ? join(', ', $this->_params['to']) : $this->_params['to']; |
---|
[42] | 267 | |
---|
[24] | 268 | // From headers are custom headers. |
---|
[38] | 269 | $headers = array('From' => $this->_params['from']); |
---|
| 270 | |
---|
| 271 | // Additional headers. |
---|
| 272 | if (isset($this->_params['headers']) && is_array($this->_params['headers'])) { |
---|
| 273 | $headers = array_merge($this->_params['headers'], $headers); |
---|
| 274 | } |
---|
[42] | 275 | |
---|
[38] | 276 | // Process headers. |
---|
| 277 | $final_headers = array(); |
---|
| 278 | foreach ($headers as $key => $val) { |
---|
| 279 | $final_headers[] = sprintf('%s: %s', $key, $val); |
---|
| 280 | } |
---|
| 281 | $final_headers = join("\r\n", $final_headers); |
---|
[42] | 282 | |
---|
[24] | 283 | // This is the address where delivery problems are sent to. We must strip off everything except the local@domain part. |
---|
[23] | 284 | $envelope_sender_header = sprintf('-f %s', preg_replace('/^.*<?([^\s@\[\]<>()]+\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5})>?$/iU', '$1', $this->_params['from'])); |
---|
[42] | 285 | |
---|
[36] | 286 | // Check for mail header injection attacks. |
---|
[38] | 287 | $full_mail_content = join("\n", array($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header)); |
---|
[36] | 288 | if (preg_match("/(Content-Type:|MIME-Version:|Content-Transfer-Encoding:|[\n\r]Bcc:|[\n\r]Cc:)/i", $full_mail_content)) { |
---|
[127] | 289 | $app->logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__); |
---|
[42] | 290 | sleep(3); |
---|
[36] | 291 | return false; |
---|
| 292 | } |
---|
[24] | 293 | |
---|
| 294 | // Ensure message was successfully accepted for delivery. |
---|
[114] | 295 | if (mail($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header)) { |
---|
[127] | 296 | $app->logMsg(sprintf('Email successfully sent to %s', $final_to), LOG_DEBUG, __FILE__, __LINE__); |
---|
[119] | 297 | return true; |
---|
[114] | 298 | } else { |
---|
[127] | 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__); |
---|
[23] | 300 | return false; |
---|
| 301 | } |
---|
| 302 | } |
---|
[42] | 303 | |
---|
[23] | 304 | /** |
---|
| 305 | * Validates an email address based on the recommendations in RFC 3696. |
---|
[42] | 306 | * Is more loose than restrictive, to allow the many valid variants of |
---|
[23] | 307 | * email addresses while catching the most common mistakes. Checks an array too. |
---|
| 308 | * http://www.faqs.org/rfcs/rfc822.html |
---|
| 309 | * http://www.faqs.org/rfcs/rfc2822.html |
---|
| 310 | * http://www.faqs.org/rfcs/rfc3696.html |
---|
| 311 | * http://www.faqs.org/rfcs/rfc1035.html |
---|
| 312 | * |
---|
| 313 | * @access public |
---|
| 314 | * @param mixed $email Address to check, string or array. |
---|
| 315 | * @return bool Validity of address. |
---|
| 316 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 317 | * @since 30 Nov 2005 22:00:50 |
---|
| 318 | */ |
---|
| 319 | function validEmail($email) |
---|
| 320 | { |
---|
[127] | 321 | $app =& App::getInstance(); |
---|
| 322 | |
---|
[24] | 323 | // If an array, check values recursively. |
---|
[23] | 324 | if (is_array($email)) { |
---|
| 325 | foreach ($email as $e) { |
---|
| 326 | if (!$this->validEmail($e)) { |
---|
| 327 | return false; |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | return true; |
---|
| 331 | } else { |
---|
[24] | 332 | // To be valid email address must match regex and fit within the lenth constraints. |
---|
| 333 | if (preg_match($this->getParam('regex'), $email, $e_parts) && strlen($e_parts[2]) < 64 && strlen($e_parts[3]) < 255) { |
---|
| 334 | return true; |
---|
| 335 | } else { |
---|
[127] | 336 | $app->logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__); |
---|
[23] | 337 | return false; |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | ?> |
---|