ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
Mail.php
Go to the documentation of this file.
1 <?php
46 require_once 'PEAR.php';
47 
57 class Mail
58 {
63  var $sep = "\r\n";
64 
74  function &factory($driver, $params = array())
75  {
76  $driver = strtolower($driver);
77  @include_once 'Mail/' . $driver . '.php';
78  $class = 'Mail_' . $driver;
79  if (class_exists($class)) {
80  $mailer = new $class($params);
81  return $mailer;
82  } else {
83  return PEAR::raiseError('Unable to find class for driver ' . $driver);
84  }
85  }
86 
114  function send($recipients, $headers, $body)
115  {
116  if (!is_array($headers)) {
117  return PEAR::raiseError('$headers must be an array');
118  }
119 
120  $result = $this->_sanitizeHeaders($headers);
121  if (is_a($result, 'PEAR_Error')) {
122  return $result;
123  }
124 
125  // if we're passed an array of recipients, implode it.
126  if (is_array($recipients)) {
127  $recipients = implode(', ', $recipients);
128  }
129 
130  // get the Subject out of the headers array so that we can
131  // pass it as a seperate argument to mail().
132  $subject = '';
133  if (isset($headers['Subject'])) {
134  $subject = $headers['Subject'];
135  unset($headers['Subject']);
136  }
137 
138  // flatten the headers out.
139  list(, $text_headers) = Mail::prepareHeaders($headers);
140 
141  return mail($recipients, $subject, $body, $text_headers);
142  }
143 
153  function _sanitizeHeaders(&$headers)
154  {
155  foreach ($headers as $key => $value) {
156  $headers[$key] =
157  preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
158  null, $value);
159  }
160  }
161 
178  function prepareHeaders($headers)
179  {
180  $lines = array();
181  $from = null;
182 
183  foreach ($headers as $key => $value) {
184  if (strcasecmp($key, 'From') === 0) {
185  include_once 'Mail/RFC822.php';
186  $parser = new Mail_RFC822();
187  $addresses = $parser->parseAddressList($value, 'localhost', false);
188  if (is_a($addresses, 'PEAR_Error')) {
189  return $addresses;
190  }
191 
192  $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
193 
194  // Reject envelope From: addresses with spaces.
195  if (strstr($from, ' ')) {
196  return false;
197  }
198 
199  $lines[] = $key . ': ' . $value;
200  } elseif (strcasecmp($key, 'Received') === 0) {
201  $received = array();
202  if (is_array($value)) {
203  foreach ($value as $line) {
204  $received[] = $key . ': ' . $line;
205  }
206  }
207  else {
208  $received[] = $key . ': ' . $value;
209  }
210  // Put Received: headers at the top. Spam detectors often
211  // flag messages with Received: headers after the Subject:
212  // as spam.
213  $lines = array_merge($received, $lines);
214  } else {
215  // If $value is an array (i.e., a list of addresses), convert
216  // it to a comma-delimited string of its elements (addresses).
217  if (is_array($value)) {
218  $value = implode(', ', $value);
219  }
220  $lines[] = $key . ': ' . $value;
221  }
222  }
223 
224  return array($from, join($this->sep, $lines));
225  }
226 
240  function parseRecipients($recipients)
241  {
242  include_once 'Mail/RFC822.php';
243 
244  // if we're passed an array, assume addresses are valid and
245  // implode them before parsing.
246  if (is_array($recipients)) {
247  $recipients = implode(', ', $recipients);
248  }
249 
250  // Parse recipients, leaving out all personal info. This is
251  // for smtp recipients, etc. All relevant personal information
252  // should already be in the headers.
253  $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
254 
255  // If parseAddressList() returned a PEAR_Error object, just return it.
256  if (is_a($addresses, 'PEAR_Error')) {
257  return $addresses;
258  }
259 
260  $recipients = array();
261  if (is_array($addresses)) {
262  foreach ($addresses as $ob) {
263  $recipients[] = $ob->mailbox . '@' . $ob->host;
264  }
265  }
266 
267  return $recipients;
268  }
269 
270 }