This article is an article based on php's idea of sending emails. Different solutions are provided for different problems. If you are interested, refer to it.
This article is an article based on php's idea of sending emails. Different solutions are provided for different problems. If you are interested, refer to it.
The main content of this article is to use PHP to send emails. The following two methods are summarized:
I. Use the built-in mail () function in PHP
<? Php $ to = "test@163.com"; // recipient $ subject = "Test"; // subject $ message = "This is a test mail! "; // Body mail ($ to, $ subject, $ message );
An error is reported as follows:
Warning: mail () [function. mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php. ini or use ini_set () inD:/www/Zend/email. php on line 10
Cause analysis: the local SMTP server is required, and the code is changed:
<? Php $ to = "test@163.com"; // recipient $ subject = "Test"; // Email subject $ message = "This is a test mail! "; // Mail body ini_set ('smtp ', 'smtp .163.com'); // sending SMTP server ini_set ('smtp _ port', 25 ); // sender SMTP server port ini_set ('sendmail _ from', "admin@163.com"); // sender mail ($ to, $ subject, $ message );
The result is still incorrect:
Warning: mail () [function. mail]: SMTP server response: 553 authentication is required, smtp2, DNGowKD7v5BTDo9NnplVBA --. 1171S2 1301220947 inD:/www/Zend/email. php on line 9
Cause Analysis: verification information is required. How do I write verification information? Where can I configure it? With these questions, I came to the conclusion that using the mail () function to send emails requires a mail server that can send emails without SMTP authentication. However, the current SMTP Mail Server basically needs to be verified, so if you want to use it to send emails, you have to build a local SMTP server that does not need to be verified. Build method: use IIS provided by windows, or download other SMTP server software from the Internet.
Conclusion: To use the mail () function to send emails, you must have an SMTP server that does not need to be verified. In this way, the configuration will work a little more, but it will be easier to use, just a few lines of code.
Ii. Use the mail class that encapsulates the SMTP protocol
This method is quite common, especially for students who do not have servers and buy virtual hosts from the Internet. The first method is unrealistic, so they should use the SMTP protocol to send emails.
However, to do this, you need to have a certain understanding of the SMTP protocol. If you like it, you can write it yourself. If you like it, you can download it from the Internet, there are many.
However, we recommend that you use the Mail class in the PEAR extension. It has powerful functions: it supports plain text and HTML-format emails. You can set encoding for each field. The correct configuration does not contain Chinese garbled characters; attachments are supported.
On the server, you can use the pear install Mail command for quick installation. If you do not have sufficient server permissions, you can directly download the PHP source code of the class to include it.
Note: The Mail class depends on Net/SMTP. php and Mail/mime. php, which must be downloaded and included in use.
The following is an example of how to send emails in Mail. The usage of other SMTP emails on the internet is similar. For details, refer:
<? Php // Pear Mail Extension require_once ('mail. php '); require_once ('mail/mime. php '); require_once ('net/SMTP. php '); $ smtpinfo = array (); $ smtpinfo ["host"] = "smtp.163.com"; // SMTP server $ smtpinfo ["port"] = "25 "; // SMTP server port $ smtpinfo ["username"] = "username@163.com"; // sender's mailbox $ smtpinfo ["password"] = "password "; // sender's email password $ smtpinfo ["timeout"] = 10; // network timeout, seconds $ smtpinfo ["auth"] = true; // login verification // $ smtpinfo ["debug"] = true; // debug mode // recipient list $ mailAddr = array ('receiver @ 163.com '); // sender display information $ from = "Name "; // Recipient display information $ to = implode (',', $ mailAddr); // mail title $ subject =" this is a test email "; // mail body $ content = "whatever you want to write"; // mail body type, format and encoding $ contentType = "text/html; charset = UTF-8 "; // newline character Linux: \ n Windows: \ r \ n $ crlf = "\ n"; $ mime = new Mail_mime ($ crlf ); $ mime-> setHTMLBody ($ content); $ param ['text _ charset'] = 'utf-8 '; $ param ['html _ charset'] = 'utf-8'; $ param ['head _ charset'] = 'utf-8 '; $ body = $ mime-> get ($ param); $ headers = Array (); $ headers ["From"] = $ from; $ headers ["To"] = $ to; $ headers ["Subject"] = $ subject; $ headers ["Content-Type"] = $ contentType; $ headers = $ mime-> headers ($ headers); $ smtp = & Mail: factory ("smtp ", $ smtpinfo); $ mail = $ smtp-> send ($ mailAddr, $ headers, $ body); $ smtp-> disconnect (); if (PEAR :: isError ($ mail) {// send failed echo 'email sending failed :'. $ mail-> getMessage (). "\ n";} else {// sent successfully echo "success! \ N ";}
If the SMTP classes found on the Internet are highly encapsulated, it is easier to use than above, but the usage is similar.
Conclusion: you do not need to install any software to send emails in this way. You only need to include a PHP class and write a few more lines of configuration code. There are also a lot of sample code on the Internet. In many cases, you only need to copy the code and modify several parameters. This method is very convenient. We recommend that you use this method.
The above are two ways to use PHP to send emails. I have my own thinking process and hope it will help you learn.