Use PHP to send mail through SMTP novice guide

Source: Internet
Author: User
Tags define array functions header pear reference socket domain
smtp| Send mail

Because PHP does not provide out-of-the-box SMTP functions, it provides a less flexible mail () function that requires support on the server configuration and does not support SMTP authentication, and is not recommended for use in many situations. The purpose of this article is to specify the direction for the novice, and did not involve those advanced content, a limited level of their own, and also worry about not accurate to tell the relevant concepts, and thus mislead you, but also to learn their own in-depth. This writing date is July 2, 2004, please pay attention to the timeliness when reading.

"Use PHP to send mail" has recently become a "register_globals" after the second novice trap, today specially write this article for novice FAQ, hoping to be confused for the direction of the 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 ' ^] '.
server.domain.com.br ESMTP Postfix (2.1.0)
MAIL from:teste@dominio.com.br
Ok
RCPT to:teste@dominio.com.br
Ok
DATA
354 End data with <CR><LF>.<CR><LF>
Teste
.
Ok:queued as 7b41f4665a
QUIT
221 Bye
Connection closed by foreign host.
Note: The above from netkiller postfix documents, lazy, directly with ready-made.

The first is to use Telnet to connect to local port 25, and people who are slightly familiar with the DOT network know that the SMTP protocol uses port 25, which means that it is now connected to the local SMTP server.
Reference:
Trying 127.0.0.1 ...
Connected to localhost.
Escape character is ' ^] '.
server.domain.com.br ESMTP Postfix (2.1.0)

These things are system output information that is already connected, and this SMTP server is postfix.

The "MAIL from:teste@dominio.com.br" command indicates that the address is teste@dominio.com.br, "OK" indicates that the command was accepted and properly executed by the server, similar to the HTTP protocol 200, 404, 500 and so on status code. The next "RCPT to:teste@dominio.com.br" indicates that the recipient address is teste@dominio.com.br.
Reference:
DATA
354 End data with <CR><LF>.<CR><LF>
Teste
.

This paragraph is the input message body, enter "DATA" after the system prompts to use "< return >.< return >" To end the input, the body content is "teste".
Finally, use "QUIT" to exit.

This is the simplest time to send mail process, from this example we can see, send mail is actually very simple thing, essentially is to establish a connection to the SMTP server, and then send a few simple commands to it, a simple message sent out, As for the more complex content of the mail or operation, in fact, on this basis a little expansion.

The process of using PHP to achieve, in fact, is to use PHP socket functions, network functions, and so on to operate the socket functions and SMTP server to establish a connection, and then send the text command to the server, If you take a look at those written classes or functions that use the SMTP protocol to send mail, I believe I can attest to that.

Since there are already a lot of ready-made packaged classes or functions that perform the underlying socket-level operations for us, we just need to use directly, and I will not take the time to bother in this article to discuss the underlying code, have the spirit to study, find the code to study it. Now go on with me, and we'll point to the actual code to show you how to use PHP to send mail, the class is pear::mail.
Code:
<?php
Require_once ' mail.php ';

$conf [' mail '] = Array (
        ' host '       => ' xx.xx.xx.xx ',                //SMTP server address, you can use IP address or domain name
        ' auth '       => true,                         //true indicates that the SMTP server requires authentication, false code does not require
         ' username ' => ' tester ',                     /user name
         ' password ' => ' Retset '                       //password
);

/***
* Using the $headers array, you can define the contents of the message header, such as using $headers[' reply-to ' to define the reply address
* In this way, you can easily customize the message headers for outgoing messages
***/
$headers [' from '] = ' tester@domain.com '; Sending address
$headers [' to '] = ' tester@domain.com '; Address of the receiving letter
$headers [' Subject '] = ' test mail send by PHP '; Message headers
$mail _object = &mail::factory (' smtp ', $conf [' Mail ']);

$body = <<< MSG//message body
Hello World!!!
MSG;

$mail _res = $mail _object->send ($headers [' to '], $headers, $body); Send

if (Mail::iserror ($mail _res)) {//Detect error
Die ($mail _res->getmessage ());
}
?>

The above code is very simple, with the annotation should not be ugly to understand, about pear and pear::mail more information, you can read pear manual to get further information.

Now you can start working on the gourd, but if you want to do better and do more, I'm here to provide some more guidance.

1. SMTP protocol
Be familiar with and understand the contents of the SMTP protocol so you can do more advanced operations and even write your own email program that meets your particular needs. The above code is simple, but there is certainly a lot of people do not know what the message header is mentioned in the note, and how it affects the message sent.
For example, "Send HTML mail why the other side see is garbled" and so on may be related to the headers, if the SMTP protocol more understanding, you can quickly know the problem.
2. MIME specification
If you want to send HTML mail or even multimedia mail, must have a certain understanding of mime, with this knowledge you can send more interesting messages.
3, PEAR
Pear is not the only tool to send mail, but pear contains classes that are already encapsulated in mail, Mail_mime, and so on, and can make our development a lot easier, and in addition to mail, it provides a number of ready-made tools that are well worth the time to learn.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.