以前寫了一個發email的class,一直都用著挺好的,可是今天再用突然發現不能用了,發信的時候發不出去,也不報錯,不知道哪裡錯了,請幫忙看一下
<?php
//$smtp = new smtp("smtp.163.com", 25, true, "email用戶名", "email密碼");//true是表示使用身份验证
//$smtp->debug = False;//调试信息
//$Se=$smtp->sendmail("收信人email","[email protected]","易居網用戶注冊激活信函","郵件正文","HTML");
//IF($Se){Echo 'ok';}set_time_limit(120);
Class smtp{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
Function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass){
$this->debug = false;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
$this->auth = $auth; //auth
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";
$this->sock = false;
} /* Main Function */
Function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = ""){
$mail_from = $this->get_address($this->strip_comment($from));
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
$header = "MIME-Version:1.0\r\nContent-Type:text/html;charset=\"UTF-8\"\r\n";
IF($mailtype == "HTML"){
$header .= "Content-Type:text/html; charset=\"UTF-8\"\r\n";
}
$header .= "To: " . $to . "\r\n";
IF($cc != ""){$header .= "Cc: " . $cc . "\r\n";}
$header .= "From: $from<" . $from . ">\r\n";
$header .= "Subject: " . $subject . "\r\n";
$header .= $additional_headers;
$header .= "Date: " . date("r") . "\r\n";
$header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
$TO = explode(",", $this->strip_comment($to));
IF($cc != ""){$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));}
IF($bcc != ""){$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));}
$sent = true;
Foreach($TO as $rcpt_to){
$rcpt_to = $this->get_address($rcpt_to);
IF(!$this->smtp_sockopen($rcpt_to)){
    $this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");
    $sent = false;
    continue;
}
IF($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)){
    $this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
} Else{
    $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");
    $sent = false;
}
Fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
Return $sent;
} /* Private Functions */
Function smtp_send($helo, $from, $to, $header, $body = ""){
IF(!$this->smtp_putcmd("HELO", $helo)){
Return $this->smtp_error("sending HELO command");
}
// auth
IF($this->auth){
IF(!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))){
    Return $this->smtp_error("sending HELO command");
}
IF(!$this->smtp_putcmd("", base64_encode($this->pass))){
    Return $this->smtp_error("sending HELO command");
}
}
IF(!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")){
Return $this->smtp_error("sending MAIL FROM command");
}
IF(!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")){
Return $this->smtp_error("sending RCPT TO command");
}
IF(!$this->smtp_putcmd("DATA")){
Return $this->smtp_error("sending DATA command");
}
IF(!$this->smtp_message($header, $body)){
Return $this->smtp_error("sending message");
}
IF(!$this->smtp_eom()){
Return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
}
IF(!$this->smtp_putcmd("QUIT")){
Return $this->smtp_error("sending QUIT command");
}
Return true;
} Function smtp_sockopen($address){
IF($this->relay_host == ""){
Return $this->smtp_sockopen_mx($address);
} Else{
Return $this->smtp_sockopen_relay();
}
} Function smtp_sockopen_relay(){
$this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
IF(!($this->sock && $this->smtp_ok())){
$this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");
$this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
Return false;
}
$this->log_write("Connected to relay host " . $this->relay_host . "\n");
Return true;;
} Function smtp_sockopen_mx($address){
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
IF(!@getmxrr($domain, $MXHOSTS)){
$this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");
Return false;
}
Foreach($MXHOSTS as $host){
$this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
IF(!($this->sock && $this->smtp_ok())){
    $this->log_write("Warning: Cannot connect to mx host " . $host . "\n");
    $this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
    continue;
}
$this->log_write("Connected to mx host " . $host . "\n");
Return true;
}
$this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");
Return false;
} Function smtp_message($header, $body){
Fputs($this->sock, $header . "\r\n" . $body);
$this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));
Return true;
} Function smtp_eom(){
Fputs($this->sock, "\r\n.\r\n");
$this->smtp_debug(". [EOM]\n");
Return $this->smtp_ok();
} Function smtp_ok(){
$response = str_replace("\r\n", "", fgets($this->sock, 512));
$this->smtp_debug($response . "\n");
IF(!ereg("^[23]", $response)){
Fputs($this->sock, "QUIT\r\n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host Returned \"" . $response . "\"\n");
Return false;
}
Return true;
} Function smtp_putcmd($cmd, $arg = ""){
IF($arg != ""){
IF($cmd == "") $cmd = $arg;
Else $cmd = $cmd . " " . $arg;
}
Fputs($this->sock, $cmd . "\r\n");
$this->smtp_debug("> " . $cmd . "\n");
Return $this->smtp_ok();
} Function smtp_error($string){
$this->log_write("Error: Error occurred while " . $string . ".\n");
Return false;
} Function log_write($message){
$this->smtp_debug($message);
IF($this->log_file == ""){
Return true;
}
$message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
IF(!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))){
$this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");
Return false;;
}
Flock($fp, LOCK_EX);
Fputs($fp, $message);
Fclose($fp);
Return true;
} Function strip_comment($address){
$comment = "\([^()]*\)";
while (ereg($comment, $address)){
$address = ereg_replace($comment, "", $address);
}
Return $address;
} Function get_address($address){
$address = ereg_replace("([ \t\r\n])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
Return $address;
} Function smtp_debug($message){
IF($this->debug){
echo $message . ";";
}
}
}
?>

解决方案 »

  1.   

    問題不是在此,因為我從http://www.phpclasses.org/下載了好多類結果都是如此,都是不報錯,也發送不成功.
      

  2.   

    這是錯誤信息
    > Content-Type:text/html;charset="UTF-8"
    > Content-Type:text/html; charset="UTF-8"
    > To: [email protected]
    > From: [email protected]<[email protected]>
    > Subject: =?UTF-8?B?5piT5bGF57ay55So5oi25rOo5YaK5r+A5rS75L+h5Ye9?=
    > Date: Fri, 27 May 2011 13:25:30 +0000
    > X-Mailer:By Redhat (PHP/5.2.6)
    > Message-ID: <[email protected]>

    > 郵件正文
    > ;. [EOM]
    ;554 DT:SPM smtp3, DdGowKD7mHjMyNNN4mHeAA--.412S2 1305725135 http://mail.163.com/help/help_spam_16.htm?ip=220.138.97.85&hostid=smtp3&time=1305725135
    ;Error: Remote host Returned "554 DT:SPM smtp3, DdGowKD7mHjMyNNN4mHeAA--.412S2 1305725135 http://mail.163.com/help/help_spam_16.htm?ip=220.138.97.85&hostid=smtp3&time=1305725135"
    ;Error: Error occurred while sending <CR><LF>.<CR><LF> [EOM].
    ;Error: Cannot send email to <[email protected]>;Disconnected from remote host
      

  3.   

    http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html
      

  4.   


    有這個可能,問題出在smtp上,但是並非發送病毒或垃圾郵件,不管我發什麼內容都不成功
    另外我測試了socket,很正常,而我這個類就是使用socket發郵件.所以我判斷問題出在smtp服務器上解決方法我已經找到了,使用SSL和gmail配合發送,下邊是解決方法:
    http://hi.baidu.com/see7di/blog/item/80119f543d425e083b293506.html