How to Send Email using Pear in PHP

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

Sure, you can send email in PHP using the PHP mail function.  But if you are doing a lot of sophisticated email sending, some might find it nicer to code in a object oriented fashion using Pear library’s Mail send method.  Even the manual says …

“If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package”

Assuming that your PHP environment has Pear package installed (here is how to check), let’s send email using Pear from to…

$recipients = '';
$headers['From'] = '';
$headers['To'] = '';
$headers['Subject'] = 'Sending test message using Pear';
$body = 'This is a test message sent using Pear';
$params['sendmail_path'] = '/usr/lib/sendmail';
$mail =& Mail::factory('sendmail', $params);
$result = $mail->send($recipients, $headers, $body);

If all goes well, the var_dump output should return bool(true).  If not, you get an PEAR_Error object like …

Error: “sendmail returned error code 127”

Note that the sendmail_path often is ‘/usr/lib/sendmail’.  However, it may be something else for your server.  In particular, if sendmail returned error code 127, then most likely the sendmail_path is not correct.

Using the mail backend instead

The Mail::factory takes “mail”, “sendmail”, or “smtp” as the backend for its first parameter. If you don’t want to figure out the sendmail_path, you can use the “mail” backend instead of the “sendmail” backend.  So write …

$mail =& Mail::factory('mail');


Code Explanation

We include Mail.php which is part of the Pear package.

Use Mail::factory to create a sendmail object $mail.  The factory needs to know the sendmail_path passed in to $params array.

Then use the created $mail object to send.  The send method takes recipients, message headers, and message email body which was built in the code above.   The send method will return true if successful.  So you should check for this.

Sending both HTML and Text Emails

The above sends text email.  To send both HTML and text so that those that can receive HTML will get the HTML format, we use …

$message = new Mail_mime();
$message->setTXTBody("This is the text version.");
$message->setHTMLBody("This is the <strong>HTML</strong> version.");
$recipients = '';
$headers['From'] = '';
$headers['To'] = '';
$headers['Subject'] = 'Sending test message using Pear';
$mail =& Mail::factory('mail');
$result = $mail->send($recipients, $message->headers($headers), $message->get());

If you have a large text email body that you don’t want to inline in the function, you can use file_get_contents() to read file off disk.   Note that for some shared webhosts, you may need to install the Mail_Mime package via the CPanel and alter your “include_path” in php.ini to include the location where the Mail_Mime package is installed.  Turning on error display temporarily with …

ini_set(‘display_errors’, ‘1’);

Can help you debug the path.

Remember that if you are testing this on your live server, delete the file afterwards.  Otherwise, everytime someone visits that page, the email will be sent out.