Send email using SMTP Authentication by PHP PEAR package

I need to do some hacks on sending email using SMTP Authentication. So, I am looking for a good solution. Finally, I got very nice PHP component in PEAR. Let me say something about PEAR Package. PEAR stands for “The PHP Extension and Application Repository” and it is basically a framework and distribution system for reusable PHP components. The most important benefit is reusing the component in many applications. Installing PEAR is very easy in Ubuntu as well as Linux. Just follow me. .
In Ubuntu, you can install it from repositories. Type thin in terminal

sudo apt-get install php-pear

In other versions of Linux

wget http://pear.php.net/go-pear.phar
php go-pear.phar

After installing it properly, you can check that weather it works or not. Just type

pear version

I should show you PEAR, PHP, Zend Engine versions and so on.
Now make test.php file with following PHP code in server

<?php
  require_once 'System.php';
  var_dump(class_exists('System', false));
?> 

Then, save the file as test.php and run it. If it shows you: bool(true). Then, it’s okay. Your PEAR installing is done.
Now install Pear mail package. Type

sudo pear install mail
sudo pear install Net_SMTP

Now write a PHP script for sending email. . .

<?php
  require_once "Mail.php";

  $to = "Testo1 <[email protected]>";
  $body = "Hi,\n\n this is just test email ? ";

  $headers = array(
        'From' => 'Test <[email protected]>',
        'To' => $to,
        Subject' => 'test email'
  );

  $smtp = Mail::factory('smtp',array (
                  'host' => 'ssl://smtp.googlemail.com',
                  'port' => '465',
                  'auth' => true,
                  'username' => 'test', //gmail user name
                  'password' => 'type your password' // gmail password
              ));

  $mail = $smtp->send($to, $headers, $body);

  if (PEAR::isError($mail)) {
       echo(" < p>" . $mail->getMessage() . " </p > ");
  } else {
    echo("<p > Message successfully sent!</p >");
  }
?>

Done. Check you email account. There should be an email waiting for you.

 

Eftakhairul Islam

Hi, I'm Eftakhairul Islam, a passionate Software Engineer, Hacker and Open Source Enthusiast. I enjoy writing about technical things, work in a couple of startup as a technical advisor and in my spare time, I contribute a lot of open source projects.

 

5 thoughts on “Send email using SMTP Authentication by PHP PEAR package

  1. Do you have a trick to avoid gmail to think that our sent email was forged?
    The test mail of your example was systematically classed as SPAM.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Read previous post:
Install Apache Tomcat server with Open JDK on Ubuntu

I need to run JavaServer Pages(JSP) in one of my client projects. So, I installed Apache Tomcat server on my...

Close