2014-02-21 108 views
0

這是一個簡單的代碼,用於通過用戶身份驗證發送SMTP郵件來發送郵件。該代碼適用於To:但密件抄送:不起作用。當我們使用正常的PHP郵件()函數時,我們在郵件頭中包含密件抄送:。但我不確定如何使用密件抄送:這裏使用SMTP協議。SMTP Bcc郵件不能正常工作

$SmtpServer="127.0.0.1"; 
$SmtpPort="25"; //default 
$SmtpUser="username"; 
$SmtpPass="password"; 

class SMTPClient 
{ 

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body, $bcc) 
{ 

$this->SmtpServer = $SmtpServer; 
$this->SmtpUser = base64_encode ($SmtpUser); 
$this->SmtpPass = base64_encode ($SmtpPass); 
$this->from = $from; 
$this->to = $to; 
$this->subject = $subject; 
$this->body = $body; 

if ($SmtpPort == "") 
{ 
$this->PortSMTP = 25; 
} 
else 
{ 
$this->PortSMTP = $SmtpPort; 
} 
} 

function SendMail() 
{ 
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) 
{ 
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n"); 
$talk["hello"] = fgets ($SMTPIN, 1024); 
fputs($SMTPIN, "auth login\r\n"); 
$talk["res"]=fgets($SMTPIN,1024); 
fputs($SMTPIN, $this->SmtpUser."\r\n"); 
$talk["user"]=fgets($SMTPIN,1024); 
fputs($SMTPIN, $this->SmtpPass."\r\n"); 
$talk["pass"]=fgets($SMTPIN,256); 
fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n"); 
$talk["From"] = fgets ($SMTPIN, 1024); 
fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n"); 
$talk["To"] = fgets ($SMTPIN, 1024); 
fputs($SMTPIN, "DATA\r\n"); 
$talk["data"]=fgets($SMTPIN,1024); 
fputs($SMTPIN, "To: <".$this->to.">\r\nBcc: ".$bcc."\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n"); 
$talk["send"]=fgets($SMTPIN,256); 
//CLOSE CONNECTION AND EXIT ... 
fputs ($SMTPIN, "QUIT\r\n"); 
fclose($SMTPIN); 
// 
} 
return $talk; 
} 
} 

$to = '[email protected]'; 
$from = '[email protected]'; 
$subject = 'test subject'; 
$body = 'body'; 
$bcc = '[email protected], [email protected], [email protected]'; 
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body, $bcc); 
$SMTPChat = $SMTPMail->SendMail(); 

以上代碼

+0

你以前檢查過這個嗎? http://stackoverflow.com/questions/16830673/wamp-send-mail-using-smtp-localhost – jycr753

+0

嘗試使用PHPMailer庫:https://github.com/Synchro/PHPMailer –

回答

0

將BCC中不會忽略自己的 「RCPT TO:」 SMTP命令,刪除密件抄送:標頭。

無論如何,不​​要取笑SMTP回覆代碼是火腿(而不是垃圾郵件)發送者的麻煩祕訣。