Beste helpers,
Ik ben nogsteeds bezig met mijn smtp mailer en hij doet het prima alleen nu krijg ik dus alle mail die ik stuur in mn spambox van windows live...
hier staat dat je geen smtp code 500-600 mag krijgen alleen is dit ook nogteeds zo wanneer je smtp al hebt afgesloten...
verder vraag ik me ook af hoe ik nu het beste een smtp batch kan schrijven voor massa mail zodat een server niet overbelast raakt die bijv elke 5 minute 30 mails verstuurd
Mail output
Ik ben nogsteeds bezig met mijn smtp mailer en hij doet het prima alleen nu krijg ik dus alle mail die ik stuur in mn spambox van windows live...
hier staat dat je geen smtp code 500-600 mag krijgen alleen is dit ook nogteeds zo wanneer je smtp al hebt afgesloten...
verder vraag ik me ook af hoe ik nu het beste een smtp batch kan schrijven voor massa mail zodat een server niet overbelast raakt die bijv elke 5 minute 30 mails verstuurd
Mail output
Code:
/home/w***/domains/***.nl/public_html220 ***.nl ESMTP Exim 4.67 Thu, 15 Apr 2010 17:05:57 +0200
EHLO localhost
250-*** Hello localhost [127.0.0.1]
250-SIZE 20971520
250-ETRN
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
CONNECTION OK
MAIL FROM:<***@hotmail.com>
250 OK
RCPT TO:<***@hotmail.com>
250 Accepted
DATA
354 Enter message, ending with "." on a line by itself
To: Michael <***@hotmail.com>
From: Michael Beers <***@hotmail.com>
Reply-to: Michael <***@hotmail.com>
Subject: Dit is een test onderwerp
MIME-Version: 1.0
Content-type: multipart/alternative; boundary="#BOUNDARY#"
--#BOUNDARY#
Content-Type: text/html; charset=iso-8859-1
<b>Dit is een htmltekst</b>
--#BOUNDARY#
Content-Type: text/plain; charset=iso-8859-1
Dit is een plaintekst
.
250 OK id=1O2Qdh-0008I9-V2
QUIT
500 unrecognized command
PHP:
<?php
Class Mail
{
public $debugMode = false;
private $smtpServer;
private $smtpPort;
private $smtpSocket;
private $authUser = "";
private $authPass = "";
private $Recipients = array();
private $Attachments = array();
private $Headers = array();
private $replyMail;
private $replyName;
private $fromMail;
private $fromName;
private $Subject;
private $Plain;
private $Html;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BASIS SPUL
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Allowed Mimetypes
private $MimeTypes = array(
"png" => "image/png"
);
/*
** Basic constructor
** @param Server De naam van de smtp server
** @param Port De port van de smtp server
*/
public function __construct($Server=false, $Port=25)
{
define("EOL","\r\n");
define("MSG_END","\r\n.\r\n");
define("EOH","\r\n\r\n");
define("SEP","\r\n--#BOUNDARY#--");
define("EOM","QUIT\r\n\0");
$this->smtpServer = (($Server!==false) ? $Server : $_SERVER["LOCAL_ADDR"]);
$this->smtpPort = $Port;
}
/*
** Connector
** @param timeout Hoelang de Socket server er maximaal over mag doen
*/
public function connect($timeout=5)
{
$this->smtpSocket = @fsockopen($this->smtpServer, $this->smtpPort, $errno, $errstr, $timout);
//Check if connected
if ( !$this->smtpSocket )
{
$this->Debug("Cannot connect to the smtp server \r\n");
return false;
}
//Check if the buffer right
if( $this->Buffer() != 220 )
{
echo "Error Close Socket:";;
fclose($this->smtpSocket);
return false;
}
//Check if the login is right
if (($this->authUser == "" && $this->authPass == ""))
{
$cmd = sprintf("EHLO %s", "localhost");
} else {
$cmd = sprintf("HELO %s", "localhost");
}
$this->Command($cmd);
//Do Login
if ($this->Buffer() == 250)
{
return $this->authLogin();
}
if ( $this->Buffer() != 250)
{
echo "Error Close Socket:";
fclose($this->smtpSocket);
return false;
}
echo "Succesfully connected";
return true;
}
/*
** Verzenden van de mail
*/
public function send($connect=false,$disconnect=false)
{
if ($connect)
{
if (!$this->connect())
{
return false;
}
}
//Ontvanger,Verzender zetten
$cmd = sprintf("MAIL FROM:<%s>",$this->fromMail);
$this->Command($cmd);
if ($this->Buffer() != 250) { fclose($this->smtpSocket); return false; }
if (!$this->sendRecipient()) { fclose($this->smtpSocket); return false; }
//Dat verzenden
$cmd = "DATA";
$this->Command($cmd);
if($this->Buffer()!=354){ fclose($this->smtpSocket); return false; }
if (!$this->sendHeaders()) { fclose($this->smtpSocket); return false; }
if (!$this->sendContent()) { fclose($this->smtpSocket); return false; }
if (!$this->sendAttachments()) { fclose($this->smtpSocket); return false; }
$this->Command(MSG_END);
if ($this->Buffer() != 250) { fclose($this->smtpSocket); return false; }
}
/*
** Disconnect the smtp server
**/
public function disconnect()
{
$this->Command("QUIT");
$quitReply = $this->Buffer();
fclose($this->smtpSocket);
if($quitReply==221)
return true;
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PRIVATES VOOR DE CONNECTOR ETC.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Login to the smtp server
//
private function authLogin()
{
if (($this->authUser == "" && $this->authPass == ""))
{
return true;
}
$cmd = "AUTH LOGIN";
$this->Command($cmd);
if ($this->Buffer() != 334)
{
echo "Error Close Socket:";
fclose($this->smtpSocket);
return false;
}
$cmd = sprintf("%s", base64_encode($this->authUser));
$this->Command($cmd);
if ($this->Buffer() != 334)
{
echo "Error Close Socket:";
fclose($this->smtpSocket);
return false;
}
$cmd = sprintf("%s", base64_encode($this->authPass));
$this->Command($cmd);
if ($this->Buffer() != 220)
{
echo "Error Close Socket:";
fclose($this->smtpSocket);
return false;
}
return true;
}
//
// Send a new smtp command
//
private function Command($lstLines, $raw=0)
{
if ($raw){}
//Create array if its not created
if ( !is_array($lstLines) )
{
$lstLines = str_replace("\r", "", $lstLines);
$lstLines = explode("\n", $lstLines);
if ( !count($lstLines) )
{
$lstLines = Array($lstLines);
}
}
//Send the Command
foreach ($lstLines as $line)
{
$line = trim($line);
$this->Debug($line);
if ( !fputs($this->smtpSocket, $line."\r\n") )
{
return false;
}
}
return true;
}
//
// Check Buffer value
//
private function Buffer()
{
$line="";
while( !feof($this->smtpSocket) )
{
$ch = fgetc($this->smtpSocket);
if(!strlen($ch))
{
return false;
}
if($ch=="\n"){
$this->Debug($line,0);
if($line[3]==" ")
{
return (int)substr($line,0,3);
}
$line = ""; continue;
}
if($ch!="\r")
{
$line.=$ch;
}
}
return false;
}
//
// Debugger
//
private function Debug($err,$sent=1)
{
$err=trim($err);
if( !$this->debugMode )
{
return;
}
echo nl2br(htmlentities($err)."<br />");
}
//
// Adding Attachments
//
private function attachFile($path,$name,$mime)
{
if (@!filesize($path))
{
return false;
}
$this->Attachments[$name] = Array(
"Path" => $path,
"Mime" => $mime
);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SETTERS
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
** Recipient toevoegen
** @param name Naam van de ontvanger
** @param email Email van de ontvanger
** @param type Type van het verzenden (to,bcc,cc)
**/
public function addRecipient($name=null,$email=null,$type=null)
{
//Check if type is empty
if ( !isset($type) )
{
$type = "to";
} else {
$type = strtolower($type);
}
//type lowercase maken
$type = strtolower($type);
//check if there is a array in the recipients
if ( !is_array($this->Recipients[$type]) )
{
$this->Recipients[$type] = array();
}
$this->Recipients[$type][$email] = $name;
}
/*
** Attachment Toevoegen
** @param path Het pad naar het bestand
** @param name De Naam van het bestand
**/
public function addAttachment($path,$name)
{
$path = str_replace("\\","/",$path);
if (!isset($name))
{
$name = substr(strrchr($path,'/'),1);
}
$fileExtension = substr(strrchr($name,'.'),1);
if ( isset($this->MimeTypes[$fileExtension]))
{
$mimeType = $this->MimeTypes[$fileExtension];
return $this->attachFile($path,$name,$mimeType);
} else {
return false;
}
return false;
}
/*
** Header toevoegen
** @param name Naam van de header
** @param function Waarden in de header
**/
public function addHeader($name,$vale)
{
$this->Headers[$name] = $value;
}
/*
** De X-importance header
** @param importance Type: low,normal,high
**/
public function setImportance($importance){
$this->addHeader("X-Importance",$importance);
}
/*
** De Sensitivity header
** @param sensitivity Type: Personal,Private,Company-Confidential
**/
public function setSensitivity($sensitivity){
$this->addHeader("X-Sensitivity",$sensitivity,"X");
}
/*
** De priority
** @param priority Type: low,normal,hight
**/
public function setPriority($priority){
$this->addHeader("X-Priority",$priority);
}
/*
**
** @param authUser Gebruikersnaam van de smtp
** @param authPass Wachtwoord van de smtp
**/
public function setAuth($authUser,$authPass)
{
$this->authUser = $authUser;
$this->authPass = $authPass;
}
/*
** Setter van wie de mail moet komen
** @param name Naam van de persoon
** @param mail Email van de persoon
**/
public function setFrom($name,$mail)
{
$this->fromName = $name;
$this->fromMail = $mail;
}
/*
** Set het onderwerp van de mail
** @param subject Onderwerp van de mail
**/
public function setSubject($subject)
{
$this->Subject = $subject;
}
/*
** Naar welke mail de Reply moet
** @param name Naam van de antwoorder
** @param mail Email van de persoon
**/
public function setReply($name,$mail)
{
$this->replyName = $name;
$this->replyMail = $mail;
}
/*
** Berichten toevoegen
** @param plain Plain tekst voor de mail
** @param html Html tekst voor de mail
**/
public function setMessage($plain,$html)
{
$this->Plain = $plain;
$this->Html = $html;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SENDERS
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
** Send the data to the smtp server
**/
private function sendRecipient()
{
$res = 0;
$mails = array();
$mailsErr = array();
while( list($type,$list)=each($this->Recipients) )
{
while( list($mail,$name)=each($list) )
{
if (in_array($mail,$mails)){
continue;
}
$cmd = sprintf("RCPT TO:<%s>",$mail);
$this->Command($cmd);
array_push($mails,$mail);
if ($this->Buffer() == 250)
{
continue;
}
array_push($mailsErr, $mail);
unset($this->Recipients[$type][$mail]);
}
}
return ((count($mails)-count($mailsErr))>0);
}
/*
** Sending the header to the smtp server
**/
private function sendHeaders()
{
reset($this->Headers);
while( list($name,$value)=each($this->Headers) )
{
$cmd ="$name: $value";
$this->Command($cmd);
}
reset($this->Recipients);
while( list($type,$list)=each($this->Recipients) )
{
$mails = array();
while( list($mail,$name)=each($list) )
{
array_push($mails,"$name <$mail>");
}
$type[0] =strtoupper($type[0]);
if(isset($this->Headers[$type]))
{
continue;
}
$cmd ="$type: ".implode(",",$mails)."";
$this->Command($cmd);
}
$cmd = sprintf("From: %s <%s>", $this->fromName,$this->fromMail);
$this->Command($cmd);
if ( strlen($this->replyMail) )
{
$cmd = sprintf("Reply-to: %s <%s>",$this->replyName,$this->replyMail);
$this->Command($cmd);
}
$cmd = sprintf("Subject: %s", $this->Subject);
$this->Command($cmd);
return true;
}
/*
** Sending the content to the smtp server
*/
private function sendContent()
{
$cmd = "MIME-Version: 1.0\r\n";
$cmd .= "Content-type: multipart/alternative; boundary=\"#BOUNDARY#\"\r\n\r\n";
$this->Command($cmd);
//Html text
$cmd = "\r\n--#BOUNDARY#\r\n" . "Content-Type: text/html; charset=iso-8859-1\r\n";
$this->Command($cmd);
$this->Command($this->Html,1);
//Plain text
$cmd = "\r\n--#BOUNDARY#\r\n" . "Content-Type: text/plain; charset=iso-8859-1\r\n";
$this->Command($cmd);
$this->Command($this->Plain,1);
return true;
}
/*
** Sending the attachments to the smtp server
*/
private function sendAttachments()
{
if ( !count($this->Attachments) )
{
return true;
}
while ( list($name,$file)=each($this->Attachments) )
{
// $fpath = $file['Path'];
// $mime = $file['Mime'];
// $fname = $this->names[$i];
// $content = base64_encode($path);
// $cmd =
// sprintf("\r\n\r\n--#BOUNDARY#\r\n".
// "Content-Type: ".$mime."; name=%s\r\n".
// "Content-Length: ".filesize($fpath)."\r\n".
// "Content-Transfer-Encoding: base64\r\n".
// "Content-Disposition: attachment; filename=%s\r\n".
// "Content-ID: <%s>\r\n\r\n",
// $name,$name,$name);
// $this->Command($cmd);
// $this->Command($content,1);
}
return true;
}
}
?>