This example describes how PHP uses pear to send mail, and how to install PHP pear under Windows to share it with you for your reference, as follows
One, pear installation
1, Pear introduction
Pear is an abbreviation of the PHP extension and the application library (the PHP Extension and application Repository). It's a code warehouse for PHP extensions and apps, simply put, Pear's php is like CPAN (Comprehensive Perl Archive Network) to Perl.
The basic goal of pear is to develop a knowledge base for PHP extensions and library code, and the most ambitious goal of the project is to try to define a standard that will help developers write portable, reusable code.
Installing pear in the WAMP integrated environment
1), first download a Go-pear.phar file.
2, into the PHP run directory installation
I use a WAMP integrated development environment here, the version is wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24-32b, my local installation path is C:\wamp\bin\php\php5.4.3 >
Execute the following command:
php.exe-d phar.require_hash=0 Go-pear.phar
To this step, you can modify the $prefix path of pear installation, I chose the default, all the way carriage return to install.
3), modify the Include_path option
Finally, after the Pear default expansion pack is installed, you are prompted to configure the Include_path option in the php.ini file to point to the installation directory of Pear
Certainly need to configure, otherwise cannot use, Y and enter, the installation package will automatically modify the Include_path option. It prompts us that the environment variable is not set, so we need to set the environment variables for the system below.
Create a new environment variable name, as shown below
Then add%php_home% to the path variable to complete it.
2, pear common commands
At the command line, you can install the Pear installation package that you want with the related commands of pear.
- Pear help lists all of pear's commands, similar to that of Pear's command assistance command.
- Pear Help <command> list specific information for a pear command
- Pear Help shortcuts lists the abbreviations for all pear commands
Second, install MAIL,MAIL_MINE,NET_SMTP
- The first step: in the cmd into the PHP running directory, that is, Php.exe.
- Step Two: Use pear install mail,pear install mail_mine,pear install NET_SMTP to install the class library for outgoing mail
- Step Three: Use Pear list to see if the above three class libraries are installed.
Iii. examples
<?php error_reporting (0); PHP will appear nonstandard prompts, so set require_once "mail.php"; Remember to add the Pear directory to the environment in order to this reference, or to refer to the absolute path $from = "sender@outlook.com"; The Sending party $to = "receiver@qq.com"; Receiver $subject = "hi!"; Theme $body = "Hi,\n\nhow Are You"; Content $host = "smtp.live.com"; SMTP Server $port = "587"; Port//$port = "25"; Two ports $username = "username@outlook.com";//username $password = "password";//password $headers = array (' From ' =& Gt $from, ' to ' => $to, ' Subject ' => $subject); Message header $SMTP = mail::factory (' SMTP ', Array (' Host ' => $host, ' Port ' => $port, ' auth ' => t Rue, ' username ' => $username, ' password ' => $password));
Service setup//Send mail $mail = $smtp->send ($to, $headers, $body); Error-Handling if (Pear::iserror ($mail)) {echo ("<p>". $mail->getmessage (). "
</p> "); else {echo ("<p>messageSuccessfully sent!</p> ");
}?>
Four, the way PHP uses the PEAR:NET_SMTP class to send mail
before using the following source code, configure pear's path and download the NET_SMTP package.
Choose a different setting for your operating system in the php.ini file:
; UNIX: "/path1:/path2"
include_path = ".:./php/pear"
;
; Windows: "\path1;\path2"
; include_path = ".; C:\php\pear "
code:
<?php require ' net/smtp.php '; $host = ' 126.com ';//smtp server's IP or domain name $username = ' Arcow ';//Login SMTP server username $password = ' secret ';//Login SMTP server password $from = ' Arcow@126.com ';
Who sent the mail $rcpt = Array (' test@test.com ', ' arcow@126.com '),//can set multiple recipients $subj = "Subject: who are you \ n"/subject $body = "Test it";//content of the message /* Create a class * */if (! ($smtp = new Net_smtp ($host)))
{Die ("Unable to initialize class net_smtp!\n");} /* Start connecting SMTP server/if (pear::iserror ($e = $smtp->connect ())) {Die ($e->getmessage ()).
"\ n");
/* SMTP requires authentication/$SMTP->auth ($username, $password, "PLAIN");
/* Set the sender's mailbox/if (Pear::iserror ($smtp->mailfrom ($from)) {die ("Cannot set the sender mailbox to < $from >\n");} /* Set receive Mail/foreach ($rcpt as $to) {if (Pear::iserror ($res = $smtp->rcptto ($to))) {Die ("mail cannot be delivered to < $to:". $res->getmessage ().
"\ n"); /* Start Sending message content//if (Pear::iserror $smtp->data ($subj. "\ r \ n".
$body))) {Die ("Unable to send data\n");}
/* Disconnect * * $SMTP->disconnect (); echo "Send success!" ";"
This is the way PHP uses pear to send mail, and I hope this article will help you learn about PHP programming.