How to send mail using PHP PEAR mail with SMTP authentication

There are several ways to send mail in PHP. Using PEAR (PHP Extension and Application Repository) package to send mail with SMTP authentication is one of them. It is always better to use one which has an option for server authentication. You can specify mail server name, port and userid/password for authentication while sending mail. If you are interested you can also read below two topics for sending mail in PHP.

  1. How to send mail using mail() function in PHP with example
  2. How to send mail from localhost using Gmail SMTP server in PHP

In this topic I will discuss how to code in PHP to send mail using PEAR package. Below code can be used for sending the mail. We will discuss about it.

Install PEAR Package Mail

The first step is to check if the server has already PEAR package installed. In most hosting servers it is usually installed. Verify in the Software section in your hosting server after login into cPanel. PEAR packages will be available, install one which is for the mail. Once it is installed you will see a folder available for the software installed. In my case it is under php folder. If you are not sure about the installation part, contact your admin and make sure it is installed properly. You need to know the path of Mail.php.

You can use PEAR classes for the mail. Let us just simply write a form with three input fields and a submit button to send message. When the form is submitted, we will call a function which will send mail using PEAR mail.

PEAR Mail in PHP

Let us look at the code below for the form in index.php

When form is submitted, I have written below code to process it and call send_mail() function to send the mail. Let us see the PHP code after form is submitted

PHP code to call send_mail() function

Once the form is submitted, below PHP code for sending mail is written:

library.php is included because our send_mail() function is written in the library.php. I kept library.php in 'lib' folder. You can see it takes three input values for to_email, subject and message, and if validations are successful, it calls send_mail() function.

send_mail() is a custom function written in library.php to have a common function to send mail. Let us see the send_mail() function.

Function to send mail using PEAR Package

If PEAR package is installed, you need to set the path of the folder where mail package is installed. Let's see the code:

In this case Pear package is installed in the path /home/user/php. We need to include Mail.php file which is actually in /home/user/php directory.

Then, we are setting the SMTP server name, port, userid and password of the mail account. Then we are creating a PEAR mail instance in line 16, passing an array of values for SMTP authentication. send() method is used to send the mail.

I have given index.php and custom stylesheet here.

index.php

style.css

send mail using PEAR mail in phpConclusion

In your project, depending on the requirement, you can trigger an email after a transaction or when an activity is performed. You can write a routine for sending mail and call it from anywhere in the application with the parameters as it was shown above. Refer to the related topics below for other ways to send mail in PHP.