Using some code I found online, I created my own library for sending SMTP messages so that I can send multiple emails per connection. Had it working using one SMTP server, but when I switched it to another, it drops the first email and sends the remainder. This one is baffling me....
$smtpConnect = fsockopen($server, $port, $errno,$errstr,45);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "HELO $smtpLocalhost". $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($this->smtpConnect,"AUTH LOGIN" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, base64_encode($uid) . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, base64_encode($pwd) . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
$mailHeader="From: ".$mailFrom.$crlf;
$multiDiv="--".md5(time());
$mailHeader.="MIME-Version: 1.0".$crlf;
$mailHeader.="Content-Type: multipart/mixed; boundary=\"".$multiDiv."\"".$crlf.$crlf;
$mailHeader.="This is a multi-part message in MIME format.".$crlf;
$mailHeader.=$multiDiv.$crlf;
$mailHeader.="Content-Type: text/html; charset=iso-8859-1".$crlf;
$mailHeader.="Content-Transfer-Encoding: 7bit".$crlf.$crlf;
$mailHeader.=$mailBody.$crlf.$crlf;
$fileName=$mailAttachName;
$fileBase=basename($mailAttachName);
$mailHeader.=$multiDiv.$crlf;
$mailHeader.="Content-Type: ".mime_content_type($fileBase)."; name=\"".$fileBase."\"".$crlf;
$mailHeader.="Content-Transfer-Encoding: base64".$crlf;
$mailHeader.="Content-Disposition: attachment; filename=\"".$fileBase."\"".$crlf.$crlf;
$mailHeader.=$mailAttachData.$crlf.$crlf;
$mailHeader.=$multiDiv."--".$crlf;
$to="abc@test.com";
fputs($smtpConnect, "MAIL FROM: <$mailFrom>" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "RCPT TO: <$to>" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "DATA" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "To: $to\r\nFrom: $mailFrom\r\nSubject: $mailSubj\r\n$mailHeader\r\n\r\n$mailBody\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$to="def@test.com";
fputs($smtpConnect, "MAIL FROM: <$mailFrom>" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "RCPT TO: <$to>" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "DATA" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "To: $to\r\nFrom: $mailFrom\r\nSubject: $mailSubj\r\n$mailHeader\r\n\r\n$mailBody\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$to="ghi@test.com";
fputs($smtpConnect, "MAIL FROM: <$mailFrom>" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "RCPT TO: <$to>" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "DATA" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect, "To: $to\r\nFrom: $mailFrom\r\nSubject: $mailSubj\r\n$mailHeader\r\n\r\n$mailBody\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
fputs($smtpConnect,"QUIT" . $crlf);
$smtpResponse = fgets($smtpConnect, 4096);
fclose($smtpConnect);
If I track all the $smtpResponse values... they all look good with proper response for each email. The only problem... abc@test.com never gets their email.
I tried doing some Google searches for SMTP help to see if I am missing something, a call, a parameter, etc... but trying to understand SMTP itself... that can be a nightmare. All what I am doing so far is using some PHP examples that I ran across.
I know there are classes and libraries out there will do this... but I also have heard problems with some of them. That's why we tried to work our own out.
Any suggestions?