SMTP协议建立在TCP协议之上,所以原则上按照SMTP协议的规范,使用Socket跟SMTP服务器进行交互。使用fsockopen()函数代替socket_*()类函数。
fsockopen()函数的好处是把Socket连接绑定到一个流上,然后使用各种操作流的函数操作这个Socket连接。
使用fsockopen()函数创建的Socket连接句柄可以提供给诸如fgets()、fputs()、fwrite()、fread()、fclose()等流函数使用。
示例代码如下:
<?php
class smtp_mail{
private $host;
private $port = 25;
private $user;
private $pass;
private $debug = false;
private $sock;
private $mail_format = 0;
public function __construct($host, $port, $user, $pass, $format=1, $debug=0){
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user);
$this->pass = base64_encode($pass);
$this->mail_format = $format;
$this->debug = $debug;
$this->sock = fsockopen($this->host, $this->port, & $errno, & $errstr, 10);
if (!$this->sock){
exit("Error number:{$errno}, Error message:{$errstr}\n");
}
$response = fgets($this->sock);
if (strstr($response, "220") === false) {
exit("server error: {$response}\n");
}
}
private function show_debug($message){
if ($this->debug){
echo "<p>Debug:{$message}</p>\n";
}
}
private function do_command($cmd, $return_code){
fwrite($this->sock, $cmd);
$response = fgets($this->sock);
if (strstr($response, "$return_code") === false){
$this->show_debug($response);
return false;
}
return true;
}
private function is_email($email){
$pattren = "/^[^_][\w]*@[\w.]+[\w]*[^_]$/";
if (preg_match($pattren, $email, $matches)){
return true;
} else {
return false;
}
}
public function send_mail($from, $to, $subject, $body){
if (!$this->is_email($from) OR !$this->is_email($to)){
$this->show_debug("Please enter vaild from/to email.");
return false;
}
if (empty($subject) OR empty($body)){
$this->show_debug("Please enter subject/content.");
return false;
}
$detail = "From:".$from."\r\n";
$detail .= "To:".$to."\r\n";
$detail .= "Subject:".$subject."\r\n";
if ($this->mail_format == 1) {
$detail .= "Content-Type:text/html;\r\n";
} else {
$detail .= "Content-Type:text/plain;\r\n";
}
$detail .= "charset=gb2312\r\n\r\n";
$detail .= $body."\r\n.\r\n";
$this->do_command("HELO smtp.qq.com\r\n", 250);
$this->do_command("AUTH LOGIN\r\n", 334);
$this->do_command($this->user."\r\n", 334);
$this->do_command($this->pass."\r\n", 235);
$this->do_command("MAIL FROM:<".$from.">\r\n", 250);
$this->do_command("RCPT TO:<".$to.">\r\n", 250);
$this->do_command("DATA\r\n",354);
$this->do_command($detail, 250);
$this->do_command("QUIT\r\n", 221);
return true;
}
}
$host = "smtp.163.com";
$port = 25;
$user = "tsq7473281@163.com";
$pass = "********";
$from = "tsq7473281@163.com";
$to = "fengyeyuye@163.com";
$subject = "Hello Body";
$content = "This is example email for you";
$mail = new smtp_mail($host, $port, $user, $pass);
$mail->send_mail($from, $to, $subject, $content);
?>