Guide to sending emails via SMTP using PHP

Source: Internet
Author: User
Because php does not provide a ready-made smtp function, it provides a mail () function with less flexible functions. this function requires server configuration support and does not support smtp verification.

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:

  1. [Root @ server ~ /] # Telnet localhost 25
  2. Trying 127.0.0.1...
  3. Connected to localhost.
  4. Escape character is '^]'.
  5. 220 server.domain.com.br ESMTP Postfix (2.1.0)
  6. Mail from: teste@dominio.com.br
  7. 250 OK
  8. Rcpt to: teste@dominio.com.br
  9. 250 OK
  10. DATA
  11. 354 End data .
  12. Teste
  13. .
  14. 250 OK: queued as 7B41F4665A
  15. QUIT
  16. 221 Bye
  17. Connection closed by foreign host.

Note: The postfix document from netkiller is lazy and is ready for use. first, telnet is used to connect to the local port 25. anyone familiar with the network knows that the smtp protocol uses port 25, this is to say, now connecting to the local smtp server, reference:

  1. Trying 127.0.0.1...
  2. Connected to localhost.
  3. Escape character is '^]'.
  4. 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.

  1. 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.
  2. Reference:
  3. DATA
  4. 354 End data .
  5. Teste
  6. .

This section is the body of the email. after entering "DATA", the system prompts you to use" <回车> . <回车> "To end the input, the body is" teste ", and then exit with" QUIT.

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, send some simple commands to it, and a simple email is sent out. as for emails or operations with more complex content, it is actually a little extended on this basis.

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.