萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php基於socket實現SMTP發送郵件的方法

php基於socket實現SMTP發送郵件的方法

 這篇文章主要介紹了php基於socket實現SMTP發送郵件的方法,實例分析了php采用socket實現smtp發送郵件的原理與技巧,具有一定參考借鑒價值,需要的朋友可以參考下

   

本文實例講述了php基於socket實現SMTP發送郵件的方法。分享給大家供大家參考。具體分析如下:

php采用socket通過SMTP發送郵件。
用的是php的php-sockets擴展,可以發送純文本和html格式的郵件。代碼如下:

代碼如下:
<?php
/**
* 郵件發送類
* 支持發送純文本郵件和HTML格式的郵件
* @example
* $config = array(
* "from" => "*****",
* "to" => "***",
* "subject" => "test",
* "body" => "<b>test</b>",
* "username" => "***",
* "password" => "****",
* "isHTML" => true
* );
*
* $mail = new MySendMail();
*
* $mail->setServer("smtp.126.com");
*
* $mail->setMailInfo($config);
* if(!$mail->sendMail()) {
* echo $mail->error();
* return 1;
* }
*/
class MySendMail {
/**
* @var 郵件傳輸代理用戶名
* @access private
*/
private $_userName;
/**
* @var 郵件傳輸代理密碼
* @access private
*/
private $_password;
/**
* @var 郵件傳輸代理服務器地址
* @access protected
*/
protected $_sendServer;
/**
* @var 郵件傳輸代理服務器端口
* @access protected
*/
protected $_port=25;
/**
* @var 發件人
* @access protected
*/
protected $_from;
/**
* @var 收件人
* @access protected
*/
protected $_to;
/**
* @var 主題
* @access protected
*/
protected $_subject;
/**
* @var 郵件正文
* @access protected
*/
protected $_body;
/**
* @var 是否是HTML格式的郵件
* @access protected
*/
protected $_isHTML=false;
/**
* @var socket資源
* @access protected
*/
protected $_socket;
/**
* @var 錯誤信息
* @access protected
*/
protected $_errorMessage;
public function __construct($from="", $to="", $subject="", $body="", $server="", $username="", $password="",$isHTML="", $port="") {
if(!empty($from)){
$this->_from = $from;
}
if(!empty($to)){
$this->_to = $to;
}
if(!empty($subject)){
$this->_subject = $subject;
}
if(!empty($body)){
$this->_body = $body;
}
if(!empty($isHTML)){
$this->_isHTML = $isHTML;
}
if(!empty($server)){
$this->_sendServer = $server;
}
if(!empty($port)){
$this->_port = $port;
}
if(!empty($username)){
$this->_userName = $username;
}
if(!empty($password)){
$this->_password = $password;
}
}
/**
* 設置郵件傳輸代理
* @param string $server 代理服務器的ip或者域名
* @param int $port 代理服務器的端口,smtp默認25號端口
* @param int $localPort 本地端口
* @return boolean
*/
public function setServer($server, $port=25) {
if(!isset($server) || empty($server) || !is_string($server)) {
$this->_errorMessage = "first one is an invalid parameter";
return false;
}
if(!is_numeric($port)){
$this->_errorMessage = "first two is an invalid parameter";
return false;
}
$this->_sendServer = $server;
$this->_port = $port;
return true;
}
/**
* 設置郵件
* @access public
* @param array $config 郵件配置信息
* 包含郵件發送人、接收人、主題、內容、郵件傳輸代理的驗證信息
* @return boolean
*/
public function setMailInfo($config) {
if(!is_array($config) || count($config) < 6){
$this->_errorMessage = "parameters are required";
return false;
}
$this->_from = $config['from'];
$this->_to = $config['to'];
$this->_subject = $config['subject'];
$this->_body = $config['body'];
$this->_userName = $config['username'];
$this->_password = $config['password'];
if(isset($config['isHTML'])){
$this->_isHTML = $config['isHTML'];
}
return true;
}
/**
* 發送郵件
* @access public
* @return boolean
*/
public function sendMail() {
$command = $this->getCommand();
$this->socket();
foreach ($command as $value) {
if($this->sendCommand($value[0], $value[1])) {
continue;
}
else{
return false;
}
}
$this->close(); //其實這裡也沒必要關閉,smtp命令:QUIT發出之後,服務器就關閉了連接,本地的socket資源會自動釋放
echo 'Mail OK!';
return true;
}
/**
* 返回錯誤信息
* @return string
*/
public function error(){
if(!isset($this->_errorMessage)) {
$this->_errorMessage = "";
}
return $this->_errorMessage;
}
/**
* 返回mail命令
* @access protected
* @return array
*/
protected function getCommand() {
if($this->_isHTML) {
$mail = "MIME-Version:1.0rn";
$mail .= "Content-type:text/html;charset=utf-8rn";
$mail .= "FROM:test<" . $this->_from . ">rn";
$mail .= "TO:<" . $this->_to . ">rn";
$mail .= "Subject:" . $this->_subject ."rnrn";
$mail .= $this->_body . "rn.rn";
}
else{
$mail = "FROM:test<" . $this->_from . ">rn";
$mail .= "TO:<" . $this->_to . ">rn";
$mail .= "Subject:" . $this->_subject ."rnrn";
$mail .= $this->_body . "rn.rn";
}
$command = array(
array("HELO sendmailrn", 250),
array("AUTH LOGINrn", 334),
array(base64_encode($this->_userName) . "rn", 334),
array(base64_encode($this->_password) . "rn", 235),
array("MAIL FROM:<" . $this->_from . ">rn", 250),
array("RCPT TO:<" . $this->_to . ">rn", 250),
array("DATArn", 354),
array($mail, 250),
array("QUITrn", 221)
);
return $command;
}
/**
* @access protected
* @param string $command 發送到服務器的smtp命令
* @param int $code 期望服務器返回的響應嗎
* @param boolean
*/
protected function sendCommand($command, $code) {
echo 'Send command:' . $command . ',expected code:' . $code . '<br />';
//發送命令給服務器
try{
if(socket_write($this->_socket, $command, strlen($command))){
//讀取服務器返回
$data = trim(socket_read($this->_socket, 1024));
echo 'response:' . $data . '<br /><br />';
if($data) {
$pattern = "/^".$code."/";
if(preg_match($pattern, $data)) {
return true;
}
else{
$this->_errorMessage = "Error:" . $data . "|**| command:";
return false;
}
}
else{
$thi
copyright © 萬盛學電腦網 all rights reserved