00001 <?php
00002 require_once dirname(__FILE__) . '/imailmessagebuilder.cls.php';
00003
00004
00005
00006
00007 class AttachmentsBuilder implements IMailMessageBuilder {
00008
00009
00010
00011
00012
00013 protected $message_builder;
00014
00015
00016
00017
00018
00019
00020 protected $attachments;
00021
00022
00023
00024
00025
00026
00027 protected $boundary;
00028
00029
00030
00031
00032
00033
00034
00035 public function __construct(IMailMessageBuilder $msg_builder, $attachments) {
00036 $this->message_builder = $msg_builder;
00037 $this->attachments = $attachments;
00038 $this->boundary = 'GYROMAILSEP-' . Common::create_token();
00039 }
00040
00041
00042
00043
00044
00045
00046 public function get_mail_mime() {
00047 return 'multipart/mixed; boundary="' . $this->boundary . '"';
00048 }
00049
00050
00051
00052
00053
00054
00055 public function get_body() {
00056 $blocks = array();
00057 $blocks[] = $this->create_block(
00058 $this->message_builder->get_mail_mime(), false, $this->message_builder->get_body(), $this->message_builder->get_additional_headers()
00059 );
00060 foreach($this->attachments as $name => $file) {
00061 $blocks[] = $this->create_attachment_block($name, $file);
00062 }
00063 return
00064 $this->start_seperator($this->boundary) .
00065 implode("\n" . $this->start_seperator($this->boundary), $blocks) .
00066 $this->end_seperator($this->boundary);
00067 }
00068
00069
00070
00071
00072
00073
00074 public function get_boundary() {
00075 return $this->boundary;
00076 }
00077
00078
00079
00080
00081 protected function start_seperator($boundary) {
00082 return "--" . $boundary . "\n";
00083 }
00084
00085
00086
00087
00088 protected function end_seperator($boundary) {
00089 return "\n--" . $boundary . "--\n";
00090 }
00091
00092
00093
00094
00095 protected function create_attachment_block($name, $file) {
00096 return $this->create_block(
00097 $this->get_attachment_mime($file) . '; name=' . ConverterFactory::encode($name, ConverterFactory::MIMEHEADER),
00098 'base64',
00099 base64_encode(file_get_contents($file))
00100 );
00101 }
00102
00103
00104
00105
00106 protected function create_block($mime_type, $encoding, $content, $more_headers = array()) {
00107 $ret = '';
00108 $header = $more_headers;
00109 $header['Content-Type'] = $mime_type;
00110 if ($encoding) {
00111 $header['Content-Transfer-Encoding'] = $encoding;
00112 }
00113
00114 $ret = Arr::implode("\n", $header, ': ') . "\n\n" . $content;
00115 return $ret;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124 private function get_attachment_mime($file) {
00125 if (function_exists('mime_content_type')) {
00126 return mime_content_type($file);
00127 }
00128 return 'application/octet-stream';
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138 public function get_additional_headers() {
00139 return array();
00140 }
00141 }