I studied how to use PHP to send emails. I summarized the following two methods: I. I used the mail () function built in PHP to read the manual, the code is written directly, as shown below? Php $ totest@163.com; recipient $ subjectTest; Subject $ messageThisisatestmail !; Body mail ($ to, $ su
I studied how to use PHP to send emails. I summarized the following two methods: I. I used the mail () function built in PHP to read the manual, the code is written directly, as shown below? Php $ to = test@163.com; // recipient $ subject = Test; // subject $ message = This is a test mail !; // Body mail ($ to, $ su
Today I studied how to use PHP to send emails. To sum up, there are two methods:
I. Use the built-in mail () function in PHP
After reading the manual, you can directly write the code as follows:
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. phpOn line10
It seems that there is an SMTP server in the local environment, so try someone else and change the code again:
The result is still incorrect:
Warning: Mail () [function. mail]: SMTP server response: 553 authentication is required, smtp2, DNGowKD7v5BTDo9NnplVBA --. 1171S2 1301220947 inD:/www/Zend/email. phpOn line9
It seems that verification information is required. How do I write verification information? Where can I configure it? After searching for the Internet for a long time, I did not find a reason. Finally, I read some other technical articles and came to a conclusion (because I don't know much about SMTP mail, so I don't know whether this conclusion is correct): to use the mail () function to send emails, you must have an email 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. This is troublesome. I don't want to integrate it. If you are interested, you can try it on your own. You can use IIS that comes with windows, or download other SMTP server software from the Internet, I won't say much about it.
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 have no servers and buy virtual hosts from the Internet,
The first method is unrealistic, so you 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.
For detailed installation methods, visit http://pear.php.net/package/mail.
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); $ 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:In this way, you do not need to install any software to send emails. You only need to include a PHP class, and then 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.