PHP does not provide a ready-made smtp function, but provides a mail () function that is not very flexible. this function requires support on server configuration and does not support smtp verification, it is not recommended because it cannot work normally in many occasions. The purpose of this article is to specify the direction for the novice, and do not involve those advanced content. First, the level is limited, and second, I am worried about not using SMTP.
PHP does not provide a ready-made smtp function, but provides a mail () function that is not very flexible. this function requires support on server configuration and does not support smtp verification, it is not recommended because it cannot work normally in many occasions. The purpose of this article is to indicate the direction for new users and not to involve those advanced content. First, the level is limited. second, I am worried that related concepts cannot be accurately described, which in turn leads to misleading, also, please study in depth.
"Using php to send mail" has recently become the second newbie trap in this version after "register_globals". today, I write this article to help beginners solve their problems and hope to give directions to confused people.
Let's start with the following example:
Reference:
[Root @ server ~ /] # Telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 server.domain.com.br ESMTP Postfix (2.1.0)
Mail from: teste@dominio.com.br
250 OK
Rcpt to: teste@dominio.com.br
250 OK
DATA
354 End data with <CR> <LF>. <CR> <LF>
Teste
.
250 OK: queued as 7B41F4665A
QUIT
221 Bye
Connection closed by foreign host.
Note: The above postfix documents from netkiller are lazy and ready for use directly.
First, telnet is used to connect to the local port 25. anyone familiar with the network knows that the smtp protocol uses port 25, which means that the local smtp server is connected now.
Reference:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 server.domain.com.br ESMTP Postfix (2.1.0)
These are system output information, indicating that the smtp server has been connected and is postfix.
The "mail from: teste@dominio.com.br" command specifies that the sending address is a teste@dominio.com.br, and "250 OK" indicates that this command is accepted and correctly executed by the server, this is similar to the status code of http 200, 404, and 500. The next "rcpt to: teste@dominio.com.br" specifies that the recipient address is a teste@dominio.com.br.
Reference:
DATA
354 End data with <CR> <LF>. <CR> <LF>
Teste
.
After entering "DATA", the system prompts "<Press Enter>. <Press Enter>" to end the input. The body is "teste ".
Use "QUIT" to exit.
The above is the simplest process of sending mail. From this example, we can see that sending mail is actually a very simple thing. in essence, it is to establish a connection to the smtp server, then, you can send some simple commands to it, and a simple email is sent. as for emails or operations with more complex content, it is actually a little extended on this basis.
Php is used to implement this process. In fact, php Socket functions and Network Functions are used to operate socket functions to establish a connection with the smtp server, and then a text command is sent to the server, if you look at the classes or functions that have been written to send emails using the smtp protocol, I believe they can prove to me.
Because there are already a lot of well-encapsulated classes or functions for us to complete the underlying socket-level operations, we just need to use them directly, in this article, I will not discuss the underlying code with time and effort. if you want to study the underlying code, find the code to study it. Now let's continue with me. let's take some practical code to illustrate how to use php to send emails. the class is PEAR: Mail.
Code:
<? Php
Require_once 'mail. php ';
$ Conf ['mail'] = array (
'Host' => 'XX. xx. xx. xx', // smtp server address, which can be an IP address or domain name
'Auth' => true, // true indicates that the smtp server needs to be verified. false indicates that the code does not need to be verified.
'Username' => 'tester', // User name
'Password' => 'retset' // password
);
/***
* You can use the $ headers array To define the content of the mail header. for example, you can use $ headers ['reply-to'] To define the Reply address.
* In this way, you can easily customize the mail header to be sent.
***/
$ Headers ['from'] = 'tester @ domain.com '; // mail address
$ Headers ['to'] = 'tester @ domain.com '; // recipient address
$ Headers ['subobject'] = 'Test mail send by php'; // mail title
$ Mail_object = & Mail: factory ('smtp ', $ conf ['mail']);
$ Body = <MSG // body of the email
Hello world !!!
MSG;
$ Mail_res = $ mail_object-> send ($ headers ['to'], $ headers, $ body); // send
If (Mail: isError ($ mail_res) {// Check the error
Die ($ mail_res-> getMessage ());
}
?>
The above code is very simple. it is not difficult to understand it with comments. For more information about PEAR and PEAR: Mail, you can read PEAR Manual for further information.
Now, you can start working on it, but if you want to do better and do more, I will provide more guidance here.
1. SMTP protocol
Be familiar with and understand the content of SMTP protocol, so that you can perform more advanced operations and even write an email program that meets your special needs. Although the above code is simple, there are still some people who do not know what the Mail header is mentioned in the comment and what impact it has on the sent mail.
For example, the question "why does the recipient see garbled characters when sending html mail" may be related to the Mail header. if you know more about the smtp protocol, you can quickly find out the problem.
2. MIME specification
If you want to send html or even multimedia emails, you must have a certain understanding of MIME. With this knowledge, you can send more exciting emails.
3. PEAR
PEAR is not the only tool for sending emails. However, PEAR contains Mail, Mail_Mime, and other encapsulated classes. This allows us to get twice the result with half the effort, in addition to Mail, it also provides many other ready-made tools that are worth your time to learn.