Two methods for sending php mail, two methods for sending mail _ PHP Tutorial

Source: Internet
Author: User
Php mail can be sent in two ways: mail can be sent in two ways. Php mail is sent in two ways. The main content of this article is to use PHP to send emails. the following two methods are summarized: i. use PHP built-in php to send emails in two ways:

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:You need an SMTP server locally and changed the following code:

<? 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, 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:

<? 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); $ 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.

The main content of this article is to use PHP to send emails. the following two methods are summarized: 1. use PHP built-in...

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.