View Source
/hsphere/local/home/c251266/sunsetvines.com/www.sunsetvines.com/sunsetvines/current/vendor/creovel/classes/mailer.php (10.404 KB)
#0001
#0002
#0003
#0004
#0005
#0006
#0007
#0008
#0009
#0010
#0011
#0012
#0013
#0014
#0015
#0016
#0017
#0018
#0019
#0020
#0021
#0022
#0023
#0024
#0025
#0026
#0027
#0028
#0029
#0030
#0031
#0032
#0033
#0034
#0035
#0036
#0037
#0038
#0039
#0040
#0041
#0042
#0043
#0044
#0045
#0046
#0047
#0048
#0049
#0050
#0051
#0052
#0053
#0054
#0055
#0056
#0057
#0058
#0059
#0060
#0061
#0062
#0063
#0064
#0065
#0066
#0067
#0068
#0069
#0070
#0071
#0072
#0073
#0074
#0075
#0076
#0077
#0078
#0079
#0080
#0081
#0082
#0083
#0084
#0085
#0086
#0087
#0088
#0089
#0090
#0091
#0092
#0093
#0094
#0095
#0096
#0097
#0098
#0099
#0100
#0101
#0102
#0103
#0104
#0105
#0106
#0107
#0108
#0109
#0110
#0111
#0112
#0113
#0114
#0115
#0116
#0117
#0118
#0119
#0120
#0121
#0122
#0123
#0124
#0125
#0126
#0127
#0128
#0129
#0130
#0131
#0132
#0133
#0134
#0135
#0136
#0137
#0138
#0139
#0140
#0141
#0142
#0143
#0144
#0145
#0146
#0147
#0148
#0149
#0150
#0151
#0152
#0153
#0154
#0155
#0156
#0157
#0158
#0159
#0160
#0161
#0162
#0163
#0164
#0165
#0166
#0167
#0168
#0169
#0170
#0171
#0172
#0173
#0174
#0175
#0176
#0177
#0178
#0179
#0180
#0181
#0182
#0183
#0184
#0185
#0186
#0187
#0188
#0189
#0190
#0191
#0192
#0193
#0194
#0195
#0196
#0197
#0198
#0199
#0200
#0201
#0202
#0203
#0204
#0205
#0206
#0207
#0208
#0209
#0210
#0211
#0212
#0213
#0214
#0215
#0216
#0217
#0218
#0219
#0220
#0221
#0222
#0223
#0224
#0225
#0226
#0227
#0228
#0229
#0230
#0231
#0232
#0233
#0234
#0235
#0236
#0237
#0238
#0239
#0240
#0241
#0242
#0243
#0244
#0245
#0246
#0247
#0248
#0249
#0250
#0251
#0252
#0253
#0254
#0255
#0256
#0257
#0258
#0259
#0260
#0261
#0262
#0263
#0264
#0265
#0266
#0267
#0268
#0269
#0270
#0271
#0272
#0273
#0274
#0275
#0276
#0277
#0278
#0279
#0280
#0281
#0282
#0283
#0284
#0285
#0286
#0287
#0288
#0289
#0290
#0291
#0292
#0293
#0294
#0295
#0296
#0297
#0298
#0299
#0300
#0301
#0302
#0303
#0304
#0305
#0306
#0307
#0308
#0309
#0310
#0311
#0312
#0313
#0314
#0315
#0316
#0317
#0318
#0319
#0320
#0321
#0322
#0323
#0324
#0325
#0326
#0327
#0328
#0329
#0330
#0331
#0332
#0333
#0334
#0335
#0336
#0337
#0338
#0339
#0340
#0341
#0342
#0343
#0344
#0345
#0346
#0347
#0348
#0349
#0350
#0351
#0352
#0353
#0354
#0355
#0356
#0357
#0358
#0359
#0360
#0361
#0362
#0363
#0364
#0365
#0366
#0367
#0368
#0369
#0370
#0371
#0372
#0373
#0374
#0375
#0376
#0377
#0378
#0379
#0380
#0381
#0382
#0383
#0384
#0385
#0386
#0387
#0388
#0389
#0390
#0391
#0392
#0393
#0394
#0395
#0396
#0397
#0398
#0399
#0400
#0401
#0402
#0403
#0404
#0405
#0406
#0407
#0408
#0409
#0410
#0411
#0412
#0413
#0414
#0415
#0416
#0417
#0418
#0419
#0420
#0421
#0422
#0423
#0424
#0425
#0426
#0427
#0428
#0429
#0430
#0431
#0432
#0433
#0434
#0435
#0436
#0437
#0438
#0439
#0440
#0441
#0442
#0443
#0444
#0445
#0446
#0447
#0448
|
<?php /** * Emailer class. * * @todo * - auto-load attachments * - smtp support * - receiving emails */ class mailer {
/** * Private class properties. * * @author Nesbert Hidalgo * @access private */ private $_attachments; private $_content; private $_content_type = 'text/plain'; private $_content_transfer_encoding = '7bit'; private $_mailer_name; private $_message_boundary; private $_mime_boundary; private $_header; private $_send_on_close = false; /** * Public class properties. * * @author Nesbert Hidalgo * @access private */ public $delivery_method = 'sendmail'; public $bcc; public $cc; public $charset = 'utf-8'; public $from; public $reply_to; public $headers; public $recipients; public $sent_on; public $subject; public $body; public $text; public $html; /** * Construct set message boundaries on load * * @author Nesbert Hidalgo * @access public */ public function __construct() { //echo 'mailer start<br />'; // set message boundaries $this->_mailer_name = get_class($this); $this->_message_boundary = uniqid(rand(), true); $this->_mime_boundary = uniqid(rand(), true); } /** * Destruct send email if $_send_on_close is true * * @author Nesbert Hidalgo * @access public */ public function __destruct() { if ( $this->_send_on_close ) $this->send(); } /** * Magic functions. * * @author Nesbert Hidalgo * @access public * @param string $method required * @param array $args required */ public function __call($method, $args) { switch ( true ) {
case preg_match('/^create_(.+)$/', $method, $regs): case preg_match('/^deliver_(.+)$/', $method, $regs): $this->_action = $regs[1]; // set/call controller & action and pass arguments to child mailer class $this->_call_action($args); // if deliver_XXX send message if ( preg_match('/^deliver_(.+)$/', $method) ) $this->_send_on_close = true; break; default: $_ENV['error']->add("Undefined action '{$method}' in <strong>{$this->_mailer_name}</strong>"); break; } } /** * Sets $_controller & $_action and call child method * * @author Nesbert Hidalgo * @access private * @param array $args required */ private function _call_action($args) { if ( method_exists($this, $this->_action) ) { call_user_func_array(array($this, $this->_action), $args); } else { $_ENV['error']->add("Undefined action '{$this->_action}' in <strong>{$this->_mailer_name}</strong>"); } } /** * Checks if content type is 'text/plain' * * @author Nesbert Hidalgo * @access private * @return bool */ private function _is_plain_text() { return ( ($this->_content_type == 'text/plain') && !$this->html ? true : false ); } /** * Creates header of the message and loads it into $_header * * @author Nesbert Hidalgo * @access private * @return string */ private function _get_headers() { $this->_header = ""; $this->_header .= $this->from ? "From: ".$this->_get_email_address($this->from)."\n" : ""; $this->_header .= $this->reply_to ? "Reply-To: ".$this->_get_email_address($this->reply_to)."\n" : ""; $this->_header .= $this->cc ? "Cc: ".$this->_get_email_address($this->cc)."\n" : ""; $this->_header .= $this->bcc ? "Bcc: ".$this->_get_email_address($this->bcc)."\n" : ""; $this->_header .= "Date: ".( $this->sent_on ? $this->sent_on : date("r") )."\n"; if ( $this->_has_attachments() ) { $this->_header .= 'MIME-Version: 1.0'."\n"; $this->_header .= 'Content-Type: multipart/mixed; boundary="'.$this->_mime_boundary.'"'."\n"; $this->_header .= "--{$this->mime_boundary}"."\n"; } if ( !$this->_is_plain_text() ) { $this->_header .= 'Content-Type: multipart/alternative; boundary="'.$this->_message_boundary.'"'."\n"; } return $this->_header; } /** * Creates body of the message and loads it into $_content * * @author Nesbert Hidalgo * @access private * @return string */ private function _get_content() { // intialize content string $this->_content = ''; if ( $this->_is_plain_text() ) { $this->_content = $this->_get_text(); } else { // add text verison to message $this->_content .= "--{$this->_message_boundary}\n"; $this->_content .= "Content-Type: text/plain; charset={$this->charset}\n"; $this->_content .= "Content-Transfer-Encoding: {$this->content_transfer_encoding}\n"; $this->_content .= "Content-Disposition: inline\n\n"; $this->_content .= $this->_get_text(); $this->_content .= "\n\n"; // add html verison to message $this->_content .= "--{$this->_message_boundary}\n"; $this->_content .= "Content-Type: text/html; charset={$this->charset}\n"; $this->_content .= "Content-Transfer-Encoding: {$this->content_transfer_encoding}\n"; $this->_content .= "Content-Disposition: inline\n\n"; $this->_content .= $this->_get_html(); } // get attachments string if ( $this->_has_attachments() ) $this->_content .= $this->_get_attachments_str(); return $this->_content; } /* * http://us3.php.net/manual/en/function.include.php * Example 16-11. Using output buffering to include a PHP file into a string */ private function _get_include_contents($filename) { if ( is_file($filename) ) { ob_start(); include $filename; $contents = ob_get_contents(); ob_end_clean(); return $contents; } return false; } /** * Gets the view content for message * * @author Nesbert Hidalgo * @access private * @param string $str required * @return string */ private function _get_view() { return $this->_get_include_contents(VIEWS_PATH.$this->_mailer_name.DS.$this->_action.'.php'); } /** * Get text verison of message and remove all html tags from a string * * @author Nesbert Hidalgo * @access private * @param string $str required * @return string */ private function _get_text() { return strip_tags( $this->text ? $this->text : $this->_get_view() ); } /** * Get html verison of message * * @author Nesbert Hidalgo * @access private * @param string $str required * @return string */ private function _get_html() { return $this->_get_view(); } /** * Formats email address properties into a string * * @author Nesbert Hidalgo * @access private * @access mixed $email_address required * @return string */ private function _get_email_address($email_address) { return ( is_array($email_address) ? implode(',', $email_address) : $email_address ); } /** * Returns email subject * * @author Nesbert Hidalgo * @access private * @return string */ private function _get_subject() { return str_replace("\n", '', $this->subject); } /** * Sets email content type ['text/plain', 'text/html'] * * @author Nesbert Hidalgo * @access public * @param string $type optional * @return string $type */ public function set_content_type($type = 'text/plain') { return $this->_content_type = $type; } /** * Sets email content transfer encoding * * @author Nesbert Hidalgo * @access public * @param string $encoding optional * @return string $encoding */ public function set_content_transfer_encoding($encoding = '7bit') { return $this->_content_transfer_encoding = $encoding; } /** * Encodes the current email message into a string * * @author Nesbert Hidalgo * @access public * @return string */ public function encoded() { $return = "To: ".$this->_get_email_address($this->recipients)."\n"; $return .= "Subject: ".$this->_get_subject()."\n"; return $return.$this->_get_headers().$this->_get_content(); } /** * Sends the current email message. Returns true on success. * * @author Nesbert Hidalgo * @access public * @return bool */ public function send() { switch ( true ) { case ( $this->delivery_method == 'smtp' ): return false; break; case ( $this->delivery_method == 'sendmail' ): if ( mail($this->_get_email_address($this->recipients), $this->_get_subject(), $this->_get_content(), $this->_get_headers()) ) { return true; } else { return false; } break; case ( $this->delivery_method == 'test' ): return true; break; default: return false; break; } } /** * Alias to send(). Returns true on success. * * @author Nesbert Hidalgo * @access public * @return bool */ public function deliver() { return $this->send(); } /* need to finish attachment support */ private function _has_attachments() { return ( count($this->_attachments) ? true : false ); } private function _get_content_type($file_name) { return get_mime_type($file_name); } private function _get_transfer_encoding($file_name) { return 'base64'; } private function _encode_attachment($file_path) { if ( file_exists($file_path) ) { $file = fopen($file_path, 'r'); $attachment = fread($file, filesize($file_path)); $attachment = chunk_split(base64_encode($attachment)); fclose($file); return $attachment; } else { return true; } } private function _get_attachments_str() { if ( $this->_has_attachments() ) { $return = "\n--{$this->message_boundary}--\n"; foreach ( $this->_attachments as $content_id => $attachment ) { $return .= "\n--{$this->mime_boundary}\n"; $return .= "Content-Type: {$attachment[content_type]}; name={$attachment[file_name]}\n"; $return .= "Content-Transfer-Encoding: {$attachment[content_transfer_encoding]}\n"; $return .= "Content-ID: {$content_id}\n"; $return .= "Content-Disposition: attachment; filename=\"{$attachment[file_name]}\"\n\n"; $return .= $attachment['content_data']; $return .= "\n\n"; } return $return; } else { return false; } } public function add_attachment($file_path, $content_type = null, $content_transfer_encoding = null) { $key = 'attachment'.count($this->_attachments); $file_name = basename($file_path); $this->_attachments[$key]['content_id'] = $key; $this->_attachments[$key]['content_type'] = ( $content_type ? $content_type : $this->_get_content_type($file_name) ); $this->_attachments[$key]['content_transfer_encoding'] = ( $content_transfer_encoding ? $content_transfer_encoding : $this->_get_transfer_encoding($file_name) ); $this->_attachments[$key]['file_name'] = $file_name; $this->_attachments[$key]['content_data'] = $this->_encode_attachment($file_path); return $key; } } ?>
|