Home Backend Development PHP Tutorial PHP uses Pear to send emails (Windows environment)_php tips

PHP uses Pear to send emails (Windows environment)_php tips

May 16, 2016 pm 08:00 PM
pear php mail

The example in this article introduces the method of using PHP to send emails, as well as the steps on how to install PHP Pear under Windows. It is 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 prompts us that the environment variables are not set, so next we need to set the system environment variables.
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

<&#63;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&#63;"; //内容 
 
    $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>"); 
    } 
 
&#63;> 
Copy after login

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:

<&#63;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: 你是谁
";//主题
$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 "发送成功!";
&#63;>
Copy after login

The above is how PHP uses Pear to send emails. I hope this article will be helpful to everyone learning PHP programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles