This article is an article based on php's idea of sending mails. It provides different solutions for different problems, if you are interested, you can refer to this article, which is an article based on php's idea of sending emails. different solutions are provided when you encounter different problems, for more information, see
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
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:You need an SMTP server locally and changed the following code:
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, I recommend that youMail class in PEAR extension,Powerful functions:Supports text-only and HTML-format emails. you can set the encoding for each field. the correct configuration does not contain Chinese garbled characters. attachments are supported.
Available on the serverPear install MailCommand 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:
"; // 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); $ he Aders = 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 {// echo sent successfully "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.