4af183e9d318be11be66ec2d4949cf205c9bb5f2
[diogenes.git] / include / diogenes / diogenes.hermes.inc.php
1 <?php
2 /*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 require_once('Mail.php');
22 require_once('Mail/mime.php');
23
24 // {{{ class HermesMailer
25 /** Class for sending inline or multipart-emails.
26 */
27 class HermesMailer extends Mail_Mime {
28
29 // {{{ properties
30
31 var $_mail;
32
33 // }}}
34 // {{{ constructor
35
36 function HermesMailer() {
37 $this->Mail_Mime("\n");
38 $this->_mail =& Mail::factory('sendmail', Array('-oi'));
39 }
40
41 // }}}
42 // {{{ function _correct_emails()
43
44 /**
45 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> wich is RFC compliant
46 */
47
48 function _correct_emails($email)
49 {
50 return preg_replace('!(^|, *)([^<"][^<"]*[^< "]) *(<[^>]*>)!', '\1"\2" \3', $email);
51 }
52
53 // }}}
54 // {{{ function addTo()
55
56 function addTo($email)
57 {
58 $email = $this->_correct_emails($email);
59 if (isset($this->_headers['To'])) {
60 $this->_headers['To'] .= ", $email";
61 } else {
62 $this->_headers['To'] = $email;
63 }
64 }
65
66 // }}}
67 // {{{ function addCc()
68
69 function addCc($email)
70 {
71 return parent::addCc($this->_correct_emails($email));
72 }
73
74 // }}}
75 // {{{ function addBcc()
76
77 function addBcc($email)
78 {
79 return parent::addBcc($this->_correct_emails($email));
80 }
81
82 // }}}
83 // {{{ function setFrom()
84
85 function setFrom($email)
86 {
87 return parent::setFrom($this->_correct_emails($email));
88 }
89
90 // }}}
91 // {{{ function addHeader()
92
93 function addHeader($hdr,$val)
94 {
95 switch($hdr) {
96 case 'From':
97 $this->setFrom($val);
98 break;
99
100 case 'To':
101 unset($this->_headers[$hdr]);
102 $this->addTo($val);
103 break;
104
105 case 'Cc':
106 unset($this->_headers[$hdr]);
107 $this->addCc($val);
108 break;
109
110 case 'Bcc':
111 unset($this->_headers[$hdr]);
112 $this->addBcc($val);
113 break;
114
115 default:
116 $this->headers(Array($hdr=>$val));
117 }
118 }
119
120 // }}}
121 // {{{ function send()
122
123 function send() {
124 $addrs = Array();
125 foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
126 if(isset($this->_headers[$hdr])) {
127 require_once 'Mail/RFC822.php';
128 $addrs = array_merge($addrs, Mail_RFC822::parseAddressList($this->_headers[$hdr]));
129 }
130 }
131 if(empty($addrs)) return false;
132
133 $dests = Array();
134 foreach($addrs as $a) $dests[] = "{$a->mailbox}@{$a->host}";
135
136 // very important to do it in THIS order very precisely.
137 $body = $this->get();
138 $hdrs = $this->headers();
139 return $this->_mail->send($dests, $hdrs, $body);
140 }
141
142 // }}}
143 }
144
145 // }}}
146
147 ?>