summaryrefslogtreecommitdiffstats
path: root/includes/class_email.php
blob: 5c8c4cc0c7588f591ec7e196d0cb21cea067610e (plain)
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
<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * class_email.php
 * Began: Sat January 4 2003
 * 
 * $Id: class_email.php 46 2007-06-19 07:29:11Z tsigo $
 * 
 ******************************/

if ( !defined('EQDKP_INC') )
{
     die('Do not access this file directly.');
}
 
/**
* EMail class
* Templating e-mail class inspired by phpBB
*/
class EMail
{
    /**
    * Template file
    * @var tpl_file
    */
    var $tpl_file;
    
    /**
    * EMail message
    * @var msg
    */
    var $msg;

    /**
    * @var subject
    * @var extra headers
    * @var address
    */
    var $subject, $extra_headers, $address;
    
    /**
    * Constructor
    * 
    * Make vars null
    */
    function EMail()
    {
        $this->tpl_file = null;
        $this->address = null;
        $this->msg = '';
    }
    
    /**
    * Set vars to empty strings
    */
    function reset()
    {
        $this->tpl_file = '';
        $this->address = '';
        $this->msg = '';
        $this->vars = array();
    }
    
    /**
    * Assign email address to send to
    * 
    * @param $address
    */
    function address($address)
    {
        $this->address = $address;
    }
    
    /**
    * Assign subject line
    * 
    * @param $subject
    */
    function subject($subject='')
    {
        $this->subject = $subject;
    }
    
    /**
    * Assign extra headers
    * 
    * @param $headers
    */
    function extra_headers($headers)
    {
        $this->extra_headers = $headers;
    }
    
    /**
    * Set the template to use
    * 
    * @param $template Filename
    * @param $lang Language to use
    * @return bool
    */
    function set_template($template, $lang='')
    {
        global $eqdkp, $eqdkp_root_path;
        
        $lang = ( $lang == '' ) ? $eqdkp->config['default_lang'] : $lang;
        
        $this->tpl_file = $eqdkp_root_path . 'language/' . $lang . '/email/' . $template . '.txt';

        if ( !file_exists($this->tpl_file) )
        {
            message_die('Could not find email template file ' . $template);
        }

        if ( !$this->load_msg() )
        {
            message_die('Could not load email template file ' . $template);
        }

        return true;
    }
    
    /**
    * Load the message file
    * 
    * @return bool
    */
    function load_msg()
    {
        if ( $this->tpl_file == null )
        {
            message_die('No template file set');
        }

        if ( !($fd = fopen($this->tpl_file, 'r')) )
        {
            message_die('Failed opening template file');
        }

        $this->msg .= fread($fd, filesize($this->tpl_file));
        fclose($fd);

        return true;
    }
    
    /**
    * Assign template vars
    * 
    * @param $vars Array of variables
    */
    function assign_vars($vars)
    {
        $this->vars = ( empty($this->vars) ) ? $vars : $this->vars . $vars;
    }
    
    /**
    * Parse email; Replace vars with their values, find subject/charset
    * 
    * @return bool
    */
    function parse_email()
    {
        @reset($this->vars);
        while (list($key, $val) = @each($this->vars))
        {
            $$key = $val;
        }

        // Escape all quotes, else the eval will fail.
        $this->msg = str_replace ("'", "\'", $this->msg);
        $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg);

        eval("\$this->msg = '$this->msg';");

        //
        // We now try and pull a subject from the email body ... if it exists,
        // do this here because the subject may contain a variable
        //
        $match = array();
        preg_match("/^(Subject:(.*?)[\r\n]+?)?(Charset:(.*?)[\r\n]+?)?(.*?)$/is", $this->msg, $match);

        $this->msg = ( isset($match[5]) ) ? trim($match[5]) : '';
        $this->subject = ( $this->subject != '' ) ? $this->subject : trim($match[2]);
        $this->encoding = ( trim($match[4]) != '' ) ? trim($match[4]) : 'iso-8859-1';

        return true;
    }
    
    /**
    * mail() the email
    * 
    * @return bool
    function send()
    {
        global $phpEx, $phpbb_root_path;

        if ( $this->address == null )
        {
            message_die('No email address set');
        }

        if ( !$this->parse_email() )
        {
            return false;
        }

        // Add date and encoding type
        $universal_extra = "MIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . gmdate('D, d M Y H:i:s', time()) . " UT\n";
        $this->extra_headers = $universal_extra . $this->extra_headers;

        $result = @mail($this->address, $this->subject, $this->msg, $this->extra_headers);

        if ( !$result )
        {
            message_die('Failed sending email');
        }

        return true;
    }
    */
        function send()
    {
        global $phpEx, $phpbb_root_path;

        if ( $this->address == null )
        {
            message_die('No email address set');
        }

        if ( !$this->parse_email() )
        {
            return false;
        }

        // Add date and encoding type
        $universal_extra = "MIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . gmdate('D, d M Y H:i:s', time()) . " UT\n";
        $this->extra_headers = $universal_extra . $this->extra_headers;
        
//        $result = @mail($this->address, $this->subject, $this->msg, $this->extra_headers);
        require_once "Mail.php";
        $host = "localhost";
        $port = "25";
        $email_from = "Ancient Dominion DKP <eq@rebelhq.org>";
        $email_address = "eq@rebelhq.org";
        
        $headers = array ('From' => $email_from, 'To' => $this->address, 'Subject' => $this->subject, 'Reply-To' => $email_address);
        $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => false));
        $mail = $smtp->send($this->address, $headers, $this->msg);

        if (PEAR::isError($mail)) {
            message_die('Failed sending email');
        }
        
        return true;
    }
}
?>