I have a problem that's driving me nuts.... I'm trying to send mail via SMTP/sockets.
Some of the data I send to the socket repeats itself, and I have no idea why. "You are repeating something!", I hear you say, but I'm fairly sure I'm not. I've echoed what is sent to the SMTP server below.
Notice I've used NOOP once which returns OK, but in the socket response it comes up twice! What am I missing? The mail comes through fine, but the server gives up if you repeat this because of too many errors, so I can't email a bunch of people at once. I'm using Sendmail 8.9.3, PHP Version 4.0.1pl2, Linux, and I've used some code from 452Productions.com. I've even tested the orginal code, which does the same thing.

Any insight much appreciated...

// Commands that are sent...
EHLO SP www.test.com
MAIL FROM: <sender@test.com>
RCPT TO: <recipient@test.com>
NOOP
DATA
To: recipient@test.com
From: <sender@test.com>
X-Sender: <sender@test.com>
Reply-To: <sender@test.com>
X-Mailer: BSMailer
Subject: Test

Body text..

recipient@test.com

.
RSET
QUIT

// Response from server..
250 sender@test.com... Sender ok
250 recipient@test.com... Recipient ok
250 OK
354 Enter mail, end with "." on a line by itself
250 XXXXXXXX Message accepted for delivery

// Why repeat here!!?!?!?!

503 Need MAIL before RCPT
250 OK
503 Need MAIL command
500 Command unrecognized: "To: recipient@test.com"
500 Command unrecognized: "From: sender@test.com"
500 Command unrecognized: "X-Sender: sender@test.com"
500 Command unrecognized: " Reply-To: sender@test.com"
500 Command unrecognized: "Subject: Test"
500 Command unrecognized: ""
500 Command unrecognized: "Body text.."
500 Command unrecognized: ""
500 Command unrecognized: "recipient@test.com"
500 Command unrecognized: ""
500 Command unrecognized: "."
250 Reset state
221 www.test.com closing connection

// Code to open socket..
function open_socket($socket) {
global $smtp_id, $sockprint;
fputs($socket,"EHLO SP $smtp_id\r\n");
$sockprint .= "EHLO SP $smtp_id\r\n";
}

// Code to write to socket..
fputs($socket,"MAIL FROM: $from\r\n");
fputs($socket,"RCPT TO: <$uemail>\r\n");
fputs($socket,"NOOP\r\n");
fputs($socket,"DATA\r\n");
fputs($socket,"To: $uemail\r\n");
fputs($socket,"$header\r\n");
fputs($socket,"Subject: $subject\r\n");
fputs($socket,"\r\n");
fputs($socket,"$ubody\r\n");
fputs($socket,".\r\n");
fputs($socket, "RSET\r\n");

function close_socket_print($socket) {
global $sockprint;
fputs($socket, "QUIT\r\n");
$sockprint .= "QUIT\r\n";
while(!feof($socket)){
echo fgets($socket, "1024") . "<br>";
flush();
}
fclose($socket);
}

    You are violating SMTP protocol.
    Sending a command stringt in hope all be good is a bad idea.
    Read http://cr.yp.to/smtp.html and take a look into my sample
    SMTP implementation (a wrote this for experiments) at www.ima.pl/blizbor/smtp.phps. Call this script with a server=name.of.mail.server.

    Regards,
    Blizbor

      In other words, you need to read from the socket after every command, and see what the return code is, rather than waiting to the end and trying to read all the responses at once.

        Write a Reply...