/home/banprang/domains/plai.go.th/public_html/coremain/mimemail.inc.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/*
 +-------------------------------------------------------------------+
 |                     M I M E M A I L   (v1.5)                      |
 |                                                                   |
 | Copyright Gerd Tentler               www.gerd-tentler.de/tools    |
 | Created: Nov. 2, 2004                Last modified: Oct. 19, 2005 |
 +-------------------------------------------------------------------+
 | This program may be used and hosted free of charge by anyone for  |
 | personal purpose as long as this copyright notice remains intact. |
 |                                                                   |
 | Obtain permission before selling the code for this program or     |
 | hosting this software on a commercial website or redistributing   |
 | this software over the Internet or in any other medium. In all    |
 | cases copyright must remain intact.                               |
 +-------------------------------------------------------------------+

==========================================================================================================
 This script can send MIME-mails with attachments. It uses the PHP mail-function.

 EXAMPLE:

 $mail = new MIMEMAIL("HTML");

 $mail->senderName = "sender name";
 $mail->senderMail = "sender@email";
 $mail->bcc = "bcc@email";

 $mail->subject = "This is the subject line";

 $mail->body = "Hello! This is a message for you.";   // OR: $mail->body = "path_to_file/filename";

 $mail->attachment[] = "path_to_file1/filename1";
 $mail->attachment[] = "path_to_file2/filename2";
 ...

 $mail->create();

 $mail->send("recipient1@email");
 $mail->send("recipient2@email,recipient3@email,recipient4@email");
 ...
==========================================================================================================
*/
  
error_reporting(E_WARNING);

  class 
MIMEMAIL {
//--------------------------------------------------------------------------------------------------------
// Configuration
//--------------------------------------------------------------------------------------------------------
    
var $type 'Text';             // e-mail type ("HTML" or "Text")
    
var $senderName '';           // sender name
    
var $senderMail '';           // sender e-mail address
    
var $cc '';                   // Cc (e-mail address)
    
var $bcc '';                  // Bcc (e-mail address)
    
var $documentRoot '';         // document root (path to images, stylesheets, etc.)
    
var $saveDir '';              // save e-mail to this directory (do not send) => just for testing :)
    
var $charSet 'ISO-8859-1';    // character set (ISO)
    
var $useQueue false;          // use mail queue (true = yes, false = no) => does not work with PHP
                                    // versions < 4.0.5, or with versions >= 4.2.3 in Safe Mode, or with
                                    // MTAs other than sendmail!

    
var $mime = array('jpg'  => 'image/jpeg',
                      
'jpeg' => 'image/jpeg',
                      
'gif'  => 'image/gif',
                      
'png'  => 'image/png',
                      
'swf'  => 'application/x-shockwave-flash',
                      
'doc'  => 'application/msword',
                      
'xls'  => 'application/msexcel',
                      
'ppt'  => 'application/mspowerpoint',
                      
'pdf'  => 'application/pdf',
                      
'zip'  => 'application/zip',
                      
'rtf'  => 'text/rtf',
                      
'rtx'  => 'text/richtext',
                      
'txt'  => 'text/plain',
                      
'js'   => 'text/javascript',
                      
'css'  => 'text/css');

    var 
$exclude = array('htm''php''pl''prl''cgi');
    var 
$inline = array();
    var 
$attachment = array();
    var 
$subject$body$header$footer;

//--------------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------------
    
function MIMEMAIL($type '') {
      if(
$type$this->type $type;
    }

    function 
get_img_type($data) {
      
$abc substr($data020);
      if(
strstr($abc'GIF')) $ftype 'gif';
      else if(
strstr($abc'JFIF') || strstr($abc'Exif')) $ftype 'jpeg';
      else if(
strstr($abc'PNG')) $ftype 'png';
      else if(
strstr($abc'FWS') || strstr($abc'CWS')) $ftype 'swf';
      else 
$ftype '';

      return 
$ftype;
    }

    function 
get_img_data($html$m$css) {
      global 
$HTTP_HOST;

      
$host 'http://' ereg_replace('/$'''$HTTP_HOST);

      for(
$i 0$i count($m[0]); $i++) {
        
$ftype $data $ext $fname '';

        if(!
preg_match('/^(http|ftp|mailto|javascript)/i'$m[2][$i])) {
          
$inlName $m[2][$i];
          
$ext substr($inlNamestrrpos($inlName'.') + 1);
          
$incl true;

          for(
$j 0$j count($this->exclude) && $incl$j++) {
            if(
stristr($ext$this->exclude[$j])) $incl false;
          }

          if(
$incl) {
            if(
$this->documentRoot) {
              
$doc_root $this->documentRoot;

              while(
ereg('^\.\./'$inlName)) {
                
$inlName substr($inlName3);
                
$doc_root ereg_replace('/[^/]+$'''$doc_root);
              }
              
$fname "$doc_root/$inlName";
            }
            else 
$fname $inlName;

            if(
$fp = @fopen($fname'rb')) {
              
$data fread($fpfilesize($fname));
              
fclose($fp);
            }
          }
        }

        if(
$data) {
          if(!
$ext$ftype $this->get_img_type($data);
          else 
$ftype $ext;

          if(
$css$html str_replace($m[0][$i], ' ' $m[1][$i] . '(cid:' $inlName ')'$html);
          else 
$html str_replace($m[0][$i], ' ' $m[1][$i] . '="cid:' $inlName '"'$html);

          if(!
$this->inline[$ftype][$inlName]) {
            
$this->inline[$ftype][$inlName] = chunk_split(base64_encode($data));
          }
        }
        else if(!
preg_match('/^(http|ftp|mailto|javascript)/i'$m[2][$i])) {
          if(
$css$html str_replace($m[0][$i], ' ' $m[1][$i] . "($host/$inlName)"$html);
          else 
$html str_replace($m[0][$i], ' ' $m[1][$i] . "=\"$host/$inlName\""$html);
        }
      }
      return 
$html;
    }

    function 
check_body() {
      if(
preg_match_all('/ (src|background|href)="?([^" >]+)"?/i'$this->body$m))
        
$this->body $this->get_img_data($this->body$mfalse);
      if(
preg_match_all('/ (url)\(([^\)]+)\)/i'$this->body$m))
        
$this->body $this->get_img_data($this->body$mtrue);

      
$this->body preg_replace("/<(table|tr|div)([^>]*)>\r?\n?/i""<\\1\\2>\r\n"$this->body);
      
$this->body preg_replace("/<\/(table|tr|td|style|script|div|p)>\r?\n?/i""</\\1>\r\n"$this->body);
    }

    function 
prepare() {
      
$attachments $filetype = array();

      if(
count($this->attachment)) while(list(, $att) = each($this->attachment)) {
        if(
$att && $att != 'none') {
          if(
$fp = @fopen($att'rb')) {
            
$filename basename(str_replace('\\''/'$att));
            
$file fread($fpfilesize($att));
            
fclose($fp);

            
$ext substr($filenamestrrpos($filename'.')+1);
            
$filetype[$filename] = $this->mime[$ext] ? $this->mime[$ext] : 'application/octet-stream';
            
$attachments[$filename] = chunk_split(base64_encode($file));
          }
        }
      }

      
$this->header $this->footer '';
      
$uid1 'Next_' strtoupper(md5(uniqid(time()) . 1));
      
$uid2 'Next_' strtoupper(md5(uniqid(time()) . 2));

      
$this->header .= "Return-Path: " $this->senderMail "\n" .
                       
"Subject:\n" .
                       
"From: " $this->senderName " <" $this->senderMail ">\n" .
                       
"X-Sender: <" $this->senderMail ">\n" .
                       
"X-Mailer: PHP " phpversion() . "\n" .
                       
"MIME-Version: 1.0\n";

      if(
$this->cc$this->header .= "Cc: " $this->cc "\n";
      if(
$this->bcc$this->header .= "Bcc: " $this->bcc "\n";

      if(
$this->type == 'HTML' || count($attachments)) {
        
$this->header .= "Content-Type: multipart/mixed;\n\t" .
                         
"boundary=\"$uid1\"\n\n" .
                         
"This is a multi-part message in MIME format.\n\n" .
                         
"--$uid1\n";
      }

      if(
$this->type == 'HTML') {
        
$this->header .= "Content-Type: multipart/related;\n\t" .
                         
"boundary=\"$uid2\"\n\n" .
                         
"--$uid2\n";
      }

      
$this->header .= "Content-Type: text/" . (($this->type == 'HTML') ? 'html' 'plain') . ";\n\t" .
                       
"charset=\"" $this->charSet "\"\n" .
                       
"Content-Transfer-Encoding: 8bit\n\n";

      if(
$this->type == 'HTML') {
        if(
count($this->inline)) while(list($ftype$arr) = each($this->inline)) {
          if(
count($arr)) while(list($inlName$data) = each($arr)) {
            
$inlType $this->mime[$ftype] ? $this->mime[$ftype] : 'application/octet-stream';
            
$this->footer .= "--$uid2\n" .
                             
"Content-Type: $inlType;\n\t" .
                             
"name=\"$inlName\"\n" .
                             
"Content-ID: <$inlName>\n" .
                             
"Content-Disposition: inline;\n\t" .
                             
"filename=\"$inlName\"\n" .
                             
"Content-Transfer-Encoding: base64\n\n" .
                             
"$data\n\n";
          }
        }
        
$this->footer .= "--$uid2--\n\n";
      }

      if(
count($attachments)) while(list($filename$file) = each($attachments)) {
        
$this->footer .= "--$uid1\n" .
                         
'Content-Type: ' $filetype[$filename] . ";\n\t" .
                         
"name=\"$filename\"\n" .
                         
"Content-Disposition: attachment;\n\t" .
                         
"filename=\"$filename\"\n" .
                         
"Content-Transfer-Encoding: base64\n\n" .
                         
"$file\n\n";
      }

      if(
$this->type == 'HTML' || $file$this->footer .= "--$uid1--";
    }

    function 
send($email) {
      if(
$this->saveDir$this->header str_replace("Subject:\n"'Subject: ' $this->subject "\n"$this->header);
      else 
$this->header str_replace("Subject:\n"''$this->header);

      
$mime_mail $this->header $this->body "\n\n" $this->footer;

      if(
$this->saveDir) {
        if(
$fp = @fopen($this->saveDir '/mail.eml''w')) {
          
$mime_mail "To: $email\n" $mime_mail;
          
fwrite($fp$mime_mailstrlen($mime_mail));
          
fclose($fp);
          
$ok true;
        }
        else 
$ok false;
      }
      else {
        
$php_ver phpversion();
        if(
function_exists('ini_get')) $safe_mode ini_get('safe_mode');
        else 
$safe_mode get_cfg_var('safe_mode');

        if(
$php_ver '4.0.5' || ($php_ver >= '4.2.3' && $safe_mode)) {
          
$ok = @mail($email$this->subject''$mime_mail);
        }
        else {
          
$options = ($this->useQueue '-odq ' '') . '-i -f ' $this->senderMail;
          
$ok = @mail($email$this->subject''$mime_mail$options);
        }
      }

      return 
$ok;
    }

    function 
create() {
      if(
strlen($this->body) < 100) {
        
$file str_replace('\\''/'$this->body);

        if(
$fp = @fopen($file'r')) {
          
$this->body fread($fpfilesize($file));
          
fclose($fp);
          
$this->documentRootdirname($file);
        }
      }
      if(
$this->type == 'HTML'$this->check_body();
      
$this->prepare();
    }
  }
?>