I wrote a class to send mail by remote mail server.
But it was failed. So I captured the network packets by tcpdump,
and found that there is a strange packet "NOOP" was sent.
But in the source code I mean to send command "DATA".
I don't know why a "NOOP" packet was sent instead.
Here is my php source code and the attachment is tcpdump file.
---
<?php
/**
* sendmail class
*/
class sendmail
{
var $lastmessage; //last reply
var $lastact; //last action
var $welcome; //welcome info
var $smtp; //smtp server
var $port; //smtp port
var $fp; //socket handler
var $user; //smtp user account
var $pass; //smtp user passwd
/**
* Constructor
*/
function sendmail($welcome)
{
global $config;
$this->smtp=$config['smtp_svr'];
if(empty($welcome)){
$this->welcome=gethostbyaddr($_SERVER['REMOTE_ADDR']);
}else{
$this->welcome=$welcome;
}
$this->lastmessage="";
$this->lastact="";
$this->port=$config['smtp_port'];
$this->user = base64_encode($config['smtp_user']);
$this->pass = base64_encode($config['smtp_pass']);
}
/**
* Exe command such as HELO, MAIL FROM, RCPT TO, QUIT, DATA
* $code: reply code
*/
function do_command($command, $code)
{
$this->lastact=$command;
fputs ( $this->fp, $this->lastact );
$this->lastmessage = fgets ( $this->fp, 512 );
if(!ereg("^$code", $this->lastmessage)) {
return false;
} else {
return true;
}
}
/**
* Send mail
* $to: the mail address of receiver
* $subject:the subject of mail
* $message:mail content
* return: true -- successful
* false -- fail
*/
function send( $to,$subject,$message)
{
global $config;
$from=$config['smtp_from'];
/* Connect to server. */
$this->lastact="connect";
$this->show_debug("Connect to SMTP server :
".$this->smtp,"out");
$this->fp = fsockopen ( $this->smtp, $this->port );
if ( $this->fp ) {
set_socket_blocking( $this->fp, true );
$this->lastmessage=fgets($this->fp,512);
/* reply 220 means connect sucessfully */
if (! ereg ( "^220", $this->lastmessage ) ) {
return false;
} else {
$this->lastact="HELO " . $this->welcome . "\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
$this->lastact="AUTH LOGIN\r\n";
if(!$this->do_command($this->lastact, "334")){
fclose($this->fp);
return false;
}
$this->lastact=$this->user."\r\n";
if(!$this->do_command($this->lastact, "334")){
fclose($this->fp);
return false;
}
$this->lastact=$this->pass."\r\n";
if(!$this->do_command($this->lastact, "235")){
fclose($this->fp);
return false;
}
$this->lastact="MAIL FROM: $from" . "\n";
if(!$this->do_command($this->lastact, "250")) {
fclose($this->fp);
return false;
}
$this->lastact="RCPT TO: $to" . "\n";
if(!$this->do_command($this->lastact, "250")) {
fclose($this->fp);
return false;
}
/* Send data, I send a command "DATA", but
tcpdump capture a "NOOP" packet */
$this->lastact="DATA\n";
if(!$this->do_command($this->lastact, "354")) {
fclose($this->fp);
return false;
}
/* Insert Conten-Type */
$message = "Content-Transfer-Encoding:
8bit\n".$message;
$message = "Content-Type: text/plain;
charset=UTF-8\n".$message;
$message = "MIME-Version: 1.0\n".$message;
/* Deal with subject header */
$subject =
"=?UTF-8?B?".base64_encode($subject)."?=";
$head="Subject: $subject\n";
if(!empty($subject) && !ereg($head, $message)) {
$message = $head.$message;
}
/* Deal with from header */
$head="From: $from\n";
if(!empty($from) && !ereg($head, $message)) {
$message = $head.$message;
}
/* Deal with to header */
$head="To: $to\n";
if(!empty($to) && !ereg($head, $message)) {
$message = $head.$message;
}
/* Ending string */
if(!ereg("\n\.\n", $message))
$message .= "\n.\n";
fputs($this->fp, $message);
$this->lastact="QUIT\n";
if(!$this->do_command($this->lastact, "250")) {
fclose($this->fp);
return false;
}
}
return true;
} else {
$this->show_debug("Connect failed!", "in");
return false;
}
}
}
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php