Has anyone successfully used PEAR to send mail instead of sendmail or smtp? My host doesn't allow for sending mail except through the PEAR modules.


smooshy’s picture

It's easier than I thought in Drupal 5.x. I created a pearmail.inc and copied that to includes/ and then added smtp_library conf in settings.php to point to pearmail.inc.


 * Send mail using PEAR Mail Factory
function drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $headers) {
    $headers['From']     = $from;
    $headers['To']        = $to;
    $headers['Subject']    = $subject;

    $params['host']     = ini_get('SMTP');

    $smtp =& Mail::factory('smtp', $params);

    $mail = $smtp->send($to, $headers, $body);
    if ( PEAR::isError($mail) ) {
        watchdog('debug', $mail->getMessage());
        return false;
    else {
        watchdog('debug', 'Mail successfully sent!');
    return true;
Christefano-oldaccount’s picture

Thanks for posting your solution. Very elegant.

It's great when people come back to share what works.

nirl’s picture

dan3h’s picture

@nirl's solution that he links to in the previous comment is good and works for me. But we had one obscure problem with it.

These two header values (coming from Drupal) were causing our ISP to reject our send-attempt:

     [Reply-To] => Foo-books.com <info@foo-books.com>
     [From] => Foo-books.com <info@foo-books.com>

I think it's because the "name" part of the email address looks like a mal-formed email address. By forcing it to Foobooks Com <info@foo-books.com> or "Foo-books.com" <info@foo-books.com>, then it the SMTP server honours the request and sends the email.

So we look through the headers, and insert quotes if necessary, to make it look like this:

     [Reply-To] => "Foo-books.com" <info@foo-books.com>
     [From] => "Foo-books.com" <info@foo-books.com>

To implement this fix, insert the following code snippet in the smtpmail.inc file (which @nirl suggests creating) after the headers are set, and before you do the actual send.

  foreach ($headers as $key => $val) {
    $low_key = strtolower($key);
    if ($low_key == 'reply-to' || $low_key == 'from') {
      // This regexp looks for an un-quoted name followed by an email addr in pointy brackets.
      if (preg_match('#^\s*([^"]+)\s+<([^@]+@[^@]+\.[^@]+)>#', $val, $matches)) {
        // Set to revised value.  Match 1 is name, match 2 is the email addr.
        $headers[$key] = sprintf(""%s" <%s>", $matches[1], $matches[2]);
zimbo’s picture

As said in nirl's link this solution only works in drupal 5.x. This is a fix to pearmail.inc for drupal 6.x:

require_once 'Mail.php';
require_once 'PEAR.php';

function drupal_mail_wrapper($message) {

global $smtpmail;

$to = $message['to'];
$from = $message['from'];
$subject = $message['subject'];
$headers = $message['headers'];
$body = $message['body'];

$headers['From'] = $from;
if ( empty($headers['To'])) {
$headers['To'] = $to;
$headers['Subject'] = $subject;

$recipients = $to;
$mail_object =&Mail::factory('smtp', $smtpmail);
$output = $mail_object->send($recipients, $headers, $body);
return $output;

Hawaiipersson’s picture

Is there any one who could help me with a fix for Pears MAIL:FACTORT in Drupal 7? I've tried to follow the instructions, but it feels like there's a difference between D6 and D7...