File size: 10,435 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
 * class.mailer.php
 *
 * @package gulliver.system
 *
 * ProcessMaker Open Source Edition
 * Copyright (C) 2004 - 2011 Colosa Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
 * Coral Gables, FL, 33134, USA, or email info@colosa.com.
 *
 */

/* Send emails using the class "PHPMailer"
 *  Email server configuration constants:
 *    MAIL_MAILER     mail/smtp
 *    MAIL_HOST       email.server.address
 *    MAIL_SMTPAUTH   true/false
 *    MAIL_USERNAME   Email Username (smtp)
 *    MAIL_PASSWORD   Email Password (smtp)
 *    MAIL_TIMEOUT    Email Timeout  (smtp)
 *    MAIL_CHARSET    Email Charset  "utf-8"
 *    MAIL_ENCODING   Email Encoding "base64"
 *  Other required configuration constants:
 *    PATH_HTMLMAIL   Email templates path
 * @author David Callizaya <davidsantos@colosa.com>
 */

/**
 *
 * @package gulliver.system
 *
 */

class mailer
{

    /**
     * instanceMailer
     *
     * @return object(PHPMailer) $mailer
     */
    function instanceMailer ()
    {
        $mailer = new PHPMailer();
        $mailer->PluginDir = PATH_THIRDPARTY . 'phpmailer/';
        //DEFAULT CONFIGURATION
        $mailer->Mailer = 'mail';
        $mailer->Host = "";
        $mailer->SMTPAuth = false;
        $mailer->Username = "";
        $mailer->Password = "";
        $mailer->Timeout = 30;
        $mailer->CharSet = 'utf-8';
        $mailer->Encoding = 'base64';
        if (defined( 'MAIL_MAILER' )) {
            $mailer->Mailer = MAIL_MAILER;
        }
        if (defined( 'MAIL_HOST' )) {
            $mailer->Host = MAIL_HOST;
        }
        if (defined( 'MAIL_SMTPAUTH' )) {
            $mailer->SMTPAuth = MAIL_SMTPAUTH;
        }
        if (defined( 'MAIL_USERNAME' )) {
            $mailer->Username = MAIL_USERNAME;
        }
        if (defined( 'MAIL_PASSWORD' )) {
            $mailer->Password = MAIL_PASSWORD;
        }
        if (defined( 'MAIL_TIMEOUT' )) {
            $mailer->Timeout = MAIL_TIMEOUT;
        }
        if (defined( 'MAIL_CHARSET' )) {
            $mailer->CharSet = MAIL_CHARSET;
        }
        if (defined( 'MAIL_ENCODING' )) {
            $mailer->Encoding = MAIL_ENCODING;
        }
        return $mailer;
    }

    /* ARPA INTERNET TEXT MESSAGES
     * Returns an array with the "name" and "email" of an ARPA type
     * email $address.
     * @author David Callizaya
     */
    function arpaEMAIL ($address)
    {
        $arpa = array ();
        preg_match( "/([^<>]*)(?:\<([^<>]*)\>)?/", $address, $matches );
        $isEmail = preg_match( "/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i", $matches[1] );
        $arpa['email'] = ((isset( $matches[2] )) && ($matches[2] != '')) ? $matches[2] : (($isEmail) ? $matches[1] : '');
        $arpa['name'] = ($isEmail) ? '' : $matches[1];
        return $arpa;
    }

    /**
     * sendTemplate
     *
     * @param string $from default value empty
     * @param string $target default value default value empty
     * @param string $cc default value default value empty
     * @param string $bcc default value default value empty
     * @param string $subject default value empty
     * @param array $Fields
     * @param string $templateFile default value "empty.html"
     * @param array $attachs
     * @param boolean $plainText default value false
     * @param boolean $returnContent default value false
     *
     * @return object $content or $result
     */
    function sendTemplate ($from = "", $target = "", $cc = "", $bcc = "", $subject = "", $Fields = array(), $templateFile = "empty.html", $attachs = array(), $plainText = false, $returnContent = false)
    {
        // Read the content of the TemplateFile
        $fp = fopen( PATH_HTMLMAIL . $templateFile, "r" );
        $content = fread( $fp, filesize( PATH_HTMLMAIL . $templateFile ) );
        fclose( $fp );
        //Replace the @@Fields with the $Fields array.
        $content = mailer::replaceFields( $Fields, $content );
        //Compatibility  with class.Application
        if ($attachs === 'FALSE') {
            return $content;
        }
            //Create the alternative body (text only)
            //$h2t =& new html2text($content);
        $text = ''; //$h2t->get_text();
        //Prepate el phpmailer
        $mailer = mailer::instanceMailer();
        $arpa = mailer::arpaEMAIL( $from );
        $mailer->From = $arpa['email'] == '' ? $mailer->defaultEMail : $arpa['email'];
        $mailer->FromName = $arpa['name'];
        $arpa = mailer::arpaEMAIL( $target );
        $mailer->AddAddress( $arpa['email'], $arpa['name'] );
        $mailer->AddCC( $cc );
        $mailer->AddBCC( $bcc );
        $mailer->Subject = $subject;
        if ($plainText) {
            $content = $text;
        }
        if ($content === '') {
            $content = 'empty';
        }
        $mailer->Body = $content;
        //$mailer->AltBody = $text;
        $mailer->isHTML( ! $plainText );
        //Attach the required files
        if (is_array( $attachs ))
            if (sizeof( $attachs ) > 0)
                foreach ($attachs as $aFile)
                    $mailer->AddAttachment( $aFile, basename( $aFile ) );
            //Send the e-mail.
        for ($r = 1; $r <= 4; $r ++) {
            $result = $mailer->Send();
            if ($result) {
                break;
            }
        }
        //unset($h2t);
        if ($result && $returnContent) {
            return $content;
        }
        return $result;
    }

    /**
     * sendHtml
     *
     * @param string $from default value empty
     * @param string $target default value empty
     * @param string $cc default value empty
     * @param string $bcc default value empty
     * @param string $subject default value empty
     * @param array $Fields
     * @param string $content default value empty
     * @param array $attachs default value empty
     * @param string $plainText default false
     * @param boolean $returnContent default value false
     *
     * @return object $result
     */
    function sendHtml ($from = "", $target = "", $cc = "", $bcc = "", $subject = "", $Fields = array(), $content = "", $attachs = array(), $plainText = false, $returnContent = false)
    {
        //Replace the @@Fields with the $Fields array.
        $content = mailer::replaceFields( $Fields, $content );
        //Create the alternative body (text only)
        //$h2t =& new html2text($content);
        $text = ''; //$h2t->get_text();
        //Prepate el phpmailer
        $mailer = mailer::instanceMailer();
        $arpa = mailer::arpaEMAIL( $from );
        $mailer->From = $arpa['email'] == '' ? $mailer->defaultEMail : $arpa['email'];
        $mailer->FromName = $arpa['name'];
        $arpa = mailer::arpaEMAIL( $target );
        $mailer->AddAddress( $arpa['email'], $arpa['name'] );
        $mailer->AddCC( $cc );
        $mailer->AddBCC( $bcc );
        $mailer->Subject = $subject;
        if ($plainText) {
            $content = $text;
        }
        if ($content === '') {
            $content = 'empty';
        }
        $mailer->Body = $content;
        //$mailer->AltBody = $text;
        $mailer->isHTML( ! $plainText );
        //Attach the required files
        if (is_array( $attachs ))
            if (sizeof( $attachs ) > 0)
                foreach ($attachs as $aFile)
                    $mailer->AddAttachment( $aFile, basename( $aFile ) );
            //Send the e-mail.
        for ($r = 1; $r <= 4; $r ++) {
            $result = $mailer->Send();
            if ($result) {
                break;
            }
        }
        //unset($h2t);
        if ($result && $returnContent) {
            return $content;
        }
        return $result;
    }

    /**
     * sendText
     *
     * @param string $from default value empty
     * @param string $target default value empty
     * @param string $cc default value empty
     * @param string $bcc default value empty
     * @param string $subject default value empty
     * @param string $content default value empty
     * @param array $attachs
     *
     * @return object $result
     */
    function sendText ($from = "", $target = "", $cc = "", $bcc = "", $subject = "", $content = "", $attachs = array())
    {
        //Prepate el phpmailer
        $mailer = mailer::instanceMailer();
        $arpa = mailer::arpaEMAIL( $from );
        $mailer->From = $arpa['email'] == '' ? $mailer->defaultEMail : $arpa['email'];
        $mailer->FromName = $arpa['name'];
        $arpa = mailer::arpaEMAIL( $target );
        $mailer->AddAddress( $arpa['email'], $arpa['name'] );
        $mailer->AddCC( $cc );
        $mailer->AddBCC( $bcc );
        $mailer->Subject = $subject;
        if ($content === '') {
            $content = 'empty';
        }
        $mailer->Body = $content;
        $mailer->AltBody = $content;
        $mailer->isHTML( false );
        //Attach the required files
        if (sizeof( $attachs ) > 0) {
            foreach ($attachs as $aFile) {
                $mailer->AddAttachment( $aFile, basename( $aFile ) );
            }
        }
            //Send the e-mail.
        for ($r = 1; $r <= 4; $r ++) {
            $result = $mailer->Send();
            if ($result) {
                break;
            }
        }
        return $result;
    }

    /**
     * replaceFields
     *
     * @param array $Fields
     * @param string $content default value string empty
     *
     * @return none
     */
    function replaceFields ($Fields = array(), $content = "")
    {
        return G::replaceDataField( $content, $Fields );
    }

    /**
     * html2text
     *
     * empty
     *
     * @return none
     */
    function html2text ()
    {
        //$h2t =& new html2text($content);
        //return $h2t->get_text();
    }
}