PHP郵件傳送類

2021-06-22 22:00:45 字數 3907 閱讀 9380

<?php

/*** 郵件傳送類

* 支援傳送純文字郵件和html格式的郵件

* @example

* $config = array(

* "from" => "*****",

* "to" => "***",

* "subject" => "test",

* "body" => "test",

* "username" => "***",

* "password" => "****",

* "ishtml" => true

* );

** $mail = new mysendmail();

** $mail->setserver("smtp.126.com");

** $mail->setmailinfo($config);

* if(!$mail->sendmail())

*/class mysendmail

if(!empty($to))

if(!empty($subject))

if(!empty($body))

if(!empty($ishtml))

if(!empty($server))

if(!empty($port))

if(!empty($username))

if(!empty($password))

}/**

* 設定郵件傳輸**

* @param string $server **伺服器的ip或者網域名稱

* @param int $port **伺服器的埠,smtp預設25號埠

* @param int $localport 本地埠

* @return boolean

*/public function setserver($server, $port=25)

if(!is_numeric($port))

$this->_sendserver = $server;

$this->_port = $port;

return true;

}/**

* 設定郵件

* @access public

* @param array $config 郵件配置資訊

* 包含郵件傳送人、接收人、主題、內容、郵件傳輸**的驗證資訊

* @return boolean

*/public function setmailinfo($config)

$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']))

return true;

}/**

* 傳送郵件

* @access public

* @return boolean

*/public function sendmail()

else

}$this->close(); //其實這裡也沒必要關閉,smtp命令:quit發出之後,伺服器就關閉了連線,本地的socket資源會自動釋放

echo 'mail ok!';

return true;

}/**

* 返回錯誤資訊

* @return string

*/public function error()

return $this->_errormessage;

}/**

* 返回mail命令

* @access protected

* @return array

*/protected function getcommand()

else

$command = array(

array("helo sendmail\r\n", 250),

array("auth login\r\n", 334),

array(base64_encode($this->_username) . "\r\n", 334),

array(base64_encode($this->_password) . "\r\n", 235),

array("mail from:<" . $this->_from . ">\r\n", 250),

array("rcpt to:<" . $this->_to . ">\r\n", 250),

array("data\r\n", 354),

array($mail, 250),

array("quit\r\n", 221)

);return $command;

}/**

* @access protected

* @param string $command 傳送到伺服器的smtp命令

* @param int $code 期望伺服器返回的響應嗎

* @param boolean

*/protected function sendcommand($command, $code)

else

}else

}else

}catch(exception $e)

}/**

* 建立到伺服器的網路連線

* @access private

* @return boolean

*/private function socket()

//建立socket資源

$this->_socket = socket_create(af_inet, sock_stream, getprotobyname('tcp'));

if(!$this->_socket)

//連線伺服器

if(!socket_connect($this->_socket, $this->_sendserver, $this->_port))

socket_read($this->_socket, 1024);

return true;

}/**

* 關閉socket

* @access private

* @return boolean

*/private function close()

$this->_errormessage = "no resource can to be close";

return false;

}} /**************************** test ***********************************/

$config = array(

"from" => "********@163.com",

"to" => "******@163.com",

"subject" => "test",

"body" => "test",

"username" => "******",

"password" => "password",

);$mail = new mysendmail();

$mail->setserver("smtp.163.com");

$mail->setmailinfo($config);

if(!$mail->sendmail())

php郵件傳送

今天要用到php的郵件傳送,便用phpmailer弄了個 類檔案 emaillclass.php class phpmailer function send addr,fromname,title,content smtp array array ehlo hello lb,220,250 ehlo...

php 郵件傳送

2 修改class.phpmailer.php 增加變數 fromemail 修改下面內容 成如下內容 if this fromname else result this headerline from this fromemail.fromname result this headerline r...

php 傳送郵件

results wpdb query sql 傳送郵件提醒有人申請 smtpserver smtp.qq.com smtp伺服器,如 smtp.qq.com smtpserverport 25 smtp伺服器端口,一般為25 smtpusermail 117 x837 qq.com smtp伺服器的...