Andrew's Web Libraries (AWL)
Loading...
Searching...
No Matches
Multipart.php
1<?php
2
3require_once('AWLUtilities.php');
4
5class SinglePart {
6 private $content;
7 private $type;
8 private $otherHeaders;
9 private $disposition;
10 private $id;
11
12 public static $crlf = "\r\n";
13
14 function __construct( $content, $type='text/plain', $otherHeaders=array() ) {
15 $this->content = $content;
16 $this->type = $type;
17 $this->otherHeaders = $otherHeaders;
18 }
19
20 function render() {
21 $result = 'Content-Type: '.$this->type.self::$crlf;
22 $encoded = false;
23 foreach( $this->otherHeaders AS $header => $value ) {
24 $result .= $header.': '.$value.self::$crlf;
25 if ( $header == 'Content-Transfer-Encoding' ) $encoded = true;
26 }
27
28 if ( $encoded )
29 return $result . self::$crlf . $this->content;
30
31 return $result . 'Content-Transfer-Encoding: base64' . self::$crlf
32 . self::$crlf
33 . base64_encode($this->content);
34 }
35}
36
37
38class Multipart {
39
40 private $parts; // Always good for a giggle :-)
41 private $boundary;
42
43 function __construct() {
44 $this->parts = array();
45 $this->boundary = uuid();
46 }
47
48 function addPart() {
49 $args = func_get_args();
50 if ( is_string($args[0]) ) {
51 $newPart = new SinglePart( $args[0], (isset($args[1])?$args[1]:'text/plain'), (isset($args[2])?$args[2]:array()));
52 }
53 else
54 $newPart = $args[0];
55
56 $this->parts[] = $newPart;
57
58 return $newPart;
59 }
60
61
62 function getMimeHeaders() {
63 return 'MIME-Version: 1.0' . SinglePart::$crlf
64 .'Content-Type: multipart/mixed; boundary='.$this->boundary . SinglePart::$crlf ;
65 }
66
67 function getMimeParts() {
68 $result = '--' . $this->boundary . SinglePart::$crlf;
69 foreach( $this->parts AS $part ) {
70 $result .= $part->render() . SinglePart::$crlf . '--' . $this->boundary;
71 }
72 $result .= '--' . SinglePart::$crlf;
73 return $result;
74 }
75
76}