


PHP uses Pear to send emails (Windows environment), pear sends emails_PHP tutorial
PHP uses Pear to send emails (Windows environment), pear sends emails
This article introduces the example of PHP using Pear to send emails, and the steps on how to install PHP Pear under Windows , shared with everyone for your reference, the specific content is as follows
1. PEAR installation
1. Introduction to PEAR
PEAR is the abbreviation of the PHP Extension and Application Repository. It is a code repository for PHP extensions and applications. Simply put, PEAR is to PHP what CPAN (Comprehensive Perl Archive Network) is to Perl.
The basic goal of PEAR is to develop into a knowledge base for PHP extension 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.
Install PEAR in WAMP integrated environment
1). First download a go-pear.phar file.
2) Enter the running directory of php to install
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:wampbinphpphp5.4.3>
Execute the following command:
php.exe -d phar.require_hash=0 go-pear.phar
At this step, you can modify the $prefix path of PEAR installation. I chose the default and pressed Enter all the way to install.
3). Modify the include_path option
Finally, after the PEAR default extension package is installed, you will be asked whether you need to configure the include_path option in the php.ini file to point to the pear installation directory
Of course it needs to be configured, otherwise it cannot be used, press Y and press Enter, the installation package will automatically modify the include_path option. It reminds us that the environment variables are not set, so we need to set the system environment variables next.
Create a new environment variable name, as shown below
Then add %php_home% to the PATH variable and you’re done~.
2. Common PEAR commands
In command line mode, you can use PEAR related commands to install the PEAR installation package you need.
- pear help Lists all commands of pear, similar to pear's command help command.
- pear help
List the specific information of a pear command - pear help shortcuts List the abbreviations of all pear commands
2. Install Mail, Mail_Mine, Net_SMTP
- Step one: Enter the PHP running directory in cmd, which is php.exe.
- Step 2: Use pear install Mail, pear install Mail_Mine, pear install Net_SMTP to install the class libraries needed to send emails
- Step 3: Use pear list to check whether the above three class libraries are installed.
3. Examples
<?php error_reporting(0); //PHP会出现不规范提示,故此设置 require_once "Mail.php"; //记得将Pear目录加入环境,才能这样引用,否则要引用绝对路径 $from = "sender@outlook.com"; //发送方 $to = "receiver@qq.com"; //接收方 $subject = "Hi!"; //主题 $body = "Hi,\n\nHow are you?"; //内容 $host = "smtp.live.com"; //SMTP服务器 $port = "587"; //端口 // $port = "25"; //两个端口都行 $username = "username@outlook.com";//用户名 $password = "password";//密码 $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); //邮件头 $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); //服务设置 //发送邮件 $mail = $smtp->send($to, $headers, $body); //错误处理 if (PEAR::isError($mail)) { echo("<p>". $mail->getMessage() ."</p>"); } else { echo("<p>Message successfully sent!</p>"); } ?>
4. How to send emails using pear:Net_SMTP class in php
Before using the following source code, please configure the pear path and download the net_smtp package.
Choose different setting methods according to your operating system in the php.ini file:
; UNIX: "/path1:/path2"
include_path = ".:./php/pear"
;
; Windows: "path1;path2"
;include_path = ".;c:phppear"
Code:
<?php require 'Net/SMTP.php'; $host = '126.com';//smtp服务器的ip或域名 $username= 'arcow';//登陆smtp服务器的用户名 $password= 'secret';//登陆smtp服务器的密码 $from = 'arcow@126.com'; //谁发的邮件 $rcpt = array('test@test.com', 'arcow@126.com');//可设多个接收者 $subj = "Subject: 你是谁\n";//主题 $body = "test it";//邮件内容 /* 建立一个类 */ if (! ($smtp = new Net_SMTP($host))) { die("无法初始化类Net_SMTP!\n"); } /* 开始连接SMTP服务器*/ if (PEAR::isError($e = $smtp->connect())) { die($e->getMessage() . "\n"); } /* smtp需要身份验证 */ $smtp->auth($username,$password,"PLAIN"); /*设置发送者邮箱 */ if (PEAR::isError($smtp->mailFrom($from))) { die("无法设置发送者邮箱为 <$from>\n"); } /* 设置接收邮件者 */ foreach ($rcpt as $to) { if (PEAR::isError($res = $smtp->rcptTo($to))) { die("邮件无法投递到 <$to>: " . $res->getMessage() . "\n"); } } /* 开始发送邮件内容 */ if (PEAR::isError($smtp->data($subj . "\r\n" . $body))) { die("Unable to send data\n"); } /* 断开连接 */ $smtp->disconnect(); echo "发送成功!"; ?>
The above is how PHP uses Pear to send emails. I hope this article will be helpful to everyone learning PHP programming.
Articles you may be interested in:
- PHP 5.0 Pear installation method
- php What is PEAR?
- PHP Pear installation and use
- How to install PEAR under windows and solve the error under php5.3.1
- How to install phpunit with pear package
- Install under windows pear and phpunit (pay attention to configuring the php command line environment)
- The difference between PHP extension modules Pecl, Pear and Perl
- How PHP uses pear’s own mail class library to send emails

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
