<?php
class smtpconnection
{
private $host;
private $authentication;
private $username;
private $password;
private $ssl;
public function __construct($host,$authentication=false,$username="",$password="",$ssl=false)
{
$this->host=$host;
$this->authentication=$authentication;
$this->username=$username;
$this->password=$password;
$this->ssl=$ssl;
}
public function sendmail($mail)
{
if (isset($_SERVER['SERVER_NAME']))
{
$hostname=$_SERVER['SERVER_NAME'];
}
elseif (isset($_SERVER['HOSTNAME']))
{
$hostname=$_SERVER['HOSTNAME'];
}
else
{
$hostname="PHP/ReptilerSMPTClass";
}
$startpos=strpos($mail,"From: ")+6;
$endpos=strpos($mail,"\r\n",$startpos);
$from=substr($mail,$startpos,$endpos-$startpos);
$startpos=strpos($mail,"To: ")+4;
$endpos=strpos($mail,"\r\n",$startpos);
$to=substr($mail,$startpos,$endpos-$startpos);
$size=strlen($mail);
if ($this->ssl==true)
{
$connection=@fsockopen("ssl://".$this->host,465);
}
else
{
$connection=@fsockopen($this->host,25);
}
if ($connection==false)
{
return false;
}
stream_set_timeout($connection,0,50000);
fgets($connection);
fwrite($connection,'EHLO '.$hostname."\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=2)
{
fclose($connection);
return false;
}
while (fgets($connection)!=false)
{
}
if ($this->authentication!=false)
{
fwrite($connection,'AUTH '.strtoupper($this->authentication)."\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=3)
{
fclose($connection);
return false;
}
if (strtoupper($this->authentication)=="LOGIN")
{
fwrite($connection,base64_encode($this->username)."\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=3)
{
fclose($connection);
return false;
}
fwrite($connection,base64_encode($this->password)."\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=2)
{
fclose($connection);
return false;
}
}
elseif (strtoupper($this->authentication)=="PLAIN")
{
fwrite($connection,base64_encode($this->username.chr(0).$this->username.chr(0).$this->password)."\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=2)
{
fclose($connection);
return false;
}
}
else
{
fclose($connection);
return false;
}
}
fwrite($connection,'MAIL FROM:<'.$from.'> SIZE='.$size."\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=2)
{
fclose($connection);
return false;
}
fwrite($connection,'RCPT TO:<'.$to.">\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=2)
{
fclose($connection);
return false;
}
fwrite($connection,"DATA\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=3)
{
fclose($connection);
return false;
}
fwrite($connection,$mail."\r\n.\r\n");
$response=fgets($connection);
if (substr($response,0,1)!=2)
{
fclose($connection);
return false;
}
fwrite($connection,"QUIT\r\n");
fgets($connection);
fclose($connection);
return true;
}
}
?>