In another post I showed how to send an email using the PHP 4/5 builtin function mail().

The mail() function is very simple and you can develop the code to send an email quickly. But if you have to do something more complex like the SMTP authentication you have to use something else.

For this tutorial we will use the PEAR Mail Package.

You can find all info about the PEAR Mail Package at the following address:

First of all, make sure the PEAR Mail package is installed.

Then go on writing some code:


// import the Mail package:

    require_once "Mail.php";

// setup the header

 $from = "Your Name <>";

 $to = "destination <>";

 $subject = "Using PEAR!";

// Setup the body of your mail message

 $body = "This mail has been sent using PEAR";

// Setup the server

 $host = "";

// Prepare username and password

 $username = "username";

 $password = "password";

// Setup the headers

 $headers = array ('From' => $from, 'To' => $to,  'Subject' => $subject);

// Prepare the email 

 $smtp = Mail::factory('smtp',

   array ('host' => $host,

     'auth' => true,

     'username' => $username,

     'password' => $password));

// Send the email

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

// Check for errors

 if (PEAR::isError($mail)) {

   echo("<p>" . $mail->getMessage() . "</p>");

  } else {

   echo("<p>Message successfully sent!</p>");