1 | <?php |
---|
2 | /** |
---|
3 | * |
---|
4 | * Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify it under |
---|
7 | * the terms of the GNU Affero General Public License version 3 as published by |
---|
8 | * the Free Software Foundation with the addition of the following permission |
---|
9 | * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED |
---|
10 | * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE |
---|
11 | * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
15 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
16 | * details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU Affero General Public License |
---|
19 | * along with this program; if not, see www.gnu.org/licenses or write to |
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
21 | * MA 02110-1301 USA. |
---|
22 | * |
---|
23 | * This code is based on the OpenXchange Connector and on the Prognus pSync |
---|
24 | * Connector both developed by the community and licensed under the GPL |
---|
25 | * version 2 or above as published by the Free Software Foundation. |
---|
26 | * |
---|
27 | * You can contact Prognus Software Livre headquarters at Av. Tancredo Neves, |
---|
28 | * 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at |
---|
29 | * e-mail address prognus@prognus.com.br. |
---|
30 | * |
---|
31 | * Serviço de email |
---|
32 | * |
---|
33 | * @package Services |
---|
34 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
35 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
36 | * @version 1.0 |
---|
37 | * @sponsor Caixa Econômica Federal |
---|
38 | */ |
---|
39 | |
---|
40 | |
---|
41 | if( !function_exists( 'zend_include' ) ) |
---|
42 | { |
---|
43 | function zend_include( $path ) |
---|
44 | { |
---|
45 | set_include_path( LIBRARY ); |
---|
46 | require_once( $path ); |
---|
47 | restore_include_path(); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | zend_include( 'Zend/Mail.php' ); |
---|
53 | |
---|
54 | |
---|
55 | /** |
---|
56 | * Serviço de email |
---|
57 | * |
---|
58 | * Classe responsável pelas operações de email |
---|
59 | * |
---|
60 | * @package Service |
---|
61 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
62 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
63 | * @sponsor Caixa Econômica Federal |
---|
64 | * @version 1.0 |
---|
65 | */ |
---|
66 | class MailService extends Zend_Mail /*implements ConfigurationService, HookableService*/ |
---|
67 | { |
---|
68 | static $configuration = array( 'host' => '', 'port' => '', 'username' => '', 'password' => '' ); |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * Construtor da classe |
---|
73 | * |
---|
74 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
75 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
76 | * @sponsor Caixa Econômica Federal |
---|
77 | * @param string $config |
---|
78 | */ |
---|
79 | public function __construct( $config = null) |
---|
80 | { |
---|
81 | if( !$config ) |
---|
82 | { |
---|
83 | require_once ( ROOT.'/header.inc.php' ); |
---|
84 | |
---|
85 | $boemailadmin = CreateObject('emailadmin.bo'); |
---|
86 | $emailadmin = $boemailadmin->getProfileList(); |
---|
87 | $emailadmin = $boemailadmin->getProfile($emailadmin[0]['profileID']); |
---|
88 | |
---|
89 | self::$configuration['host'] = $emailadmin['smtpServer']; |
---|
90 | self::$configuration['port'] = $emailadmin['smtpPort']; |
---|
91 | |
---|
92 | // self::$configuration['username'] = $emailadmin['user']; |
---|
93 | // self::$configuration['password'] = $emailadmin['pass']; |
---|
94 | // self::$configuration['name'] = $emailadmin['name']; |
---|
95 | // self::$configuration['type'] = 'SMTP'; |
---|
96 | |
---|
97 | // array_merge( $configuration, array( 'auth' => 'login' ) ); |
---|
98 | |
---|
99 | zend_include( 'Zend/Mail/Transport/Smtp.php' ); |
---|
100 | zend_include( 'Zend/Mail/Part.php' ); |
---|
101 | set_include_path(LIBRARY); |
---|
102 | parent::setDefaultTransport( new Zend_Mail_Transport_Smtp( self::$configuration['host'], self::$configuration ) ); |
---|
103 | restore_include_path(); |
---|
104 | } |
---|
105 | |
---|
106 | else |
---|
107 | $this->configure( $config ); |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | /** |
---|
112 | * Método que envia a mensagem |
---|
113 | * |
---|
114 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
115 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
116 | * @sponsor Caixa Econômica Federal |
---|
117 | * @param string $to |
---|
118 | * @param string $from |
---|
119 | * @param string $subject |
---|
120 | * @param string $body |
---|
121 | * @param string $bcc |
---|
122 | * @param string $cc |
---|
123 | * @param string $isHTML |
---|
124 | */ |
---|
125 | public function send( $to, $from, $subject, $body, $bcc = null, $cc = null, $isHTML = true ) |
---|
126 | { |
---|
127 | set_include_path( LIBRARY ); |
---|
128 | if( $body ) |
---|
129 | { |
---|
130 | if( $isHTML ) |
---|
131 | parent::setBodyHtml( $body ); |
---|
132 | |
---|
133 | else |
---|
134 | parent::setBodyText( $body ); |
---|
135 | } |
---|
136 | |
---|
137 | if( $subject ) |
---|
138 | parent::setSubject( $subject ); |
---|
139 | |
---|
140 | if( !$from ) |
---|
141 | $from = $GLOBALS['phpgw']->preferences->values['email']; |
---|
142 | |
---|
143 | parent::setFrom( $from, self::getNameByMail( $from ) ); |
---|
144 | |
---|
145 | //pra nao ter que fazer os ifs acima... |
---|
146 | $destTypes = array( 'to', 'bcc', 'cc' ); |
---|
147 | |
---|
148 | foreach( $destTypes as $destination ) |
---|
149 | { |
---|
150 | $dest = $$destination; |
---|
151 | |
---|
152 | if( $dest ) |
---|
153 | { |
---|
154 | if( !is_array( $dest ) ) |
---|
155 | $dest = array( $dest ); |
---|
156 | |
---|
157 | foreach( $dest as $d ) |
---|
158 | { |
---|
159 | $addDestination = 'add'.ucfirst($destination); |
---|
160 | |
---|
161 | parent::$addDestination( $d, self::getNameByMail( $d ) ); |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | parent::send(); |
---|
167 | restore_include_path(); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | /** |
---|
172 | * Método de configuração |
---|
173 | * |
---|
174 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
175 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
176 | * @sponsor Caixa Econômica Federal |
---|
177 | * @param string $config |
---|
178 | */ |
---|
179 | public function configure( $config ) |
---|
180 | { |
---|
181 | if( !$config ) return( false ); |
---|
182 | |
---|
183 | foreach( $config as $key => $value ) |
---|
184 | { |
---|
185 | if( $value && isset( self::$configuration[$key] ) ) |
---|
186 | { |
---|
187 | self::$configuration[$key] = $value; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | //hook |
---|
193 | public function getNameByMail( $mail ) |
---|
194 | { |
---|
195 | return( null ); |
---|
196 | } |
---|
197 | |
---|
198 | public function getAttachment( $attachment ) |
---|
199 | { |
---|
200 | return( null ); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Adds attachment to the mail message |
---|
205 | * |
---|
206 | * @param string $file binary file |
---|
207 | * @param string $filename file name |
---|
208 | * @param string $type type example: image/gif |
---|
209 | * @param Zend_Mime $disposition example: Zend_Mime::DISPOSITION_INLINE |
---|
210 | * @param Zend_Mime $encoding example: Zend_Mime::ENCODING_BASE64 |
---|
211 | * @return bool |
---|
212 | */ |
---|
213 | public function addAttachment($file, $filename, $type, $encoding = null, $disposition = 'DISPOSITION_ATTACHMENT') |
---|
214 | { |
---|
215 | $part = new Zend_Mime_Part( $file ); |
---|
216 | |
---|
217 | $part->type = $type; |
---|
218 | $part->filename = $filename; |
---|
219 | |
---|
220 | switch ($disposition) |
---|
221 | { |
---|
222 | case 'DISPOSITION_INLINE': |
---|
223 | $part->disposition = Zend_Mime::DISPOSITION_INLINE; |
---|
224 | break; |
---|
225 | case 'DISPOSITION_ATTACHMENT': |
---|
226 | $part->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; |
---|
227 | break; |
---|
228 | default: |
---|
229 | break; |
---|
230 | } |
---|
231 | |
---|
232 | switch ($encoding) |
---|
233 | { |
---|
234 | case 'ENCODING_7BIT': |
---|
235 | $part->encoding = Zend_Mime::ENCODING_7BIT; |
---|
236 | break; |
---|
237 | case 'ENCODING_8BIT:': |
---|
238 | $part->encoding = Zend_Mime::ENCODING_8BIT; |
---|
239 | break; |
---|
240 | case 'ENCODING_QUOTEDPRINTABLE:': |
---|
241 | $part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE; |
---|
242 | break; |
---|
243 | default: |
---|
244 | $part->encoding = Zend_Mime::ENCODING_BASE64; |
---|
245 | break; |
---|
246 | } |
---|
247 | |
---|
248 | parent::addAttachment( $part ); |
---|
249 | |
---|
250 | return; |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | ServiceLocator::register( 'mail', new MailService() ); |
---|
255 | |
---|