Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
multipart.php
1<?php
8namespace Bitrix\Main\Mail;
9
14class Multipart extends Part
15{
16 const MIXED = 'multipart/mixed';
17 const ALTERNATIVE = 'multipart/alternative';
18 const RELATED = 'multipart/related';
19
21 protected $parts = [];
22
24 protected $uniqueString;
25
27 protected $eol;
28
32 public function __construct()
33 {
34 parent::__construct();
35 $this->uniqueString = mb_substr(uniqid(mt_rand(100, 999)), 0, 10);
36 $this->setContentType(self::MIXED);
37 }
38
45 public function setEol($eol)
46 {
47 parent::setEol($eol);
48 foreach ($this->parts as $part)
49 {
50 $part->setEol($this->getEol());
51 }
52 return $this;
53 }
54
61 public function setContentType($type)
62 {
63 $boundary = $this->getBoundary($type);
64 $this->addHeader('Content-Type', "$type; boundary=\"$boundary\"");
65 return $this;
66 }
67
74 public function addPart(Part $part)
75 {
76 $part->setEol($this->getEol());
77 $this->parts[] = $part;
78 return $this;
79 }
80
86 public function toStringHeaders()
87 {
88 $count = count($this->parts);
89 if ($count === 0)
90 {
91 return '';
92 }
93 elseif ($count === 1)
94 {
95 return current($this->parts)->toStringHeaders();
96 }
97
98 return parent::toStringHeaders();
99 }
100
106 public function toStringBody()
107 {
108 $count = count($this->parts);
109 if ($count === 0)
110 {
111 return '';
112 }
113 elseif ($count === 1)
114 {
115 return current($this->parts)->toStringBody();
116 }
117
118
119 $result = '';
120 $boundary = $this->getBoundary();
121 foreach ($this->parts as $part)
122 {
123 $result .= '--' . $boundary . $this->eol;
124 $result .= (string) $part;
125 }
126 $result .= '--' . $boundary . '--' . $this->eol;
127
128 return $result;
129 }
130
136 protected function getPartCount()
137 {
138 $count = 0;
139 foreach ($this->parts as $part)
140 {
141 if ($part instanceof Multipart)
142 {
143 $count += $part->getPartCount();
144 }
145 else
146 {
147 $count += 1;
148 }
149 }
150
151 return $count;
152 }
153
160 protected function getBoundary($contentType = null)
161 {
162 $type = $contentType ?: $this->getHeader('Content-Type');
163 $type = explode(';', $type);
164 $type = $type[0];
165 switch ($type)
166 {
167 case self::MIXED:
168 $prefix = 'mix';
169 break;
170
172 $prefix = 'alt';
173 break;
174
175 default:
176 $prefix = '';
177 }
178
179 return '-------' . $prefix . $this->uniqueString;
180 }
181}
getBoundary($contentType=null)
addHeader($name, $value)
Definition part.php:62