[4445] | 1 | <?php |
---|
[5068] | 2 | /** |
---|
| 3 | * |
---|
[5130] | 4 | * Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
[5068] | 5 | * |
---|
[5130] | 6 | * This program is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU General Public License |
---|
| 8 | * as published by the Free Software Foundation; either version 2 |
---|
| 9 | * of the License, or (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. |
---|
[5068] | 19 | * |
---|
| 20 | * You can contact Prognus Software Livre headquarters at Av. Tancredo Neves, |
---|
[5130] | 21 | * 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at |
---|
[5068] | 22 | * e-mail address prognus@prognus.com.br. |
---|
| 23 | * |
---|
| 24 | * |
---|
[5130] | 25 | * @package MailService |
---|
[5068] | 26 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[5130] | 27 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 28 | * @sponsor Caixa EconÃŽmica Federal |
---|
[5068] | 29 | * @version 1.0 |
---|
[5130] | 30 | * @since 2.4.0 |
---|
[5068] | 31 | */ |
---|
[4445] | 32 | |
---|
[5316] | 33 | require_once ( dirname(__FILE__).'/../library/mime/mimePart.php' ); |
---|
| 34 | require_once ( dirname(__FILE__).'/../library/mime/mime.php' ); |
---|
[5068] | 35 | |
---|
[4445] | 36 | |
---|
[5130] | 37 | class MailService |
---|
[4445] | 38 | { |
---|
[5130] | 39 | static $configuration = array( 'host' => '', 'port' => '', 'username' => '', 'password' => '' , 'auth' => ''); |
---|
| 40 | protected $mail; |
---|
| 41 | protected $smtpConfig; |
---|
| 42 | protected $arrayFields = array(); |
---|
[5068] | 43 | |
---|
| 44 | public function __construct( $config = null) |
---|
[4445] | 45 | { |
---|
[5130] | 46 | $this->mail = new Mail_mime(); |
---|
| 47 | |
---|
[5068] | 48 | if( !$config ) |
---|
| 49 | { |
---|
| 50 | require_once ( ROOT.'/header.inc.php' ); |
---|
| 51 | |
---|
| 52 | $boemailadmin = CreateObject('emailadmin.bo'); |
---|
| 53 | $emailadmin = $boemailadmin->getProfileList(); |
---|
| 54 | $emailadmin = $boemailadmin->getProfile($emailadmin[0]['profileID']); |
---|
| 55 | |
---|
| 56 | self::$configuration['host'] = $emailadmin['smtpServer']; |
---|
| 57 | self::$configuration['port'] = $emailadmin['smtpPort']; |
---|
[5130] | 58 | self::$configuration['auth'] = $emailadmin['smtpAuth'] ? true : false; |
---|
| 59 | self::$configuration['username'] = $emailadmin['imapAdminUsername']; |
---|
| 60 | self::$configuration['password'] = $emailadmin['imapAdminPW']; |
---|
[5068] | 61 | } |
---|
| 62 | else |
---|
| 63 | $this->configure( $config ); |
---|
[4445] | 64 | |
---|
[5130] | 65 | } |
---|
[5068] | 66 | |
---|
[5130] | 67 | public function sendMail( $to, $from, $subject, $body, $bcc = false, $cc = false, $isHTML = true ) |
---|
[4445] | 68 | { |
---|
[5130] | 69 | |
---|
[5068] | 70 | if( $body ) |
---|
| 71 | { |
---|
[5130] | 72 | if( $isHTML ) $this->mail->setHTMLBody( $body ); |
---|
| 73 | else $this->mail->setTXTBody( $body ); |
---|
[5068] | 74 | } |
---|
| 75 | |
---|
[5130] | 76 | if( $subject ) $this->mail->setSubject ( $subject ); |
---|
[5068] | 77 | |
---|
[5130] | 78 | if( !$from ) $from = $GLOBALS['phpgw']->preferences->values['email']; |
---|
[5068] | 79 | |
---|
[5130] | 80 | $this->mail->setFrom( $from ); |
---|
[5068] | 81 | |
---|
[5130] | 82 | $this->mail->addTo($to); |
---|
[5068] | 83 | |
---|
[5130] | 84 | if( $bcc ) $this->mail->addBcc ($bbc); |
---|
[5068] | 85 | |
---|
[5130] | 86 | if( $cc ) $this->mail->addCc ($cc); |
---|
[5068] | 87 | |
---|
[5130] | 88 | $hdrs = $this->mail->headers($this->arrayFields); |
---|
[5068] | 89 | |
---|
[5316] | 90 | require_once (dirname(__FILE__).'/../library/Mail/Mail.php'); |
---|
[5130] | 91 | |
---|
| 92 | $mail_object =& Mail::factory("smtp", self::$configuration); |
---|
[4445] | 93 | |
---|
[5130] | 94 | $recipients = ''; |
---|
| 95 | |
---|
| 96 | if( $hdrs["To"] ) |
---|
| 97 | $recipients .= $hdrs["To"]; |
---|
| 98 | if( $hdrs["Cc"] && $recipients) |
---|
| 99 | $recipients .= ', '.$hdrs["Cc"]; |
---|
| 100 | if($hdrs["Cc"] && !$recipients) |
---|
| 101 | $recipients = $hdrs["Cc"]; |
---|
| 102 | |
---|
| 103 | if($hdrs["Bcc"]) |
---|
| 104 | $arrayBcc = explode(',',$hdrs["Bcc"]); |
---|
| 105 | |
---|
| 106 | if($recipients) |
---|
| 107 | $sent = $mail_object->send($recipients, $hdrs , $this->mail->getMessageBody()); |
---|
| 108 | |
---|
| 109 | if(isset($arrayBcc)){ |
---|
| 110 | foreach ($arrayBcc as $bcc) |
---|
| 111 | if($bcc) |
---|
| 112 | $sent = $mail_object->send($bcc, $hdrs , $this->mail->getMessageBody()); |
---|
[4445] | 113 | } |
---|
[5130] | 114 | if($sent !== true) |
---|
| 115 | return $sent->message; |
---|
[4445] | 116 | |
---|
[5130] | 117 | return true; |
---|
| 118 | } |
---|
[5068] | 119 | |
---|
| 120 | public function configure( $config ) |
---|
[4445] | 121 | { |
---|
[5068] | 122 | if( !$config ) return( false ); |
---|
| 123 | |
---|
| 124 | foreach( $config as $key => $value ) |
---|
| 125 | if( $value && isset( self::$configuration[$key] ) ) |
---|
| 126 | self::$configuration[$key] = $value; |
---|
| 127 | } |
---|
[5130] | 128 | |
---|
| 129 | public function send() |
---|
| 130 | { |
---|
[5316] | 131 | require_once (dirname(__FILE__).'/../library/Mail/Mail.php'); |
---|
[5130] | 132 | $hdrs = $this->mail->headers($this->arrayFields); |
---|
| 133 | $mail_object =& Mail::factory("smtp", self::$configuration); |
---|
| 134 | |
---|
| 135 | $recipients = ''; |
---|
| 136 | |
---|
| 137 | if( $hdrs["To"] ) |
---|
| 138 | $recipients .= $hdrs["To"]; |
---|
| 139 | if( $hdrs["Cc"] && $recipients) |
---|
| 140 | $recipients .= ', '.$hdrs["Cc"]; |
---|
| 141 | if($hdrs["Cc"] && !$recipients) |
---|
| 142 | $recipients = $hdrs["Cc"]; |
---|
| 143 | |
---|
| 144 | if($hdrs["Bcc"]) |
---|
| 145 | $arrayBcc = explode(',',$hdrs["Bcc"]); |
---|
| 146 | |
---|
| 147 | if($recipients) |
---|
| 148 | $sent = $mail_object->send($recipients, $hdrs , $this->mail->getMessageBody()); |
---|
| 149 | |
---|
| 150 | if(isset($arrayBcc)){ |
---|
| 151 | foreach ($arrayBcc as $bcc) |
---|
| 152 | if($bcc) |
---|
| 153 | $sent = $mail_object->send($bcc, $hdrs , $this->mail->getMessageBody()); |
---|
| 154 | } |
---|
| 155 | if($sent !== true) |
---|
| 156 | return $sent->message; |
---|
| 157 | |
---|
| 158 | return true; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | public function addStringAttachment($file, $filename, $type, $encoding = 'base64', $disposition = 'attachment') |
---|
| 162 | { |
---|
[5461] | 163 | $this->mail->addAttachment($file, $type, $filename, false, $encoding, $disposition , $charset = '' , $language = '' , $location = '' , $n_encoding = 'quoted-printable', null , '' , 'ISO-8859-1'); |
---|
[5130] | 164 | } |
---|
| 165 | |
---|
| 166 | public function addFileAttachment($file, $filename, $type, $encoding = 'base64', $disposition = 'attachment') |
---|
| 167 | { |
---|
[5461] | 168 | $this->mail->addAttachment($file, $type, $filename, true, $encoding, $disposition, $charset = '' , $language = '' , $location = '' , $n_encoding = 'quoted-printable' , null , '' , 'ISO-8859-1'); |
---|
[5130] | 169 | } |
---|
| 170 | |
---|
| 171 | public function addFileImage($file, $c_type='application/octet-stream', $name = '', $content_id = null) |
---|
| 172 | { |
---|
| 173 | $this->mail->addHTMLImage($file, $c_type, $name, true, $content_id ); |
---|
[5068] | 174 | } |
---|
[5130] | 175 | |
---|
| 176 | public function addStringImage($file, $c_type='application/octet-stream', $name = '', $content_id = null) |
---|
| 177 | { |
---|
| 178 | $this->mail->addHTMLImage($file, $c_type, $name, false, $content_id ); |
---|
[4445] | 179 | } |
---|
| 180 | |
---|
[5130] | 181 | public function getMessage() |
---|
[4445] | 182 | { |
---|
[5130] | 183 | return $this->mail->getMessage(null,null,$this->arrayFields); |
---|
[4445] | 184 | } |
---|
| 185 | |
---|
[5130] | 186 | public function addTo($email) |
---|
[4445] | 187 | { |
---|
[5130] | 188 | $this->mail->addTo($email); |
---|
[4445] | 189 | } |
---|
| 190 | |
---|
[5130] | 191 | public function addCc($email) |
---|
[4445] | 192 | { |
---|
[5130] | 193 | $this->mail->addCc($email); |
---|
| 194 | } |
---|
[4445] | 195 | |
---|
[5130] | 196 | public function addBcc($email) |
---|
| 197 | { |
---|
| 198 | $this->mail->addBcc($email); |
---|
| 199 | } |
---|
[4445] | 200 | |
---|
[5130] | 201 | public function setSubject($subject) |
---|
[4445] | 202 | { |
---|
[5130] | 203 | $this->mail->setSubject($subject); |
---|
[4445] | 204 | } |
---|
| 205 | |
---|
[5130] | 206 | public function setFrom($email) |
---|
[4445] | 207 | { |
---|
[5130] | 208 | $this->mail->setFrom($email); |
---|
[4445] | 209 | } |
---|
| 210 | |
---|
[5130] | 211 | public function setBodyText($data) |
---|
| 212 | { |
---|
| 213 | $this->mail->setTXTBody($data); |
---|
| 214 | } |
---|
[4445] | 215 | |
---|
[5130] | 216 | public function setBodyHtml($data) |
---|
| 217 | { |
---|
| 218 | $this->mail->setHTMLBody($data); |
---|
[4445] | 219 | } |
---|
[5130] | 220 | |
---|
| 221 | public function getBodyHtml() |
---|
| 222 | { |
---|
| 223 | return $this->mail->getHTMLBody(); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | public function addHeaderField($field,$value) |
---|
| 227 | { |
---|
| 228 | $this->arrayFields[$field] = $value; |
---|
| 229 | } |
---|
| 230 | |
---|
[4445] | 231 | } |
---|
| 232 | |
---|
| 233 | ServiceLocator::register( 'mail', new MailService() ); |
---|
| 234 | |
---|