我尝试使用SSL连接发送电子邮件,但它返回错误。 SSL连接已成功建立。 我使用fsockopen打开连接和fwrite写在上面。 请关注我的代码:
class smtp{
var $conn;
var $user;
var $pass;
var $debug;
function smtp($host, $port){
$this->conn = fsockopen($host, $port, $errno, $errstr, 10);
if ($this->conn){
$this->Put("HELO $host");
}else{
return false;
}
}
function Auth(){
$this->Put("AUTH LOGIN");
$this->Put(base64_encode($this->user));
$this->Put(base64_encode($this->pass));
}
function Send($to, $from, $subject, $msg, $nomeexibicao){
$this->Auth();
$this->Put("MAIL FROM: " . $from);
$this->Put("RCPT TO: " . $to);
$this->Put("DATA");
$this->Put($this->toHeader($to, $from, $subject, $nomeexibicao));
$this->Put("\r\n");
$this->Put($msg);
$this->Put(".");
$this->Close();
if(isset($this->conn)){
return true;
}else{
return false;
}
}
function Put($value){
if ($this->conn) return fwrite($this->conn, $value . "\r\n");
}
function toHeader($to, $from, $subject, $nomeexibicao){
$header = "Message-Id: <". date('YmdHis').".". md5(microtime()).".". strtoupper($from) ."> \r\n";
$header .= "From: ".$nomeexibicao ."<" . $from . "> \r\n";
$header .= "To: <".$to."> \r\n";
$header .= "Subject: ".$subject." \r\n";
$header .= "Date: ". date('D, d M Y H:i:s O') ." \r\n";
$header .= "X-MSMail-Priority: Normal \r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-Type: Text/HTML; charset=utf-8 \r\n";
$header .= "X-MimeOLE: By Huawei \r\n";
$header .= "Return-Path: $from\r\n";
return $header;
}
function Close(){
$this->Put("QUIT");
if($this->debug == true){
while (!feof ($this->conn)) {
fgets($this->conn) . "<br>\n";
}
}
return fclose($this->conn);
}
}
$host = "ssl://p3plcpnl0808.prod.phx3.secureserver.net";
$port = 465;
$smtp = new smtp($host,$port);
$smtp->user = "myemail@server.com";
$smtp->pass = "my_password";
$destinatario = "to_email@server.com";
$remetente = "from_email@server.com";
$assunto = "email title";
$mensagem = "email message";
$nome_exibicao = "My name";
$smtp->Send($destinatario, $remetente, $assunto, $mensagem, $nome_exibicao);
当我运行它时,我收到以下错误:
Warning: fwrite(): 12 is not a valid stream resource in C:\xampp\htdocs\libs\class.smtp.php on line 45
Warning: fwrite(): 12 is not a valid stream resource in C:\xampp\htdocs\libs\class.smtp.php on line 45
Warning: fwrite(): 12 is not a valid stream resource in C:\xampp\htdocs\libs\class.smtp.php on line 45
...
SSL连接稳定并生成资源连接。我不明白为什么我连接资源存在时收到无效的流资源。