在PHP中,我写了一个相对简单的邮件传递脚本。 这是实际交付部分:
private function SendMail($to, $subj) { if(!$this->smtp) { $this->OpenSMTPSock(); } fwrite($this->smtp, "MAIL FROM:<[email protected]>\r\n"); fwrite($this->smtp, "RCPT TO:<$to>\r\n"); fwrite($this->smtp, "DATA\r\n"); fwrite($this->smtp, "Received: from email.com by domain.com ; ".date("r")."\r\n"); fwrite($this->smtp, "Date: ".date("r")."\r\n"); fwrite($this->smtp, "From: ".$this->message["display_name"]." <[email protected]>\r\n"); fwrite($this->smtp, "Reply-to: ".$this->message["reply"]."\r\n"); fwrite($this->smtp, "Subject: $subj\r\n"); fwrite($this->smtp, "To: $to\r\n"); if($this->message["type"] == "H") { fwrite($this->smtp, "content-type: text/html; charset=`iso-8859-1`"); fwrite($this->smtp, "\r\n".$this->m_html."\r\n"); } if($this->message["type"] == "T") { fwrite($this->smtp, "\r\n".$this->m_text."\r\n"); } if($this->message["type"] == "B") { echo "Sending multi part message\r\n"; $boundary = "_----------=_10167391337129230"; fwrite($this->smtp, "content-type: multipart/alternative; boundary=\"$boundary\"\r\n"); $outMsg = "This is a multi-part message in MIME format.\r\n"; $outMsg .= "--$boundary\r\nContent-Type: text/plain; charset=\"US-ASCII\"\r\n\n"; $outMsg .= "Content-Transfer-Encoding: 7bit\r\n\n"; $outMsg .= $this->m_text; $outMsg .= "\r\n--$boundary\r\nContent-Type: text/html; charset=\"US-ASCII\"\r\n\n"; $outMsg .= $this->m_html; $outMsg .= "\r\n--$boundary--"; fwrite($this->smtp, "Message: $outMsg\r\n"); } fwrite($this->smtp, ".\r\n"); //This sends the message $this->Log("Message sent to $to"); }
这是打开套接字的位置:
private function OpenSMTPSock() { $this->Log("Attempting to open socket connection to SMTP host."); $this->smtp = fsockopen("localhost", 25, $errno, $errstr, 30); if(!$this->smtp) { $this->Log("Failed to connect to SMTP server The following was observed."); $this->Log("Fatal Error: $errno : $errstr"); exit; } else { $this->Log("Connected to SMTP socket."); fwrite($this->smtp, "HELO email.com\r\n"); } }
*析构函数closures套接字基本上我循环遍历收件人列表,并发送一个自定义的消息给每一个。 它完美的工作,直到它到达最后的消息,这不会被发送。
如果我将$ this-> smtp更改为fopen(“file”,“w”),则所有内容都按预期方式出现。 此外,如果我然后打开该文件并将内容转储到:telnet localhost 25所有消息正常。 另外,如果我在PHP脚本中打开“文件”,并将这些行写入到套接字中,则所有消息都将按照预期发送。
对于我的生活,我无法弄清楚这里有什么问题。 HALP!
你的析构函数是否发送QUIT命令? 我同意,如果一切都按照你想的方式发生,那么如果服务器返回“250 OK”(你最后一条消息之后是否在debugging这个消息?),邮件应该已经发送了。 尝试在“CRLF.CRLF”之后将其添加到您的代码中:
fwrite($this->smtp, "NOOP\r\n");
也许你的类与缓冲区有某种竞争条件,在缓冲区中的最终数据被发送之前,类正在closures连接。 向链中添加一个虚拟的NOOP可能会导致它将前面的数据从pipe道中清除。