Jump to content

why is fgets() required while sending mail with SMTP socket connection


avroo

Recommended Posts

Ok, so I am working on a script thats sends email using a socket connection to the SMTP server (with fsockopen).

 

It's not clear to me why the fgets() function is required for a good mail delivery of multiple emails. Example code:

 

fputs($connection, "MAIL FROM: $from\n");
$res=fgets($connection,256);
if(substr($res,0,3) != "250") echo "FROM error\n"; 

fputs($connection, "RCPT TO: $to\n");
$res=fgets($connection,256); 
if(substr($res,0,3) != "250") echo "RCPT TO error\n"; 

fputs($connection, "DATA\n");
$res=fgets($connection,256); 
if(substr($res,0,3) != "354") echo "Mail DATA error\n"; 

fputs($connection, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$res=fgets($connection,256); 
if(substr($res,0,3) != "250") echo "Mail $i SEND  error \n";}

 

THis works, but I don't want to get the server response. So I tried this:

 

fputs($connection, "MAIL FROM: $from\n");
fputs($connection, "RCPT TO: $to\n");
fputs($connection, "DATA\n");
fputs($connection, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");

 

This doesn't work though, so it looks like its required to get the server response, before sending the next command. Can anybody tell me why?

 

Another thing is, when I add one single fgets() after MAIL FROM command, but skip the rest, some emails are delivered, but other aren't (in case I loop it 10 times for example).

 

Example:

 

- for loop start-
fputs($connection, "MAIL FROM: $from\n");
fputs($connection, "RCPT TO: $to\n");
fputs($connection, "DATA\n");
fputs($connection, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
- for loop end -

 

In this case 7 out of 10 emails are sent. Hope some can help me out here, Its buggin me for days now. Thanks!

 

sorry, I meant:

 

- for loop start-

fputs($connection, "MAIL FROM: $from\n");

$res=fgets($connection,256);

fputs($connection, "RCPT TO: $to\n");

fputs($connection, "DATA\n");

fputs($connection, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");

- for loop end -

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.