Envoi de mails avec SMTP problème avec caractères spéciaux

Eléphanteau du PHP | 38 Messages

30 juil. 2014, 21:50

Bonsoir,

Voilà depuis plusieurs jours je bloque sur un de mes scripts qui me permet d'envoyer des mails en smtp depuis mon site.
J'ai repris le script dans tous les sens, mais je pense avoir trouvé le problème.

Dès que je tente d'envoyer un mail avec des caractères spéciaux (û, à, ç etc.) le script ne m'envoi rien...

Je sais que le même message (corps du mail) sans ces caractères, passe...

Voici le script qui gère l'envoi :
class Mail{
    private $smtpServer = 'xxxx'; //     YOURSMTPHOST    Enter the smtp server
    private $port = '26';
    private $timeout = '45';
    private $uname = '[email protected]';             //	YourSiteName	 enter uname
    private $username = '[email protected]';             // 	YourEmail	enter username (smtp server)
    private $password = 'xxxx';             //	YourPassword enter password (smtp server)
    private $newline = "\r\n";
    private $localdomain = 'xxxx';          //enter localdomain.
    private $charset = 'utf-8';  //charser code
    private $contentTransferEncoding = false;

    // Do not change anything below
    private $smtpConnect = false;
    private $to = false;
    private $subject = false;
    private $message = false;
    private $format = 1;
    private $headers = false;
    private $logArray = array(); // Array response message for debug
    private $Error = '';

	//ajour de $webmasteremail, $nom_autorep
    public function __construct($to, $subject, $message, $format, $webmasteremail, $nom_autorep, $idmembre) {
        $this->to = &$to;
        $this->subject = &$subject;
        $this->message = &$message;
        echo "message : ".$this->message;
        $this->format = &$format;
	$this->webmasteremail = &$webmasteremail;
	$this->nom_autorep = &$nom_autorep;
	$this->idmembre = &$idmembre;
	$this->newmailmembre = $this->idmembre.'@xxxx';
        // 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("EHLO $this->localdomain");
        $this->logArray['EHLO'] = $this->readResponse();
        // Let's know each other
        $this->sendCommand('AUTH LOGIN');
        $this->logArray['AUTH_REQUEST'] = $this->readResponse();
        // My name...
        $this->sendCommand(base64_encode($this->username));
        $this->logArray['REQUEST_USER'] = $this->readResponse();
        // My password..
        $this->sendCommand(base64_encode($this->password));
        $this->logArray['REQUEST_PASSWD'] = $this->readResponse();
        // If error in response auth...
        if (substr($this->logArray['REQUEST_PASSWD'],0,3)!='235') {
            $this->Error .= 'Authorization error! '.$this->logArray['REQUEST_PASSWD'].$this->newline;
            return false;
        }
        // "From" mail...
        $this->sendCommand("MAIL FROM: $this->username");
        $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 H:i:s")." +0700");
		//changement $this->sendCommand("From: $this->uname <$this->username>"); en $this->sendCommand("From: $this->nom_autorep <$this->webmasteremail>");
        $this->sendCommand("From: $this->nom_autorep <$this->webmasteremail>");
		//changement $this->sendCommand("Reply-To: <$this->username>"); en $this->sendCommand("Reply-To: <$this->webmasteremail>");
        $this->sendCommand("Reply-To: <$this->webmasteremail>");
		//ajout $this->sendCommand("Sender: <$this->newmailmembre");
		$this->sendCommand("Sender: <$this->newmailmembre>");
        $this->sendCommand("To: <$this->to>");
        $this->sendCommand("Subject: $this->subject");
        $this->sendCommand("MIME-Version: 1.0");
if(($this->format)==1)
        $this->sendCommand("Content-Type: text/plain; charset=$this->charset");
else
        $this->sendCommand("Content-Type: text/html; charset=$this->charset");
        if ($this->contentTransferEncoding) $this->sendCommand("Content-Transfer-Encoding: $this->contentTransferEncoding");
        $this->sendCommand($this->newline);
//		print_r($this->to); 
        return ;
    }

    public function __destruct() {
        if (is_resource($this->smtpConnect)) fclose($this->smtpConnect);
    }


}
Voilà, c'est pas du code propre de chez propre, mais ça à le mérite de marcher (presque)...
Avez-vous une idée de là où je pourrais corrigé la machine pour que ça fonctionne parfaitement ?

Merci par avance pour votre aide.

Eléphanteau du PHP | 38 Messages

30 juil. 2014, 22:09

Re... après quelques tests c'est surtout le "^" qui semble ne pas passer (j'ai fais le test avec août).
Le ç et le à passe sans problème...

Merci pour votre aide.