123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- require_once 'PEAR.php';
- class Mail
- {
-
- public $sep = "\r\n";
-
- public static function factory($driver, $params = array())
- {
- $driver = strtolower($driver);
- @include_once 'Mail/' . $driver . '.php';
- $class = 'Mail_' . $driver;
- if (class_exists($class)) {
- $mailer = new $class($params);
- return $mailer;
- } else {
- return PEAR::raiseError('Unable to find class for driver ' . $driver);
- }
- }
-
- public function send($recipients, $headers, $body)
- {
- if (!is_array($headers)) {
- return PEAR::raiseError('$headers must be an array');
- }
- $result = $this->_sanitizeHeaders($headers);
- if (is_a($result, 'PEAR_Error')) {
- return $result;
- }
-
- if (is_array($recipients)) {
- $recipients = implode(', ', $recipients);
- }
-
-
- $subject = '';
- if (isset($headers['Subject'])) {
- $subject = $headers['Subject'];
- unset($headers['Subject']);
- }
-
- list(, $text_headers) = Mail::prepareHeaders($headers);
- return mail($recipients, $subject, $body, $text_headers);
- }
-
- protected function _sanitizeHeaders(&$headers)
- {
- foreach ($headers as $key => $value) {
- $headers[$key] =
- preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
- null, $value);
- }
- }
-
- protected function prepareHeaders($headers)
- {
- $lines = array();
- $from = null;
- foreach ($headers as $key => $value) {
- if (strcasecmp($key, 'From') === 0) {
- include_once 'Mail/RFC822.php';
- $parser = new Mail_RFC822();
- $addresses = $parser->parseAddressList($value, 'localhost', false);
- if (is_a($addresses, 'PEAR_Error')) {
- return $addresses;
- }
- $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
-
- if (strstr($from, ' ')) {
- return false;
- }
- $lines[] = $key . ': ' . $value;
- } elseif (strcasecmp($key, 'Received') === 0) {
- $received = array();
- if (is_array($value)) {
- foreach ($value as $line) {
- $received[] = $key . ': ' . $line;
- }
- }
- else {
- $received[] = $key . ': ' . $value;
- }
-
-
-
- $lines = array_merge($received, $lines);
- } else {
-
-
- if (is_array($value)) {
- $value = implode(', ', $value);
- }
- $lines[] = $key . ': ' . $value;
- }
- }
- return array($from, join($this->sep, $lines));
- }
-
- protected function parseRecipients($recipients)
- {
- include_once 'Mail/RFC822.php';
-
-
- if (is_array($recipients)) {
- $recipients = implode(', ', $recipients);
- }
-
-
-
- $Mail_RFC822 = new Mail_RFC822();
- $addresses = $Mail_RFC822->parseAddressList($recipients, 'localhost', false);
-
- if (is_a($addresses, 'PEAR_Error')) {
- return $addresses;
- }
- $recipients = array();
- if (is_array($addresses)) {
- foreach ($addresses as $ob) {
- $recipients[] = $ob->mailbox . '@' . $ob->host;
- }
- }
- return $recipients;
- }
- }
|