Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / login / email / smtp.php
blob5f0c2fd4f81dfe67f0975abc405d210cd906666c
1 <?php
2 /**
3 * Filename.......: class.smtp.inc
4 * Project........: SMTP Class
5 * Version........: 1.0.5
6 * Last Modified..: 21 December 2001
7 */
9 define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
10 define('SMTP_STATUS_CONNECTED', 2, TRUE);
12 class smtp{
14 var $authenticated;
15 var $connection;
16 var $recipients;
17 var $headers;
18 var $timeout;
19 var $errors;
20 var $status;
21 var $body;
22 var $from;
23 var $host;
24 var $port;
25 var $helo;
26 var $auth;
27 var $user;
28 var $pass;
30 /**
31 * Constructor function. Arguments:
32 * $params - An assoc array of parameters:
34 * host - The hostname of the smtp server Default: localhost
35 * port - The port the smtp server runs on Default: 25
36 * helo - What to send as the HELO command Default: localhost
37 * (typically the hostname of the
38 * machine this script runs on)
39 * auth - Whether to use basic authentication Default: FALSE
40 * user - Username for authentication Default: <blank>
41 * pass - Password for authentication Default: <blank>
42 * timeout - The timeout in seconds for the call Default: 5
43 * to fsockopen()
46 function smtp($params = array()){
48 if(!defined('CRLF'))
49 define('CRLF', "\r\n", TRUE);
51 $this->authenticated = FALSE;
52 $this->timeout = 5;
53 $this->status = SMTP_STATUS_NOT_CONNECTED;
54 $this->host = 'localhost';
55 $this->port = 25;
56 $this->helo = 'localhost';
57 $this->auth = FALSE;
58 $this->user = '';
59 $this->pass = '';
60 $this->errors = array();
62 foreach($params as $key => $value){
63 $this->$key = $value;
67 /**
68 * Connect function. This will, when called
69 * statically, create a new smtp object,
70 * call the connect function (ie this function)
71 * and return it. When not called statically,
72 * it will connect to the server and send
73 * the HELO command.
76 function &connect($params = array()){
78 if(!isset($this->status)){
79 $obj = new smtp($params);
80 if($obj->connect()){
81 $obj->status = SMTP_STATUS_CONNECTED;
84 return $obj;
86 }else{
87 $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
88 if(function_exists('socket_set_timeout')){
89 @socket_set_timeout($this->connection, 5, 0);
92 $greeting = $this->get_data();
93 if(is_resource($this->connection)){
94 return $this->auth ? $this->ehlo() : $this->helo();
95 }else{
96 $this->errors[] = 'Failed to connect to server: '.$errstr;
97 return FALSE;
103 * Function which handles sending the mail.
104 * Arguments:
105 * $params - Optional assoc array of parameters.
106 * Can contain:
107 * recipients - Indexed array of recipients
108 * from - The from address. (used in MAIL FROM:),
109 * this will be the return path
110 * headers - Indexed array of headers, one header per array entry
111 * body - The body of the email
112 * It can also contain any of the parameters from the connect()
113 * function
116 function send($params = array()){
118 foreach($params as $key => $value){
119 $this->set($key, $value);
122 if($this->is_connected()){
124 // Do we auth or not? Note the distinction between the auth variable and auth() function
125 if($this->auth AND !$this->authenticated){
126 if(!$this->auth())
127 return FALSE;
130 $this->mail($this->from);
131 if(is_array($this->recipients))
132 foreach($this->recipients as $value)
133 $this->rcpt($value);
134 else
135 $this->rcpt($this->recipients);
137 if(!$this->data())
138 return FALSE;
140 // Transparency
141 $headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
142 $body = str_replace(CRLF.'.', CRLF.'..', $this->body);
143 $body = $body[0] == '.' ? '.'.$body : $body;
145 $this->send_data($headers);
146 $this->send_data('');
147 $this->send_data($body);
148 $this->send_data('.');
150 $result = (substr(trim($this->get_data()), 0, 3) === '250');
151 //$this->rset();
152 return $result;
153 }else{
154 $this->errors[] = 'Not connected!';
155 return FALSE;
160 * Function to implement HELO cmd
163 function helo(){
164 if(is_resource($this->connection)
165 AND $this->send_data('HELO '.$this->helo)
166 AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
168 return TRUE;
170 }else{
171 $this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
172 return FALSE;
177 * Function to implement EHLO cmd
180 function ehlo(){
181 if(is_resource($this->connection)
182 AND $this->send_data('EHLO '.$this->helo)
183 AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
185 return TRUE;
187 }else{
188 $this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
189 return FALSE;
194 * Function to implement RSET cmd
197 function rset(){
198 if(is_resource($this->connection)
199 AND $this->send_data('RSET')
200 AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
202 return TRUE;
204 }else{
205 $this->errors[] = 'RSET command failed, output: ' . trim(substr(trim($error),3));
206 return FALSE;
211 * Function to implement QUIT cmd
214 function quit(){
215 if(is_resource($this->connection)
216 AND $this->send_data('QUIT')
217 AND substr(trim($error = $this->get_data()), 0, 3) === '221' ){
219 fclose($this->connection);
220 $this->status = SMTP_STATUS_NOT_CONNECTED;
221 return TRUE;
223 }else{
224 $this->errors[] = 'QUIT command failed, output: ' . trim(substr(trim($error),3));
225 return FALSE;
230 * Function to implement AUTH cmd
233 function auth(){
234 if(is_resource($this->connection)
235 AND $this->send_data('AUTH LOGIN')
236 AND substr(trim($error = $this->get_data()), 0, 3) === '334'
237 AND $this->send_data(base64_encode($this->user)) // Send username
238 AND substr(trim($error = $this->get_data()),0,3) === '334'
239 AND $this->send_data(base64_encode($this->pass)) // Send password
240 AND substr(trim($error = $this->get_data()),0,3) === '235' ){
242 $this->authenticated = TRUE;
243 return TRUE;
245 }else{
246 $this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
247 return FALSE;
252 * Function that handles the MAIL FROM: cmd
255 function mail($from){
257 if($this->is_connected()
258 AND $this->send_data('MAIL FROM:<'.$from.'>')
259 AND substr(trim($this->get_data()), 0, 2) === '250' ){
261 return TRUE;
263 }else
264 return FALSE;
268 * Function that handles the RCPT TO: cmd
271 function rcpt($to){
273 if($this->is_connected()
274 AND $this->send_data('RCPT TO:<'.$to.'>')
275 AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){
277 return TRUE;
279 }else{
280 $this->errors[] = trim(substr(trim($error), 3));
281 return FALSE;
286 * Function that sends the DATA cmd
289 function data(){
291 if($this->is_connected()
292 AND $this->send_data('DATA')
293 AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
295 return TRUE;
297 }else{
298 $this->errors[] = trim(substr(trim($error), 3));
299 return FALSE;
304 * Function to determine if this object
305 * is connected to the server or not.
308 function is_connected(){
310 return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
314 * Function to send a bit of data
317 function send_data($data){
319 if(is_resource($this->connection)){
320 return fwrite($this->connection, $data.CRLF, strlen($data)+2);
322 }else
323 return FALSE;
327 * Function to get data.
330 function &get_data(){
332 $return = '';
333 $line = '';
334 $loops = 0;
336 if(is_resource($this->connection)){
337 while((strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' ') AND $loops < 100){
338 $line = fgets($this->connection, 512);
339 $return .= $line;
340 $loops++;
342 return $return;
344 }else
345 return FALSE;
349 * Sets a variable
352 function set($var, $value){
354 $this->$var = $value;
355 return TRUE;
358 } // End of class