diogenes.hermes.inc.php

00001 <?php
00002 /*
00003  * Copyright (C) 2003-2004 Polytechnique.org
00004  * http://opensource.polytechnique.org/
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 require_once('Mail.php');
00022 require_once('Mail/mime.php');
00023 
00024 // {{{ class HermesMailer
00027 class HermesMailer extends Mail_Mime {
00028 
00029     // {{{ properties
00030     
00031     var $_mail;
00032 
00033     // }}}
00034     // {{{ constructor
00035 
00036     function HermesMailer() {
00037         $this->Mail_Mime("\n");
00038         $this->_mail =& Mail::factory('sendmail', Array('sendmail_args' => '-oi'));
00039     }
00040 
00041     // }}}
00042     // {{{ function _correct_emails()
00043 
00048     function _correct_emails($email)
00049     {
00050         return preg_replace('!(^|, *)([^<"][^<"]*[^< "]) *(<[^>]*>)!', '\1"\2" \3', $email);
00051     }
00052 
00053     // }}}
00054     // {{{ function addTo()
00055 
00056     function addTo($email)
00057     {
00058         $email = $this->_correct_emails($email);
00059         if (isset($this->_headers['To'])) {
00060             $this->_headers['To'] .= ", $email";
00061         } else {
00062             $this->_headers['To'] = $email;
00063         }
00064     }
00065 
00066     // }}}
00067     // {{{ function addCc()
00068 
00069     function addCc($email)
00070     {
00071         return parent::addCc($this->_correct_emails($email));
00072     }
00073 
00074     // }}}
00075     // {{{ function addBcc()
00076 
00077     function addBcc($email)
00078     {
00079         return parent::addBcc($this->_correct_emails($email));
00080     }
00081 
00082     // }}}
00083     // {{{ function setFrom()
00084 
00085     function setFrom($email)
00086     {
00087         return parent::setFrom($this->_correct_emails($email));
00088     }
00089 
00090     // }}}
00091     // {{{ function addHeader()
00092     
00093     function addHeader($hdr,$val)
00094     {
00095         switch($hdr) {
00096             case 'From':
00097                 $this->setFrom($val);
00098                 break;
00099 
00100             case 'To':
00101                 unset($this->_headers[$hdr]);
00102                 $this->addTo($val);
00103                 break;
00104 
00105             case 'Cc':
00106                 unset($this->_headers[$hdr]);
00107                 $this->addCc($val);
00108                 break;
00109 
00110             case 'Bcc':
00111                 unset($this->_headers[$hdr]);
00112                 $this->addBcc($val);
00113                 break;
00114 
00115             default:
00116                 $this->headers(Array($hdr=>$val));
00117         }
00118     }
00119 
00120     // }}}
00121     // {{{ function send()
00122 
00123     function send() {
00124         $addrs = Array();
00125         foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
00126             if(isset($this->_headers[$hdr])) {
00127                 require_once 'Mail/RFC822.php';
00128                 $addrs = array_merge($addrs, Mail_RFC822::parseAddressList($this->_headers[$hdr]));
00129             }
00130         }
00131         if(empty($addrs)) return false;
00132         
00133         $dests = Array();
00134         foreach($addrs as $a) $dests[] = "{$a->mailbox}@{$a->host}";
00135         
00136         // very important to do it in THIS order very precisely.
00137         $body = $this->get();
00138         $hdrs = $this->headers();
00139         return $this->_mail->send($dests, $hdrs, $body);
00140     }
00141 
00142     // }}}
00143 }
00144 
00145 // }}}
00146 
00147 ?>

Generated on Fri Jan 11 01:20:08 2008 for Diogenes by  doxygen 1.5.1