How to Send Email using Pear in PHP
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 php.net 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 somesender@example.com to person@example.net…
<?php include('Mail.php');
$recipients = 'person@example.net';
$headers['From'] = 'somesender@example.com'; $headers['To'] = 'person@example.net'; $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);
var_dump($result); ?>
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');
instead.
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 …
require_once('Mail.php'); require_once('Mail/mime.php');
$message = new Mail_mime(); $message->setTXTBody("This is the text version."); $message->setHTMLBody("This is the <strong>HTML</strong> version.");
$recipients = 'person@example.net';
$headers['From'] = 'somesender@example.com'; $headers['To'] = 'person@example.net'; $headers['Subject'] = 'Sending test message using Pear';
$mail =& Mail::factory('mail');
$result = $mail->send($recipients, $message->headers($headers), $message->get());
var_dump($result);
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 …
error_reporting(E_ALL);
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.