Using PHP to send mail via SMTP Novice guide _php tutorial

Source: Internet
Author: User
Tags pear
because PHP does not provide ready-made SMTP functions, but provides a less flexible mail () function, this function requires server configuration support, and does not support SMTP authentication, in many cases does not work properly, Therefore, it is not recommended. The purpose of this article is to identify the direction of the novice, and does not involve those advanced content, the level of its own limited, but also worried that can not accurately tell the relevant concepts, and therefore misleading to you, also please learn in-depth. This writing date is July 2, 2004, please pay attention to the timeliness when reading.

"Send mail using PHP" has recently become the second "register_globals" after the new version of the trap, today specifically to write this article for novice doubts, hope can for confused people to indicate direction.
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 .
Teste
.
Ok:queued as 7b41f4665a
QUIT
221 Bye
Connection closed by foreign host.
Note: The above postfix documents from Netkiller, lazy, directly with ready-made.

The first is to use Telnet to connect to the local 25 port, and a bit of familiarity with the DOT network knows that the SMTP protocol uses 25 ports, which means that the SMTP server is now connected to the local.
Reference:
Trying 127.0.0.1 ...
Connected to localhost.
Escape character is ^].
server.domain.com.br ESMTP Postfix (2.1.0)

These are the system output information that indicates that it is connected, and that the SMTP server is postfix.

"MAIL from:teste@dominio.com.br" This command indicates that the sender address is teste@dominio.com.br, "OK" indicates that the command is accepted by the server and executed correctly, similar to the HTTP protocol of 200, 404, Status code of 500. The next "RCPT to:teste@dominio.com.br" indicates that the pickup address is teste@dominio.com.br.
Reference:
DATA
354 END Data with .
Teste
.

This paragraph is to enter the message body, enter "DATA" after the system prompted to use "carriage return". "Enter" to end the input, the body content is "teste". The
ends with quit.

Above is the simplest process of sending mail, 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 some simple commands to it, a simple message sent out, As for the more complex content of the mail or operation, in fact, it is based on a little expansion.

This process is implemented in PHP, in fact, the use of PHP socket functions, Network functions and so on Operation socket function to establish a connection with the SMTP server, and then send the command of the text to the server, If you take a look at those well-written classes or functions that use the SMTP protocol to send mail, I believe you can attest to my claim.

Because there are already many ready-to-encapsulate classes or functions for us to complete the underlying socket-level operation, we just need to directly use it, and I will not time-consuming to discuss the underlying code in this article, have the spirit to study, I find the code to study it. Now go on with me, let's have some actual code to show how to use PHP to send mail, the class used is pear::mail.
code:
!--? php require_once M ail.php;

$conf [mail] = Array (
Host + xx.xx.xx.xx,//SMTP server address, IP address or domain name
Auth = True,//true indicates that the SMTP server needs to be
Username + tester,//user name
Password = retset//password
);

/***
* Use the $headers array to define the contents of the message header, such as using $headers[reply-to] to define a reply-to address
* In this way, you can easily customize the message header for outgoing messages
***/
$headers [from] = tester@domain.com;//Address
$headers [to] = tester@domain.com;//delivery address
$headers [Subject] = Test mail Send by PHP; Message header
$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)) { Detection error
Die ($mail _res->getmessage ());
}
?

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

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

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 specific needs. The above code is simple, but there are still a lot of people who don't know what the message header is in the note, and how it affects the messages that are sent.
For example, "Send HTML message why the other party is garbled" and so on, and so on may be related to the message header, if the SMTP protocol more understanding, you can quickly know where the problem lies.
2. MIME specification
If you want to send HTML mail or even multimedia mail, you must have a certain understanding of mime, with this knowledge you can send content more exciting messages.
3, PEAR
Pear is not the only tool for sending mail, but pear contains classes that have been packaged in mail, Mail_mime, and so on, which can make our development more effective, and in addition to the mail side, it provides many other tools that are readily available, and it is worth taking the time to learn.

http://www.bkjia.com/PHPjc/532627.html www.bkjia.com true http://www.bkjia.com/PHPjc/532627.html techarticle since PHP does not provide a ready-made SMTP function, it provides a less flexible mail () function that requires support on the server configuration and does not support SMTP authentication in many ...

  • 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.