Hoa central
Message.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Mail\Content;
38 
39 use Hoa\Mail;
40 
50 class Message extends Content
51 {
57  const BOUNDARY = '@hoaproject-';
58 
64  protected $_content = [];
65 
66 
67 
73  public function __construct()
74  {
75  $this['content-type'] = 'multipart/mixed';
76  $this['date'] = date('r');
77 
78  return;
79  }
80 
87  public function addContent(Content $content)
88  {
89  $this->_content[] = $content;
90 
91  return $this;
92  }
93 
99  public function getContent()
100  {
101  return $this->_content;
102  }
103 
110  protected function _getContent()
111  {
112  $content = $this->getContent();
113 
114  if (empty($content)) {
115  throw new Mail\Exception('The message does not have content.', 0);
116  }
117 
118  if (1 < count($content)) {
119  $boundary =
120  '__bndry-' .
121  md5(static::BOUNDARY . microtime(true));
122  $frontier = '--' . $boundary;
123 
124  $this['content-type'] =
125  $this['content-type'] . '; ' .
126  'boundary="' . $boundary . '"';
127 
128  $message = static::formatHeaders($this->getHeaders()) . CRLF;
129 
130  foreach ($content as $c) {
131  $message .= $frontier . CRLF . $c . CRLF;
132  }
133 
134  $message .= $frontier . '--' . CRLF;
135  } else {
136  $oldContentType = $this['content-type'];
137  unset($this['content-type']);
138 
139  $message =
140  static::formatHeaders($this->getHeaders()) .
141  current($content);
142 
143  $this['content-type'] = $oldContentType;
144  }
145 
146  return $message;
147  }
148 
155  public function getRecipients()
156  {
157  $out = [];
158 
159  $this->_getRecipients($this['to'], $out);
160 
161  if (isset($this['cc'])) {
162  $this->_getRecipients($this['cc'], $out);
163  }
164 
165  if (isset($this['bcc'])) {
166  $this->_getRecipients($this['bcc'], $out);
167  }
168 
169  return $out;
170  }
171 
179  protected function _getRecipients($line, Array &$out)
180  {
181  $line = preg_replace_callback(
182  '#("|\')[^\1]+\1#',
183  function ($matches) {
184  return '';
185  },
186  $line
187  );
188 
189  foreach (explode(',', $line) as $contact) {
190  $out[] = static::getAddress($contact);
191  }
192 
193  return;
194  }
195 
202  public function getFormattedContent($headers = false)
203  {
204  return parent::getFormattedContent(false);
205  }
206 }
getFormattedContent($headers=false)
Definition: Message.php:202
_getRecipients($line, Array &$out)
Definition: Message.php:179
$content
Definition: Hoa.php:119
addContent(Content $content)
Definition: Message.php:87