Raz0r
19.06.2007, 22:48
Написал простенький класс для SMTP, не хочет робить, вернее робит, но только на Yandex.Ru.
<?php
class SMTP
{
# SMTP Hostname
var $Host = "localhost";
# SMTP Server port
var $Port = 25;
# SMTP endline
var $CRLF = "\r\n";
# User
var $Username = "";
# Password
var $Password = "";
# Connection var
var $Connection;
# Timeout
var $timeout = 30;
# Curr code
var $code;
# Debug
var $debug = 1;
# Charset
var $Charset = 'windows-1251';
# type
var $Type = 'html';
function Send($mail, $subject, $message) {
global $Config,$Lang,$Member;
$this->Connection = @fsockopen($this->Host, $this->Port, $errno, $errstr, $this->timeout);
if(empty($this->Connection)) {
return false;
}
$this->get_code();
// EHLO
fputs($this->Connection, "EHLO gopneg" . $this->CRLF);
$this->get_code();
if($this->code != 250) {
if($this->debug == 1) echo '[SMTP class] [Notice] [Server desn\'t accept <b>EHLO</b> message]';
fclose($this->Connection);
return false;
}
// puts the login attempt
fputs($this->Connection, "AUTH LOGIN" . $this->CRLF);
$this->get_code();
if($this->code != 334) {
if($this->debug == 1) echo '[SMTP class] [Notice] [Server desn\'t accept <b>AUTH LOGIN</b> message]';
fclose($this->Connection);
return false;
}
// puts the username
fputs($this->Connection, base64_encode($this->Username) . $this->CRLF);
$this->get_code();
if($this->code != 334) {
if($this->debug == 1) echo '[SMTP class] [Notice] [Wrong username specified]';
fclose($this->Connection);
return false;
}
// puts the password
fputs($this->Connection, base64_encode($this->Password) . $this->CRLF);
$this->get_code();
if($this->code != 235) {
if($this->debug == 1) echo '[SMTP class] [Notice] [Wrong password specified]';
fclose($this->Connection);
return false;
}
fputs($this->Connection,"MAIL FROM:<" . $вар_с_моим_email . ">" . $this->CRLF); // Smtp user
fputs($this->Connection,"RCPT TO:<" . $mail["addr"] . ">" . $this->CRLF); // Recipient
fputs($this->Connection,"DATA" . $this->CRLF); // START Message
fputs($this->Connection,"From: \"Превед!\" <" . $вар_с_моим_email . ">" . $this->CRLF); // HEADER (sender)
fputs($this->Connection,"To: " . $mail["to"] . " <" . $mail["addr"] . ">" . $this->CRLF); // HEADER (message recipient)
fputs($this->Connection,"Subject: ". $subject."" . $this->CRLF); // HEADER (message subject)
fputs($this->Connection,"Content-Type: text/" . $this->Type . "; charset=\"" . $this->Charset . "\"\r\n" . $this->CRLF); // HEADER (content-type and charset)
fputs($this->Connection, $message . $this->CRLF); // Message BODY
fputs($this->Connection,"." . $this->CRLF); // END message
$this->get_code();
if($this->code != 250)
{
if($this->debug == 1) echo '[SMTP class] [Fatal error] [Unrecognised error on message send stage] [#'.htmlspecialchars($this->code).']';
fclose($this->Connection);
return false;
} else {
// exit
if(!empty($this->Connection)) fclose($this->Connection);
// return true
return true;
}
}
function get_code() {
$reply = $this->get_lines();
$code = substr($reply,0,3);
return $this->code = intval($code);
}
function get_lines() {
$data = "";
while($str = fgets($this->Connection,515)) {
$data .= $str;
if(substr($str,3,1) == " ") { break; }
}
return $data;
}
}
?>
Входят данные такого типа:
$Mail = array(
"to" => "Дядя Вася", // Имя лола
"addr" => "vasyamail@mail.ru", // Мыло лола
);
$message = $smtp->Send($Mail,"хай васёк, пошли пидь водку!","Тут литр застоялсо уже, пошли выпьем!");
if($message == 1) { echo "message sended!"; } else { echo "message is not sended"; }
Где я туплю. Не предлагать юзать паблик скрипты, я хочу понять, в чем ошибка.
<?php
class SMTP
{
# SMTP Hostname
var $Host = "localhost";
# SMTP Server port
var $Port = 25;
# SMTP endline
var $CRLF = "\r\n";
# User
var $Username = "";
# Password
var $Password = "";
# Connection var
var $Connection;
# Timeout
var $timeout = 30;
# Curr code
var $code;
# Debug
var $debug = 1;
# Charset
var $Charset = 'windows-1251';
# type
var $Type = 'html';
function Send($mail, $subject, $message) {
global $Config,$Lang,$Member;
$this->Connection = @fsockopen($this->Host, $this->Port, $errno, $errstr, $this->timeout);
if(empty($this->Connection)) {
return false;
}
$this->get_code();
// EHLO
fputs($this->Connection, "EHLO gopneg" . $this->CRLF);
$this->get_code();
if($this->code != 250) {
if($this->debug == 1) echo '[SMTP class] [Notice] [Server desn\'t accept <b>EHLO</b> message]';
fclose($this->Connection);
return false;
}
// puts the login attempt
fputs($this->Connection, "AUTH LOGIN" . $this->CRLF);
$this->get_code();
if($this->code != 334) {
if($this->debug == 1) echo '[SMTP class] [Notice] [Server desn\'t accept <b>AUTH LOGIN</b> message]';
fclose($this->Connection);
return false;
}
// puts the username
fputs($this->Connection, base64_encode($this->Username) . $this->CRLF);
$this->get_code();
if($this->code != 334) {
if($this->debug == 1) echo '[SMTP class] [Notice] [Wrong username specified]';
fclose($this->Connection);
return false;
}
// puts the password
fputs($this->Connection, base64_encode($this->Password) . $this->CRLF);
$this->get_code();
if($this->code != 235) {
if($this->debug == 1) echo '[SMTP class] [Notice] [Wrong password specified]';
fclose($this->Connection);
return false;
}
fputs($this->Connection,"MAIL FROM:<" . $вар_с_моим_email . ">" . $this->CRLF); // Smtp user
fputs($this->Connection,"RCPT TO:<" . $mail["addr"] . ">" . $this->CRLF); // Recipient
fputs($this->Connection,"DATA" . $this->CRLF); // START Message
fputs($this->Connection,"From: \"Превед!\" <" . $вар_с_моим_email . ">" . $this->CRLF); // HEADER (sender)
fputs($this->Connection,"To: " . $mail["to"] . " <" . $mail["addr"] . ">" . $this->CRLF); // HEADER (message recipient)
fputs($this->Connection,"Subject: ". $subject."" . $this->CRLF); // HEADER (message subject)
fputs($this->Connection,"Content-Type: text/" . $this->Type . "; charset=\"" . $this->Charset . "\"\r\n" . $this->CRLF); // HEADER (content-type and charset)
fputs($this->Connection, $message . $this->CRLF); // Message BODY
fputs($this->Connection,"." . $this->CRLF); // END message
$this->get_code();
if($this->code != 250)
{
if($this->debug == 1) echo '[SMTP class] [Fatal error] [Unrecognised error on message send stage] [#'.htmlspecialchars($this->code).']';
fclose($this->Connection);
return false;
} else {
// exit
if(!empty($this->Connection)) fclose($this->Connection);
// return true
return true;
}
}
function get_code() {
$reply = $this->get_lines();
$code = substr($reply,0,3);
return $this->code = intval($code);
}
function get_lines() {
$data = "";
while($str = fgets($this->Connection,515)) {
$data .= $str;
if(substr($str,3,1) == " ") { break; }
}
return $data;
}
}
?>
Входят данные такого типа:
$Mail = array(
"to" => "Дядя Вася", // Имя лола
"addr" => "vasyamail@mail.ru", // Мыло лола
);
$message = $smtp->Send($Mail,"хай васёк, пошли пидь водку!","Тут литр застоялсо уже, пошли выпьем!");
if($message == 1) { echo "message sended!"; } else { echo "message is not sended"; }
Где я туплю. Не предлагать юзать паблик скрипты, я хочу понять, в чем ошибка.