在PHP中用Socket發(fā)送電子郵件(二)
Jun 21, 2016 am 09:14 AMsend_mail類的實現
現在開始介紹我所編寫的發(fā)送郵件類。有了上面的預備知識了,下面就是實現了。
類的成員變量
var $lastmessage; //記錄最后返回的響應信息
var $lastact; //最后的動作,字符串形式
var $welcome; //用在HELO后面,歡迎用戶
var $debug; //是否顯示調試信息
var $smtp; //smtp服務器
var $port; //smtp端口號
var $fp; //socket句柄
其中,$lastmessage和$lastact用于記錄最后一次響應信息及執(zhí)行的命令,當出錯時,用戶可以使用它們。為了測試需要,我還定義了$debug變量,當其值為true時,會在運行過程中顯示一些執(zhí)行信息,否則無任何輸出。$fp用于保存打開后的socket句柄。
類的構造
--------------------------------------------------------------------------------
function send_mail($smtp, $welcome="", $debug=false)
{
if(empty($smtp)) die("SMTP cannt be NULL!");
$this->smtp=$smtp;
if(empty($welcome))
{
$this->welcome=gethostbyaddr("localhost");
}
else
$this->welcome=$welcome;
$this->debug=$debug;
$this->lastmessage="";
$this->lastact="";
$this->port="25";
}
--------------------------------------------------------------------------------
這個構造函數主要完成一些初始值的判定及設置。$welcome用于HELO指令中,告訴服務器用戶的名字。HELO指令要求為機器名,但是不用也可以。如果用戶沒有給出$welcome,則自動查找本地的機器名。
顯示調試信息
--------------------------------------------------------------------------------
1 function show_debug($message, $inout)
2 {
3 if ($this->debug)
4 {
5 if($inout=="in") //響應信息
6 {
7 $m='8 }
9 else
10 $m='>> ';
11 if(!ereg("\n$", $message))
12 $message .= "
";
13 $message=nl2br($message);
14 echo "${m}${message}";
15 }
16 }
--------------------------------------------------------------------------------
這個函數用來顯示調試信息??梢栽?inout中指定是上傳的指令還是返回的響應,如果為上傳指令,則使用"out";如果為返回的響應則使用"in"。
第3行,判斷是否要輸出調試信息。
第5行,判斷是否為響應信息,如果是,則在第7行將信息的前面加上"> "來區(qū)別上傳指令。
第11-12行,判斷信息串最后是否為換行符,如不是則加上HTML換行標記。第13行將所以的換行符轉成HTML的換行標記。
第14行,輸出整條信息,同時將信息顏色置為灰色以示區(qū)別。執(zhí)行一個命令
--------------------------------------------------------------------------------
1 function do_command($command, $code)
2 {
3 $this->lastact=$command;
4 $this->show_debug($this->lastact, "out");
5 fputs ( $this->fp, $this->lastact );
6 $this->lastmessage = fgets ( $this->fp, 512 );
7 $this->show_debug($this->lastmessage, "in");
8 if(!ereg("^$code", $this->lastmessage))
9 {
10 return false;
11 }
12 else
13 return true;
14 }
--------------------------------------------------------------------------------
在編寫socket處理部分發(fā)現,一些命令的處理很相似,如HELO,MAIL FROM,RCPT TO,QUIT,DATA命令,都要求根據是否顯示調試信息將相關內容顯示出來,同時對于返回的響應碼,如果是期望的,則應繼續(xù)處理,如果不是期望的,則應中斷出理。所以為了清晰與簡化,專門對這些命令的處理編寫了一個通用處理函數。函數的參數中$code為期望的響應碼,如果響應碼與之相同則表示處理成功,否則出錯。
第3行,記錄最后執(zhí)行命令。
第4行,將上傳命令顯示出來。
第5行,則使用fputs真正向服務器傳換指令。
第6行,從服務器接收響應信息將放在最后響應消息變量中。
第7行,將響應信息顯示出來。
第8行,判斷響應信息是否期待的,如果是則第13行返回成功(true),否則在第10行返回失敗(false)。
這樣,這個函數一方面完成指令及信息的發(fā)送顯示功能,別一方面對返回的響應判斷是否成功。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

I am new to the springboot project. (1) I found that breakpoint debugging was ineffective. I was very depressed and searched for solutions online. All I saw were some very complicated solutions, which were said to be remote debugging, but also required additional opening slogans. This is different from a traditional project, so I don’t think it’s necessary. So after some exploration, I found that there is a simpler way. The steps are as follows: Add a configuration in the plugin part of the pom file: false and it will be ok; (2) Regarding the error in the SpringBoot project that the web.xml file is missing, because Traditional web projects require web.xml files, but SpringBoot projects do not require web.xml files.

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表單通過后,彈出的對話框怎樣在當前頁彈出php提交表單通過后,彈出的對話框怎樣在當前頁彈出而不是在空白頁彈出?想實現這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗證用PHP在后端,那么就用Ajax;僅供參考:HTML code

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers
