/[pliva-si]/inc/class.CMailFile
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /inc/class.CMailFile

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Mon Jun 25 09:57:25 2001 UTC (18 years, 1 month ago) by ravilov
Branch: MAIN, pliva
CVS Tags: r0, HEAD
Changes since 1.1: +0 -0 lines
initial import

1 <?php
2 /* notes from Dan Potter:
3 Sure. I changed a few other things in here too though. One is that I let
4 you specify what the destination filename is (i.e., what is shows up as in
5 the attachment). This is useful since in a web submission you often can't
6 tell what the filename was supposed to be from the submission itself. I
7 also added my own version of chunk_split because our production version of
8 PHP doesn't have it. You can change that back or whatever though =).
9 Finally, I added an extra "\n" before the message text gets added into the
10 MIME output because otherwise the message text wasn't showing up.
11 /*
12 note: someone mentioned a command-line utility called 'mutt' that
13 can mail attachments.
14 */
15 /*
16 If chunk_split works on your system, change the call to my_chunk_split
17 to chunk_split
18 */
19 /* Note: if you don't have base64_encode on your sytem it will not work */
20
21 // simple class that encapsulates mail() with addition of mime file attachment.
22 class CMailFile {
23 var $subject;
24 var $addr_to;
25 var $text_body;
26 var $text_encoded;
27 var $mime_headers;
28 var $mime_boundary = "--==================_846811060==_";
29 var $smtp_headers;
30
31 function CMailFile($subject,$to,$from,$msg,$filename,$mimetype = "application/octet-stream", $mime_filename = false) {
32 $this->subject = $subject;
33 $this->addr_to = $to;
34 $this->smtp_headers = $this->write_smtpheaders($from);
35 $this->text_body = $this->write_body($msg);
36 $this->text_encoded = $this->attach_file($filename,$mimetype,$mime_filename);
37 $this->mime_headers = $this->write_mimeheaders($filename, $mime_filename);
38 }
39
40 function attach_file($filename,$mimetype,$mime_filename) {
41 $encoded = $this->encode_file($filename);
42 if ($mime_filename) $filename = $mime_filename;
43 $out = "--" . $this->mime_boundary . "\n";
44 $out = $out . "Content-type: " . $mimetype . "; name=\"$filename\";\n";
45 $out = $out . "Content-Transfer-Encoding: base64\n";
46 $out = $out . "Content-disposition: attachment; filename=\"$filename\"\n\n";
47 $out = $out . $encoded . "\n";
48 $out = $out . "--" . $this->mime_boundary . "--" . "\n";
49 return $out;
50 // added -- to notify email client attachment is done
51 }
52
53 function encode_file($sourcefile) {
54 if (is_readable($sourcefile)) {
55 $fd = fopen($sourcefile, "r");
56 $contents = fread($fd, filesize($sourcefile));
57 $encoded = my_chunk_split(base64_encode($contents));
58 fclose($fd);
59 }
60 return $encoded;
61 }
62
63 function sendfile() {
64 $headers = $this->smtp_headers . $this->mime_headers;
65 $message = $this->text_body . $this->text_encoded;
66 mail($this->addr_to,$this->subject,$message,$headers);
67 }
68
69 function write_body($msgtext) {
70 $out = "--" . $this->mime_boundary . "\n";
71 $out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n";
72 $out = $out . $msgtext . "\n";
73 return $out;
74 }
75
76 function write_mimeheaders($filename, $mime_filename) {
77 if ($mime_filename) $filename = $mime_filename;
78 $out = "MIME-version: 1.0\n";
79 $out = $out . "Content-type: multipart/mixed; ";
80 $out = $out . "boundary=\"$this->mime_boundary\"\n";
81 $out = $out . "Content-transfer-encoding: 7BIT\n";
82 $out = $out . "X-attachments: $filename;\n\n";
83 return $out;
84 }
85
86 function write_smtpheaders($addr_from) {
87 $out = "From: $addr_from\n";
88 $out = $out . "Reply-To: $addr_from\n";
89 $out = $out . "X-Mailer: PHP3\n";
90 $out = $out . "X-Sender: $addr_from\n";
91 return $out;
92 }
93 }
94
95 // usage - mimetype example "image/gif"
96 // $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype);
97 // $mailfile->sendfile();
98
99 // Splits a string by RFC2045 semantics (76 chars per line, end with \r\n).
100 // This is not in all PHP versions so I define one here manuall.
101 function my_chunk_split($str)
102 {
103 $stmp = $str;
104 $len = strlen($stmp);
105 $out = "";
106 while ($len > 0) {
107 if ($len >= 76) {
108 $out = $out . substr($stmp, 0, 76) . "\r\n";
109 $stmp = substr($stmp, 76);
110 $len = $len - 76;
111 }
112 else {
113 $out = $out . $stmp . "\r\n";
114 $stmp = ""; $len = 0;
115 }
116 }
117 return $out;
118 }
119
120 // end script
121 ?>
122

  ViewVC Help
Powered by ViewVC 1.1.26