3 * Filename.......: class.smtp.inc
4 * Project........: SMTP Class
5 * Version........: 1.0.5
6 * Last Modified..: 21 December 2001
9 define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
10 define('SMTP_STATUS_CONNECTED', 2, TRUE);
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
46 function smtp($params = array()){
49 define('CRLF', "\r\n", TRUE);
51 $this->authenticated
= FALSE;
53 $this->status
= SMTP_STATUS_NOT_CONNECTED
;
54 $this->host
= 'localhost';
56 $this->helo
= 'localhost';
60 $this->errors
= array();
62 foreach($params as $key => $value){
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
76 function &connect($params = array()){
78 if(!isset($this->status
)){
79 $obj = new smtp($params);
81 $obj->status
= SMTP_STATUS_CONNECTED
;
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();
96 $this->errors
[] = 'Failed to connect to server: '.$errstr;
103 * Function which handles sending the mail.
105 * $params - Optional assoc array of parameters.
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()
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
){
130 $this->mail($this->from
);
131 if(is_array($this->recipients
))
132 foreach($this->recipients
as $value)
135 $this->rcpt($this->recipients
);
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');
154 $this->errors
[] = 'Not connected!';
160 * Function to implement HELO cmd
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' ){
171 $this->errors
[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
177 * Function to implement EHLO cmd
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' ){
188 $this->errors
[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
194 * Function to implement RSET cmd
198 if(is_resource($this->connection
)
199 AND $this->send_data('RSET')
200 AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
205 $this->errors
[] = 'RSET command failed, output: ' . trim(substr(trim($error),3));
211 * Function to implement QUIT cmd
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
;
224 $this->errors
[] = 'QUIT command failed, output: ' . trim(substr(trim($error),3));
230 * Function to implement AUTH cmd
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;
246 $this->errors
[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
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' ){
268 * Function that handles the RCPT TO: cmd
273 if($this->is_connected()
274 AND $this->send_data('RCPT TO:<'.$to.'>')
275 AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){
280 $this->errors
[] = trim(substr(trim($error), 3));
286 * Function that sends the DATA cmd
291 if($this->is_connected()
292 AND $this->send_data('DATA')
293 AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
298 $this->errors
[] = trim(substr(trim($error), 3));
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);
327 * Function to get data.
330 function &get_data(){
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);
352 function set($var, $value){
354 $this->$var = $value;