在PHP中用Socket發(fā)送電子郵件(三)
Jun 21, 2016 am 09:14 AM郵件發(fā)送處理
下面是真正的秘密了,可要看仔細(xì)了。:)
--------------------------------------------------------------------------------
1 function send( $to,$from,$subject,$message)
2 {
3
4 //連接服務(wù)器
5 $this->lastact="connect";
6
7 $this->show_debug("Connect to SMTP server : ".$this->smtp, "out");
8 $this->fp = fsockopen ( $this->smtp, $this->port );
9 if ( $this->fp )
10 {
11
12 set_socket_blocking( $this->fp, true );
13 $this->lastmessage=fgets($this->fp,512);
14 $this->show_debug($this->lastmessage, "in");
15
16 if (! ereg ( "^220", $this->lastmessage ) )
17 {
18 return false;
19 }
20 else
21 {
22 $this->lastact="HELO " . $this->welcome . "\n";
23 if(!$this->do_command($this->lastact, "250"))
24 {
25 fclose($this->fp);
26 return false;
27 }
28
29 $this->lastact="MAIL FROM: $from" . "\n";
30 if(!$this->do_command($this->lastact, "250"))
31 {
32 fclose($this->fp);
33 return false;
34 }
35
36 $this->lastact="RCPT TO: $to" . "\n";
37 if(!$this->do_command($this->lastact, "250"))
38 {
39 fclose($this->fp);
40 return false;
41 }
42
43 //發(fā)送正文
44 $this->lastact="DATA\n";
45 if(!$this->do_command($this->lastact, "354"))
46 {
47 fclose($this->fp);
48 return false;
49 }
50
51 //處理Subject頭
52 $head="Subject: $subject\n";
53 if(!empty($subject) && !ereg($head, $message))
54 {
55 $message = $head.$message;
56 }
57
58 //處理From頭
59 $head="From: $from\n";
60 if(!empty($from) && !ereg($head, $message))
61 {
62 $message = $head.$message;
63 }
64
65 //處理To頭
66 $head="To: $to\n";
67 if(!empty($to) && !ereg($head, $message))
68 {
69 $message = $head.$message;
70 }
71
72 //加上結(jié)束串
73 if(!ereg("\n\.\n", $message))
74 $message .= "\n.\n";
75 $this->show_debug($message, "out");
76 fputs($this->fp, $message);
77
78 $this->lastact="QUIT\n";
79 if(!$this->do_command($this->lastact, "250"))
80 {
81 fclose($this->fp);
82 return false;
83 }
84 }
85 return true;
86 }
87 else
88 {
89 $this->show_debug("Connect failed!", "in");
90 return false;
91 }
92 }
--------------------------------------------------------------------------------
有些意思很清楚的我就不說(shuō)了。
這個(gè)函數(shù)一共有四個(gè)參數(shù),分別是$to表示收信人,$from表示發(fā)信人,$subject表求郵件主題和$message表示郵件體。如果處理成功則返回true,失敗則返回false。
第8行,連接郵件服務(wù)器,如果成功響應(yīng)碼應(yīng)為220。
第12行,設(shè)置阻塞模式,表示信息必須返回才能繼續(xù)。詳細(xì)說(shuō)明看手冊(cè)吧。
第16行,判斷響應(yīng)碼是否為220,如果是,則繼續(xù)處理,否則出錯(cuò)返回。
第22-27行,處理HELO指令,期望響應(yīng)碼為250。
第29-34行,處理MAIL FROM指令,期望響應(yīng)碼為250。
第36-41行,處理RCPT TO指令,期望響應(yīng)碼為250。
第44-49行,處理DATA指令,期望響應(yīng)碼為354。
第51-76行,生成郵件體,并發(fā)送。
第52-56行,如果$subject不為空,則查找郵件體中是否有主題部分,如果沒(méi)有,則加上主題部分。
第59-63行,如果$from不為空,則查找郵件體中是否有發(fā)信人部分,如果沒(méi)有,則加上發(fā)信人部分。
第66-70行,如果$to不為空,則查找郵件體中是否有收信人部分,如果沒(méi)有,則加上收信人部分。
第73-74行,查找郵件體是否有了結(jié)束行,如果沒(méi)有則加上郵件體的結(jié)束行(以"."作為單獨(dú)的一行的特殊行)。
第76行,發(fā)送郵件體。
第78-83行,執(zhí)行QUIT結(jié)否與服務(wù)器的連接,期望響應(yīng)碼為250。
第85行,返回處理成功標(biāo)志(true)。
第81-91行,與服務(wù)器連接失敗的處理。
以上為整個(gè)send_mail類的實(shí)現(xiàn),應(yīng)該不是很難的。

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)

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

vue3 uses element-plus to call the message environment: vue3+typescript+element-plus1. After the global introduction of element, element has added the global method $message in app.config.globalProperties, so you can use mounted(){(thisasany) directly in the optionsAPI. $message.success("this.$message");}2. In the compositionAPI, the setup method passes in two variables props and

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提交表單通過(guò)后,彈出的對(duì)話框怎樣在當(dāng)前頁(yè)彈出php提交表單通過(guò)后,彈出的對(duì)話框怎樣在當(dāng)前頁(yè)彈出而不是在空白頁(yè)彈出?想實(shí)現(xiàn)這樣的效果:而不是空白頁(yè)彈出:------解決方案--------------------如果你的驗(yàn)證用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!

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

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.
