Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Abstract.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Mime.php';
28 
29 
41 {
47  public $body = '';
48 
54  public $boundary = '';
55 
61  public $header = '';
62 
68  protected $_headers = array();
69 
75  protected $_isMultipart = false;
76 
82  protected $_mail = false;
83 
89  protected $_parts = array();
90 
96  public $recipients = '';
97 
103  public $EOL = "\r\n";
104 
115  abstract protected function _sendMail();
116 
127  protected function _getHeaders($boundary)
128  {
129  if (null !== $boundary) {
130  // Build multipart mail
131  $type = $this->_mail->getType();
132  if (!$type) {
133  if ($this->_mail->hasAttachments) {
135  } elseif ($this->_mail->getBodyText() && $this->_mail->getBodyHtml()) {
137  } else {
139  }
140  }
141 
142  $this->_headers['Content-Type'] = array(
143  $type . ';'
144  . $this->EOL
145  . " " . 'boundary="' . $boundary . '"'
146  );
147  $this->boundary = $boundary;
148  }
149 
150  $this->_headers['MIME-Version'] = array('1.0');
151 
152  return $this->_headers;
153  }
154 
165  protected static function _formatHeader(&$item, $key, $prefix)
166  {
167  $item = $prefix . ': ' . $item;
168  }
169 
181  protected function _prepareHeaders($headers)
182  {
183  if (!$this->_mail) {
187  #require_once 'Zend/Mail/Transport/Exception.php';
188  throw new Zend_Mail_Transport_Exception('Missing Zend_Mail object in _mail property');
189  }
190 
191  $this->header = '';
192 
193  foreach ($headers as $header => $content) {
194  if (isset($content['append'])) {
195  unset($content['append']);
196  $value = implode(',' . $this->EOL . ' ', $content);
197  $this->header .= $header . ': ' . $value . $this->EOL;
198  } else {
199  array_walk($content, array(get_class($this), '_formatHeader'), $header);
200  $this->header .= implode($this->EOL, $content) . $this->EOL;
201  }
202  }
203 
204  // Sanity check on headers -- should not be > 998 characters
205  $sane = true;
206  foreach (explode($this->EOL, $this->header) as $line) {
207  if (strlen(trim($line)) > 998) {
208  $sane = false;
209  break;
210  }
211  }
212  if (!$sane) {
216  #require_once 'Zend/Mail/Transport/Exception.php';
217  throw new Zend_Mail_Exception('At least one mail header line is too long');
218  }
219  }
220 
233  protected function _buildBody()
234  {
235  if (($text = $this->_mail->getBodyText())
236  && ($html = $this->_mail->getBodyHtml()))
237  {
238  // Generate unique boundary for multipart/alternative
239  $mime = new Zend_Mime(null);
240  $boundaryLine = $mime->boundaryLine($this->EOL);
241  $boundaryEnd = $mime->mimeEnd($this->EOL);
242 
243  $text->disposition = false;
244  $html->disposition = false;
245 
246  $body = $boundaryLine
247  . $text->getHeaders($this->EOL)
248  . $this->EOL
249  . $text->getContent($this->EOL)
250  . $this->EOL
251  . $boundaryLine
252  . $html->getHeaders($this->EOL)
253  . $this->EOL
254  . $html->getContent($this->EOL)
255  . $this->EOL
256  . $boundaryEnd;
257 
258  $mp = new Zend_Mime_Part($body);
260  $mp->boundary = $mime->boundary();
261 
262  $this->_isMultipart = true;
263 
264  // Ensure first part contains text alternatives
265  array_unshift($this->_parts, $mp);
266 
267  // Get headers
268  $this->_headers = $this->_mail->getHeaders();
269  return;
270  }
271 
272  // If not multipart, then get the body
273  if (false !== ($body = $this->_mail->getBodyHtml())) {
274  array_unshift($this->_parts, $body);
275  } elseif (false !== ($body = $this->_mail->getBodyText())) {
276  array_unshift($this->_parts, $body);
277  }
278 
279  if (!$body) {
283  #require_once 'Zend/Mail/Transport/Exception.php';
284  throw new Zend_Mail_Transport_Exception('No body specified');
285  }
286 
287  // Get headers
288  $this->_headers = $this->_mail->getHeaders();
289  $headers = $body->getHeadersArray($this->EOL);
290  foreach ($headers as $header) {
291  // Headers in Zend_Mime_Part are kept as arrays with two elements, a
292  // key and a value
293  $this->_headers[$header[0]] = array($header[1]);
294  }
295  }
296 
305  public function send(Zend_Mail $mail)
306  {
307  $this->_isMultipart = false;
308  $this->_mail = $mail;
309  $this->_parts = $mail->getParts();
310  $mime = $mail->getMime();
311 
312  // Build body content
313  $this->_buildBody();
314 
315  // Determine number of parts and boundary
316  $count = count($this->_parts);
317  $boundary = null;
318  if ($count < 1) {
322  #require_once 'Zend/Mail/Transport/Exception.php';
323  throw new Zend_Mail_Transport_Exception('Empty mail cannot be sent');
324  }
325 
326  if ($count > 1) {
327  // Multipart message; create new MIME object and boundary
328  $mime = new Zend_Mime($this->_mail->getMimeBoundary());
329  $boundary = $mime->boundary();
330  } elseif ($this->_isMultipart) {
331  // multipart/alternative -- grab boundary
332  $boundary = $this->_parts[0]->boundary;
333  }
334 
335  // Determine recipients, and prepare headers
336  $this->recipients = implode(',', $mail->getRecipients());
337  $this->_prepareHeaders($this->_getHeaders($boundary));
338 
339  // Create message body
340  // This is done so that the same Zend_Mail object can be used in
341  // multiple transports
342  $message = new Zend_Mime_Message();
343  $message->setParts($this->_parts);
344  $message->setMime($mime);
345  $this->body = $message->generateMessage($this->EOL);
346 
347  // Send to transport!
348  $this->_sendMail();
349  }
350 }
getRecipients()
Definition: Mail.php:633
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
send(Zend_Mail $mail)
Definition: Abstract.php:305
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$message
$type
Definition: item.phtml:13
$prefix
Definition: name.phtml:25
$value
Definition: gender.phtml:16
const MULTIPART_ALTERNATIVE
Definition: Mime.php:43
static _formatHeader(&$item, $key, $prefix)
Definition: Abstract.php:165
const MULTIPART_MIXED
Definition: Mime.php:44