PHP uses Pear to send emails (in Windows) and pear to send emails _ PHP Tutorial

Source: Internet
Author: User
Tags learn php programming
PHP uses Pear to send emails (in Windows), and pear to send emails. PHP uses Pear to send emails (in Windows). This document describes how to use pear to send mails in PHP and how to install PHPPear in Windows, share an email with PHP using Pear (in Windows) and send an email with pear

This article introduces how to use Pear to send mails in PHP and how to install PHP Pear in Windows. The details are as follows:

1. install PEAR
1. PEAR introduction
PEAR is the abbreviation of the PHP Extension and Application Repository. It is a code repository for PHP extensions and applications. In short, PEAR is like CPAN (Comprehensive Perl Archive Network) to Perl in PHP.
The basic goal of PEAR is to develop into a knowledge base for PHP extension and library code, and the most ambitious goal of this project is to try to define a standard, this standard will help developers write portable and reusable code.
Install PEAR in the WAMP integration environment
First Download a go-pear.phar file.
2) go to the php running directory to install
I used a wamp integrated development environment. 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>
Run the following command:
Php.exe-d phar. require_hash = 0 go-pear.phar

In this step, you can modify the $ prefix path for PEAR installation. I have selected the default path and press enter to install it.
3) modify the include_path option
After the default PEAR extension package is installed, the system prompts whether to configure the include_path option in the php. ini file to point to the pear installation directory.

Of course, you need to configure it. Otherwise, you cannot use it. Y and press Enter. the installation package automatically modifies the include_path option. The prompt is that the environment variables are not set, so we need to set the environment variables of the system below.
Create an environment variable name, as shown in figure

Add % php_home % to the PATH variable ~.
2. common PEAR commands
In the command line mode, you can use PEAR-related commands to install the PEAR installation package you need.

  • Pear help lists all pear commands, similar to the pear command help command.
  • Pear helpList the details of a pear command
  • Pear help shortcuts short for listing all pear commands

2. install Mail, Mail_Mine, and Net_SMTP

  • Step 1: Go to the PHP runtime directory in the repository, and click php.exe.
  • Step 2: Use pear install Mail, pear install Mail_Mine, and pear install Net_SMTP to install the class library required for sending emails.
  • Step 3: Use pear list to check whether the above three class libraries are installed.

III. instances

<? Php error_reporting (0); // An error message is displayed in PHP. therefore, set require_once "Mail. php "; // Remember to add the Pear directory to the environment in order to reference this way, otherwise you need to reference the absolute path $ from =" sender@outlook.com "; // sender $ to =" receiver@qq.com "; // receiver $ subject = "Hi! "; // Subject $ body =" Hi, \ n \ nHow are you? "; // Content $ host =" smtp.live.com "; // SMTP server $ port =" 587 "; // port // $ port =" 25 "; // Both ports are supported $ username = "username@outlook.com"; // username $ password = "password"; // password $ headers = array ('from' => $ From, 'to' => $ To, 'subobject' => $ Subject); // Mail header $ smtp = Mail: factory ('smtp ', array ('host' => $ host, 'port' => $ port, 'auth' => true, 'username' => $ username, 'password' => $ password); // service settings // send email $ mail = $ smtp-> send ($ to, $ headers, $ body ); // error handling if (PEAR: isError ($ mail) {echo ("

". $ Mail-> getMessage ()."

");} Else {echo ("

Message successfully sent!

") ;}?>

4. php uses the pear: Net_SMTP class to send emails
Before using the following source code, configure the pear path and download the net_smtp package.
In the php. ini file, select different settings based on your operating system:
; UNIX: "/path1:/path2"
Export de_path = "..:./php/pear"
;
; Windows: "\ path1; \ path2"
; Include_path = ".; c: \ php \ pear"
Code:

<? Phprequire 'net/SMTP. php '; $ host = '2017. com '; // smtp server ip address or domain name $ username = 'arcow'; // username $ password = 'secret' for logging on to the smtp server '; // login smtp server password $ from = 'arcow @ 126.com '; // who sent the email $ rcpt = array ('test @ test.com', 'arcow @ 126.com '); // Multiple recipients can be set $ subj = "Subject: Who are you \ n"; // Subject $ body = "test it "; // email content/* create a class */if (! ($ Smtp = new Net_SMTP ($ host) {die ("Class Net_SMTP cannot be initialized! \ N ");}/* start to connect to the SMTP server */if (PEAR: isError ($ e = $ smtp-> connect ())) {die ($ e-> getMessage (). "\ n");}/* smtp Authentication Required */$ smtp-> auth ($ username, $ password, "PLAIN "); /* Set the sender's mailbox */if (PEAR: isError ($ smtp-> mailFrom ($ from ))) {die ("the sender's email address cannot be set to <$ from> \ n");}/* Set the recipient */foreach ($ rcpt as $ to) {if (PEAR:: isError ($ res = $ smtp-> rcp.pdf ($ to) {die ("the email cannot be delivered to <$ to> :". $ res-> getMessage (). "\ n") ;}}/* start to send the mail content */if (PEAR : IsError ($ smtp-> data ($ subj. "\ r \ n ". $ body) {die ("Unable to send data \ n");}/* disconnect */$ smtp-> disconnect (); echo "sent successfully! ";?>

The above is how PHP uses Pear to send emails. I hope this article will help you learn php programming.

Articles you may be interested in:
  • PHP 5.0 Pear installation method
  • Php what is PEAR?
  • Install and use PHP Pear
  • How to solve the error when installing PEAR php5.3.1 in windows
  • How to install phpunit in the pear package
  • Install pear and phpunit in windows (note that you have configured the php command line environment)
  • Differences between PHP extension modules Pecl, Pear, and Perl
  • PHP mail method using the mail class library provided by pear

In the Windows environment, pear sends emails. This article introduces how PHP uses Pear to send emails and how to install PHP Pear in Windows...

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.