MAIL.class.php

<?php
 
/******************************************************************************/
/*                                                                            */
/*     _  _ ____ _ ____ ____ _  _    ___  _  _    _    _ ___  ____ ____       */
/*     |\/| |__| | [__  |  | |\ |    |  \ |  |    |    | |__] |__/ |___       */
/*     |  | |  | | ___] |__| | \|    |__/ |__|    |___ | |__] |  \ |___       */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/* Titre          : MAIL.class.php                                            */
/*                                                                            */
/* URL            : http://mdl29.net                                          */
/* Auteur :       : Ivan Priorov                                              */
/* Modification   : Arnaud                                                    */
/* Date édition   : Novembre 2010                                             */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
 
 
 
 
 
class Mail {
 
    private $smtpServer = 'mail.mdl29.net';
 
    private $port = '25';
 
    private $timeout = '45';
 
   // private $from = 'maison-du-libre@mdl29.net';
 
    private $newline = "\r\n";
 
    private $localdomain = 'mail.mdl29.net';
 
    private $charset = 'utf-8';
 
    private $contentTransferEncoding = false;
 
 
 
    // Do not change anything below
 
    private $smtpConnect = false;
 
 
 
    private $from = false;
 
    private $to = false;
 
    private $subject = false;
 
    private $message = false;
 
    private $headers = false;
 
    private $logArray = array(); // Array response message for debug
 
    private $Error = '';
 
 
 
    public function __construct($from, $to, $subject, $message) {
 
        $this->from = &$from;
 
        $this->to = &$to;
 
        $this->subject = &$subject;
 
        $this->message = &$message;
 
        // Connect to server
 
        if(!$this->Connect2Server()) {
 
        	// Display error message
 
            echo $this->Error.$this->newline.'<!-- '.$this->newline;
 
            print_r($this->logArray);
 
            echo $this->newline.'-->'.$this->newline;
 
            return false;
 
        }
 
        return true;
 
    }
 
 
 
    private function Connect2Server() {
 
        // Connect to server
 
        $this->smtpConnect = fsockopen($this->smtpServer,$this->port,$errno,$error,$this->timeout);
 
        $this->logArray['CONNECT_RESPONSE'] = $this->readResponse();
 
 
 
        if (!is_resource($this->smtpConnect)) {
 
            return false;
 
        }
 
        $this->logArray['connection'] = "Connection accepted: $smtpResponse";
 
        // Hi, server!
 
        $this->sendCommand("HELO $this->localdomain");
 
        $this->logArray['HELO'] = $this->readResponse();
 
 
 
        // "From" mail...
 
        $this->sendCommand("MAIL FROM: $this->from");
 
        $this->logArray['MAIL_FROM_RESPONSE'] = $this->readResponse();
 
        if (substr($this->logArray['MAIL_FROM_RESPONSE'],0,3)!='250') {
 
            $this->Error .= 'Mistake in sender\'s address! '.$this->logArray['MAIL_FROM_RESPONSE'].$this->newline;
 
            return false;
 
        }
 
        // "To" address
 
        $this->sendCommand("RCPT TO: $this->to");
 
        $this->logArray['RCPT_TO_RESPONCE'] = $this->readResponse();
 
        if (substr($this->logArray['RCPT_TO_RESPONCE'],0,3)!='250') {
 
            $this->Error .= 'Mistake in reciepent address! '.$this->logArray['RCPT_TO_RESPONCE'].$this->newline;
 
        }
 
        // Send data to server
 
        $this->sendCommand('DATA');
 
        $this->logArray['DATA_RESPONSE'] = $this->readResponse();
 
        // Send mail message
 
        if (!$this->sendMail()) return false;
 
        // Good bye server! =)
 
        $this->sendCommand('QUIT');
 
        $this->logArray['QUIT_RESPONSE'] = $this->readResponse();
 
        // Close smtp connect 
 
        fclose($this->smtpConnect);
 
        return true;
 
    }
 
    // Function send mail
 
    private function sendMail() {
 
        $this->sendHeaders();
 
        $this->sendCommand($this->message);
 
        $this->sendCommand('.');
 
        $this->logArray['SEND_DATA_RESPONSE'] = $this->readResponse();
 
        if(substr($this->logArray['SEND_DATA_RESPONSE'],0,3)!='250') {
 
            $this->Error .= 'Mistake in sending data! '.$this->logArray['SEND_DATA_RESPONSE'].$this->newline;
 
            return false;
 
        }
 
        return true;
 
    }
 
    // Function read response
 
    private function readResponse() {
 
        $data="";
 
        while($str = fgets($this->smtpConnect,4096))
 
        {
 
            $data .= $str;
 
            if(substr($str,3,1) == " ") { break; }
 
        }
 
        return $data;
 
    }
 
    // function send command to server
 
    private function sendCommand($string) {
 
        fputs($this->smtpConnect,$string.$this->newline);
 
        return ;
 
    }
 
    // function send headers
 
    private function sendHeaders() {
 
        //$this->sendCommand("Date: ".date("D, j M Y G:i:s")." +0100");
 
        $this->sendCommand("From: <$this->from>");
 
        $this->sendCommand("Reply-To: <$this->from>");
 
        $this->sendCommand("To: <$this->to>");
 
        $this->sendCommand("Subject: $this->subject");
 
        $this->sendCommand("MIME-Version: 1.0");
 
        $this->sendCommand("Content-Type: text/html; charset=$this->charset");
 
        if ($this->contentTransferEncoding) $this->sendCommand("Content-Transfer-Encoding: $this->contentTransferEncoding");
 
        $this->sendCommand($this->newline);
 
        return ;
 
    }
 
 
    public function __destruct() {
 
        if (is_resource($this->smtpConnect)) fclose($this->smtpConnect);
 
    }
 
}
 
?>