Guide to sending emails via SMTP using PHP ZZ

Source: Internet
Author: User
Tags http 200 php send mail
Use PHP send mail via SMTP newbie guide http://tech.163.com 09:40:32 Source: IT computer tutorial network comments 0 Forum

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.

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.