avroo Posted December 13, 2009 Share Posted December 13, 2009 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! Link to comment https://forums.phpfreaks.com/topic/184987-why-is-fgets-required-while-sending-mail-with-smtp-socket-connection/ Share on other sites More sharing options...
avroo Posted December 13, 2009 Author Share Posted December 13, 2009 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 - Link to comment https://forums.phpfreaks.com/topic/184987-why-is-fgets-required-while-sending-mail-with-smtp-socket-connection/#findComment-976542 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.