Among the several PHP homepage spaces that the author has applied for, there are not many that can provide the mail function. It always fails after calling the mail() function
Nothing further. But the role of email in online life is getting bigger and bigger. I don’t want to talk about it anymore, but what should I do if the homepage space does not support mail() sending? I also thought about implementing email through socket
Send, but unfortunately I am not familiar with socket programming using PHP. In addition, sending emails requires using the SMTP protocol, and I need to read a lot of English, so
I have never studied it before. Finally one day I found an article about using socket programming to send emails. I copied it like a treasure
Download it and transform it into a class available in PHP for everyone to use. The original article was just a simple example, and there was one more
After many experiments and modifications, I finally changed it into a class that directly uses sockets to send emails to specified mailboxes, such as
If you combine it with the previous article about sending MIME, you can send emails on websites that do not support the mail() function. Because I posted
The process of sending emails takes time, and may not be exactly the same as the processing mechanism of mail(), so the speed is slower, but it can solve the need to send
The function of sending emails is urgently needed. At the same time, you can also learn to use php for socket programming. Next, I will introduce to you the implementation principle of this class,
At the same time, I will explain to you some basic knowledge about SMTP.
Introduction to Socket Programming
I would like to declare to everyone that I am not a TCP/IP programming expert, so I just share my understanding and experience here.
Use the fsockopen function to open an Internet connection. Function syntax format:
int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);
I don’t think I need to talk about the meaning of the parameters. Since the SMTP protocol is used here, the port number is 25. After the connection is successfully opened, a
will be returned
A socket handle, which can be used like a file handle. The available operations are fputs(), fgets(), feof(), fclose()
wait.
That’s it for a very simple introduction.
Basics of SMTP
The general command format of the Internet protocol based on TCP/IP is implemented through the request/response method, and all text information is used, so
It's a little easier to deal with. SMTP is the abbreviation of Simple Mail Transfer Protocol, which enables the client to send emails to the server. So
The command mentioned below refers to the client sending a request instruction to the server, and the response refers to the information returned by the server to the client.
SMTP is divided into two parts: command header and message body. The command header mainly completes the connection between the client and the server, verification, etc. The whole process consists of multiple lives
Order composition. After each command is sent to the server, the server will give response information, usually a 3-digit response code and response text. Different servers
The response code returned by the server is protocol-compliant, but the response body does not. There is a carriage return character at the end of each command and response, so
Use fputs() and fgets() to process commands and responses. SMTP command and response messages are single lines. Is the information body timid?
In the body part of the text, the last closing line should be ended with a separate "."
Some commonly used SMTP commands on the client are:
HELO hostname: Greet the server and tell the client the machine name used. You can fill it in casually
MAIL FROM: sender_id: Tell the server the address of the sender
RCPT TO: receiver_id: Tell the server the address of the recipient
DATA: The content of the letter begins to be transmitted below, and it must end with a special line containing only .
RESET: Cancel the previous command and start over
VERIFY userid: Verify whether the account exists (this command is optional and may not be supported by the server)
QUIT: Quit the connection, end
The response information returned by the server is (the format is: response code + space + explanation):
220 Service ready (this information will be returned when the socket connection is successful)
221 Processing
250 The request email action is correct and completed (this information will be returned if the HELO, MAIL FROM, RCPT TO, QUIT command is executed successfully)
354 starts sending data and ends with . (This information will be returned if the DATA command is successfully executed, and the client should send the information)
500 Syntax error, command not recognized
550 Command cannot be executed, the email is invalid
552 Interrupt handling: User exceeded file space
The following is a simple command header (this is done after opening the socket), which is the test result of sending an email to stmp.263.net:
HELO limodou
250 smtp.263.net
MAIL FROM: chatme@263.net
250 Ok
RCPT TO: chatme@263.net
250 Ok
DATA
354 End data with .
To: chatme@263.net
From: chatme@263.net
Subject: test
From: chatme@263.net
test
.
QUIT
250 Ok: queued as C46411C5097E0
This is some simple knowledge about SMTP. Related content can be found in RFC.
RFC 821 defines related commands for receiving/sending emails.
RFC 822 specifies the format of email content.
RFC 2045-2048 specifies the format for multimedia email content,
RFC 1113, 1422-1424 discusses how to improve the confidentiality of email.
Implementation of send_mail class
Now let’s introduce the sending email class I wrote. With the above preparatory knowledge, the following is the implementation.
Member variables of class
var $lastmessage; //Record the last response message returned
var $lastact; //Last action, string form
var $welcome; //Used after HELO to welcome users
var $debug; //Whether to display debugging information
var $smtp; //smtp server
var $port; //smtp port number
var $fp; //socket handle
Among them, $lastmessage and $lastact are used to record the last response information and the executed command. When an error occurs, the user can use it
them. For testing needs, I also defined the $debug variable. When its value is true, some execution information will be displayed during the running process, otherwise nothing
any output. $fp is used to save the opened socket handle.
Construction of class
-------------------------------------------------- -------------------------------
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";
}
-------------------------------------------------- -------------------------------
This constructor mainly completes the determination and setting of some initial values. $welcome is used in the HELO directive to tell the server the user's name.
The HELO instruction requires a machine name, but it can be used without it. If the user does not provide $welcome, the local machine name is automatically searched.
Show debugging information
-------------------------------------------------- ----------------------------------
1 function show_debug($message, $inout)
2 {
3 if ($this->debug)
4 {
5 if($inout=="in") //Response information
6 {
7 $m=
8}
9 else
10
$m=>> ;
11 if(!ereg("
$", $message))
12 $message .= "
";
13 $message=nl2br($message);
14 echo "${m}${message}";
15 }
16 }
-------------------------------------------------- ----------------------------------

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

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