一般发系统通知之类的邮件,这个版本就足够.<?php <br />
/**<br>
* 邮件发送类<br>
* 仅支持发送纯文本和HTML内容邮件<br>
* 需要的php扩展,sockets<br>
* @example<br>
* $mail = new MySendMail();<br>
* $mail->setServer("XXXXX", "XXXXX@XXXXX", "XXXXX"); 设置smtp服务器<br>
* $mail->setFrom("XXXXX"); 设置发件人<br>
* $mail->setReceiver("XXXXX"); 设置收件人<br>
* $mail->setMailInfo("test", "<b>test</b>"); 设置邮件主题、内容<br>
* $mail->sendMail(); 发送<br>
*/<br>
class MySendMail {<br>
/**<br>
* @var string 邮件传输代理用户名<br>
* @access private<br>
*/<br>
private $_userName;<br>
<br>
/**<br>
* @var string 邮件传输代理密码<br>
* @access private<br>
*/<br>
private $_password;<br>
<br>
/**<br>
* @var string 邮件传输代理服务器地址<br>
* @access private<br>
*/<br>
private $_sendServer;<br>
<br>
/**<br>
* @var int 邮件传输代理服务器端口<br>
* @access protected<br>
*/<br>
protected $_port=25;<br>
<br>
/**<br>
* @var string 发件人<br>
* @access protected<br>
*/<br>
protected $_from;<br>
<br>
/**<br>
* @var string 收件人<br>
* @access protected<br>
*/<br>
protected $_to;<br>
<br>
/**<br>
* @var string 主题<br>
* @access protected<br>
*/<br>
protected $_subject;<br>
<br>
/**<br>
* @var string 邮件正文<br>
* @access protected<br>
*/<br>
protected $_body;<br>
<br>
/**<br>
* @var reource socket资源<br>
* @access protected<br>
*/<br>
protected $_socket;<br>
<br>
/**<br>
* @var string 错误信息<br>
* @access protected<br>
*/<br>
protected $_errorMessage;<br>
<br>
/**<br>
* 设置邮件传输代理,如果是可以匿名发送有邮件的服务器,只需传递代理服务器地址就行<br>
* @access public<br>
* @param string $server 代理服务器的ip或者域名<br>
* @param string $username 认证账号<br>
* @param string $password 认证密码<br>
* @param int $port 代理服务器的端口,smtp默认25号端口<br>
* @return boolean<br>
*/<br>
public function setServer($server, $username="", $password="", $port=25) {<br>
$this->_sendServer = $server;<br>
$this->_port = $port;<br>
if(!empty($username)) {<br>
$this->_userName = base64_encode($username);<br>
}<br>
if(!empty($password)) {<br>
$this->_password = base64_encode($password);<br>
}<br>
return true;<br>
}<br>
<br>
/**<br>
* 设置发件人<br>
* @access public<br>
* @param string $from 发件人地址<br>
* @return boolean<br>
*/<br>
public function setFrom($from) {<br>
$this->_from = $from;<br>
return true;<br>
}<br>
<br>
/**<br>
* 设置收件人<br>
* @access public<br>
* @param string $to 收件人地址<br>
* @return boolean<br>
*/<br>
public function setReceiver($to) {<br>
$this->_to = $to;<br>
return true;<br>
}<br>
<br>
/**<br>
* 设置邮件信息<br>
* @access public<br>
* @param string $body 邮件主题<br>
* @param string $subject 邮件主体内容,可以是纯文本,也可是是HTML文本<br>
* @return boolean<br>
*/<br>
public function setMailInfo($subject, $body) {<br>
$this->_subject = $subject;<br>
$this->_body = base64_encode($body);<br>
if(!empty($attachment)) {<br>
$this->_attachment = $attachment;<br>
}<br>
return true;<br>
}<br>
<br>
/**<br>
* 发送邮件<br>
* @access public<br>
* @return boolean<br>
*/<br>
public function sendMail() {<br>
$command = $this->getCommand();<br>
$this->socket();<br>
<br>
foreach ($command as $value) {<br>
if($this->sendCommand($value[0], $value[1])) {<br>
continue;<br>
}<br>
else{<br>
return false;<br>
}<br>
}<br>
<br>
//其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放<br>
$this->close(); <br>
echo 'Mail OK!';<br>
return true;<br>
}<br>
<br>
/**<br>
* 返回错误信息<br>
* @return string<br>
*/<br>
public function error(){<br>
if(!isset($this->_errorMessage)) {<br>
$this->_errorMessage = "";<br>
}<br>
return $this->_errorMessage;<br>
}<br>
<br>
/**<br>
* 返回mail命令<br>
* @access protected<br>
* @return array<br>
*/<br>
protected function getCommand() {<br>
$separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符<br>
<br>
$command = array(<br>
array("HELO sendmail\r\n", 250)<br>
);<br>
if(!empty($this->_userName)){<br>
$command[] = array("AUTH LOGIN\r\n", 334);<br>
$command[] = array($this->_userName . "\r\n", 334);<br>
$command[] = array($this->_password . "\r\n", 235);<br>
}<br>
<br>
//设置发件人<br>
$command[] = array("MAIL FROM: _from . ">\r\n", 250);<br>
$header = "FROM: _from . ">\r\n";<br>
<br>
//设置收件人<br>
$command[] = array("RCPT TO: _to . ">\r\n", 250);<br>
$header .= "TO: _to . ">\r\n";<br>
<br>
$header .= "Subject: " . $this->_subject ."\r\n";<br>
$header .= "Content-Type: multipart/alternative;\r\n";<br>
<br>
//邮件头分隔符<br>
$header .= "\t" . 'boundary="' . $separator . '"';<br>
<br>
$header .= "\r\nMIME-Version: 1.0\r\n";<br>
$header .= "\r\n--" . $separator . "\r\n";<br>
$header .= "Content-Type:text/html; charset=utf-8\r\n";<br>
$header .= "Content-Transfer-Encoding: base64\r\n\r\n";<br>
$header .= $this->_body . "\r\n";<br>
$header .= "--" . $separator . "\r\n";<br>
<br>
//结束数据<br>
$header .= "\r\n.\r\n";<br>
<br>
$command[] = array("DATA\r\n", 354);<br>
$command[] = array($header, 250);<br>
$command[] = array("QUIT\r\n", 221);<br>
<br>
return $command;<br>
}<br>
<br>
/**<br>
* 发送命令<br>
* @access protected<br>
* @param string $command 发送到服务器的smtp命令<br>
* @param int $code 期望服务器返回的响应吗<br>
* @return boolean<br>
*/<br>
protected function sendCommand($command, $code) {<br>
echo 'Send command:' . $command . ',expected code:' . $code . '<br>';<br>
//发送命令给服务器<br>
try{<br>
if(socket_write($this->_socket, $command, strlen($command))){<br>
<br>
//当邮件内容分多次发送时,没有$code,服务器没有返回<br>
if(empty($code)) {<br>
return true;<br>
}<br>
<br>
//读取服务器返回<br>
$data = trim(socket_read($this->_socket, 1024));<br>
echo 'response:' . $data . '<br><br>';<br>
<br>
if($data) {<br>
$pattern = "/^".$code."/";<br>
if(preg_match($pattern, $data)) {<br>
return true;<br>
}<br>
else{<br>
$this->_errorMessage = "Error:" . $data . "|**| command:";<br>
return false;<br>
}<br>
}<br>
else{<br>
$this->_errorMessage = "Error:" . socket_strerror(socket_last_error());<br>
return false;<br>
}<br>
}<br>
else{<br>
$this->_errorMessage = "Error:" . socket_strerror(socket_last_error());<br>
return false;<br>
}<br>
}catch(Exception $e) {<br>
$this->_errorMessage = "Error:" . $e->getMessage();<br>
}<br>
}<br>
<br>
/**<br>
* 建立到服务器的网络连接<br>
* @access private<br>
* @return boolean<br>
*/<br>
private function socket() {<br>
if(!function_exists("socket_create")) {<br>
$this->_errorMessage = "Extension sockets must be enabled";<br>
return false;<br>
}<br>
//创建socket资源<br>
$this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));<br>
<br>
if(!$this->_socket) {<br>
$this->_errorMessage = socket_strerror(socket_last_error());<br>
return false;<br>
}<br>
<br>
socket_set_block($this->_socket);//设置阻塞模式<br>
<br>
//连接服务器<br>
if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {<br>
$this->_errorMessage = socket_strerror(socket_last_error());<br>
return false;<br>
}<br>
socket_read($this->_socket, 1024);<br>
<br>
return true;<br>
}<br>
<br>
/**<br>
* 关闭socket<br>
* @access private<br>
* @return boolean<br>
*/<br>
private function close() {<br>
if(isset($this->_socket) && is_object($this->_socket)) {<br>
$this->_socket->close();<br>
return true;<br>
}<br>
$this->_errorMessage = "No resource can to be close";<br>
return false;<br>
}<br>
}<br>
<br>
/**************************** Test ***********************************/<br>
$mail = new MySendMail();<br>
$mail->setServer("XXXX", "XXX@XXX", "XXX");<br>
$mail->setFrom("XXX@XX");<br>
$mail->setReceiver("XXX@XX");<br>
$mail->setMailInfo("test", "<b>test</b>");<br>
$mail->sendMail();
AD:真正免费,域名+虚机+企业邮箱=0元