php 邮件类,php邮件
编写一个用php socket 发送邮件的类,简单好用,当用到php程序发送邮件时,
而在163服务器中,可以在RCPT命令中还可以验证163邮箱是否存在,还有很多用处,
我现在暂时还没想到。
记录下,有需要的朋友可以参考下
class mail {
/**
* 这些即可发送email
*/
public $host;
public $port;
public $user;
public $password;
public $mail_form;
public $rcpt;
public $body;
/**
* 附属在 header 中信息
*/
public $to_name;
public $from_name;
public $subject;
public $html;
public $connection;
public $msg = array();
// 进行参数初始化
public function __construct($conf, $rcpt, $header, $body, $html = true) {
try {
$this->host = $conf['host'];
$this->port = $conf['port'];
$this->user = $conf['user'];
$this->password = $conf['password'];
$this->rcpt = $rcpt;
$this->to_name = $header['to_name'];
$this->from_name = $header['from_name'];
$this->subject = $header['subject'];
$this->html = $html;
$this->body = base64_encode($body);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function connect() {
$this->connection = @fsockopen($this->host, '25', $errno, $errstr, 10);
if (!$this->connection) {
throw new Exception('connect failed!');
}
$greet = $this->callback_code();
}
public function helo() {
if($this->is_connect() &&
$this->send_data('HELO ' . $this->host) &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function send_data($cmd) {
if (is_resource($this->connection)) {
return @fwrite($this->connection, $cmd."\r\n", strlen($cmd) + 2);
}
}
public function auth(){
if ($this->is_connect() &&
$this->send_data('AUTH LOGIN') && $this->callback_code() == 334 &&
$this->send_data(base64_encode($this->user)) && $this->callback_code() == 334 &&
$this->send_data(base64_encode($this->password)) && $this->callback_code() == 235 ) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function Mail() {
if ($this->is_connect() &&
$this->send_data("MAIL FROM:user>") &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function is_connect() {
return is_resource($this->connection);
}
public function callback_code() {
if ($this->is_connect()) {
$msg = fgets($this->connection);
if (!empty($msg)) {
$code = substr($msg, 0, strpos($msg, ' '));
} else {
return '';
}
$this->msg[] = $msg;
return $code;
}
}
public function get_callback_msg_lastest() {
return end($this->msg);
}
public function rcpt($rcpt) {
if ($this->is_connect() &&
$this->send_data("RCPT TO:") &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function data() {
if ($this->is_connect() &&
$this->send_data('DATA') &&
$this->callback_code() == 354) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function send_mail() {
try {
$this->connect();
$this->helo();
$this->auth();
$this->Mail();
if (is_array($this->rcpt)) {
foreach ($this->rcpt as $rcpt) {
$this->rcpt($rcpt);
}
} else {
$this->rcpt($this->rcpt);
}
$this->data();
$header = str_replace("\r\n" . '.', "\r\n" . '..', trim(implode("\r\n", $this->get_header())));
$body = str_replace("\r\n" . '.', "\r\n" . '..', $this->body);
$body = substr($body, 0, 1) == '.' ? '.' . $body : $body;
$this->send_data($header);
$this->send_data('');
$this->send_data($body);
$this->send_data('.');
if ($this->callback_code() != 250) {
throw new Exception('send mail falied!');
}
return true;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
//只能检测同一邮件服务器上email address
public function is_email_exist() {
}
public function get_header() {
$header = array();
$content_type = $this->html ? 'text/html;' : 'text/plain;' ;
$headers [] = 'Date: ' . gmdate('D, j M Y H:i:s') . ' +0000';
$to_mail = is_array($this->rcpt) ? implode(',', $this->rcpt) : $this->rcpt;
$headers [] = 'To: "' . '=? utf8?B?' . base64_encode($this->to_name) . '?=' . '" ';
$headers [] = 'From: "' . '=?utf8?B?' . base64_encode($this->from_name) . '?=' . '" user . '>';
$headers [] = 'Subject: ' . '=?utf8?B?' . base64_encode($this->subject) . '?=';
$headers [] = 'Content-Type:'.$content_type . ' charset=utf8; format=flowed';
$headers [] = 'Content-Transfer-Encoding: base64';
$headers [] = 'Content-Disposition: inline';
return $headers;
}
}
/**
* 用函数封装类
*/
function send_mail($conf, $rcpt, $header, $body) {
$mail = new mail($conf, $rcpt, $header, $body);
if ($mail->send_mail()) {
echo 'send email successfully!';
} else {
echo 'falied!';
}
}
$conf = array(
'host' => 'smtp.163.com',
'port' => '25',
'user' => 'z1298703836@163.com',
'password' => 'zeiwei123',
);
$rcpt = array(
'z1298703836@sina.com',
'wei.zen@baisonmail.com'
);
$header = array(
'to_name' => 'willstang',
'from_name' => 'sinawill',
'subject' => 'tst of reo ',
);
$body = '
sfsfsd
';$mail = new mail($conf, $rcpt, $header, $body);
$mail->send_mail();
echo '
';<br>print_r($mail->msg); <p> </p> <p>熟悉邮件发送的过程,它的stmp协议,</p> <p>还有一些发送的细节,如:发送的内容分为两部分, 一部分在头部,另一部分是email的正文,</p> <p> </p> <p align="left"></p><div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/965030.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/965030.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">php 邮件类,php邮件 编写一个用php socket 发送邮件的类,简单好用,当用到php程序发送邮件时, 而在163服务器中,可以在RCPT命令中还可以验...</span> </div> <div class="art_confoot"></div>

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInAcookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionWwithSession_start()和stordoredAtain $ _session.2)

在PHP中,遍歷會話數據可以通過以下步驟實現:1.使用session_start()啟動會話。 2.通過foreach循環遍歷$_SESSION數組中的所有鍵值對。 3.處理複雜數據結構時,使用is_array()或is_object()函數,並用print_r()輸出詳細信息。 4.優化遍歷時,可採用分頁處理,避免一次性處理大量數據。這將幫助你在實際項目中更有效地管理和使用PHP會話數據。

會話通過服務器端的狀態管理機制實現用戶認證。 1)會話創建並生成唯一ID,2)ID通過cookies傳遞,3)服務器存儲並通過ID訪問會話數據,4)實現用戶認證和狀態管理,提升應用安全性和用戶體驗。

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

PHPSession失效的原因包括配置錯誤、Cookie問題和Session過期。 1.配置錯誤:檢查並設置正確的session.save_path。 2.Cookie問題:確保Cookie設置正確。 3.Session過期:調整session.gc_maxlifetime值以延長會話時間。

在PHP中調試會話問題的方法包括:1.檢查會話是否正確啟動;2.驗證會話ID的傳遞;3.檢查會話數據的存儲和讀取;4.查看服務器配置。通過輸出會話ID和數據、查看會話文件內容等方法,可以有效診斷和解決會話相關的問題。

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

在PHP中配置會話生命週期可以通過設置session.gc_maxlifetime和session.cookie_lifetime來實現。 1)session.gc_maxlifetime控制服務器端會話數據的存活時間,2)session.cookie_lifetime控制客戶端cookie的生命週期,設置為0時cookie在瀏覽器關閉時過期。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具