Sunday, March 23, 2008

Drupal SMTP Authentication with PEAR::Mail

If you want to use smtp authentication with Drupal you can use PEAR::Mail with drupal_mail_wrapper.


  1. Install PEAR::Mail. On linux you can just run the following command as root:
     pear install --alldeps Mail


  2. Create a new file, say includes/smtpmail/smtpmail.inc and paste the following code into it:

    <?php
    require_once 'Mail.php';
    require_once 'PEAR.php';

    function drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $headers) {
    global $smtpmail;

    $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;
    }


  3. Edit your settings.php and add the following at the end:

    $conf = array(
    'smtp_library' => 'includes/smtpmail/smtpmail.inc'
    );
    global $smtpmail;
    $smtpmail= array();
    $smtpmail["host"] = 'your.mail.host';
    $smtpmail["auth"] = TRUE;
    $smtpmail["username"] = 'your_user_name';
    $smtpmail["password"] = 'your_password';



From now on every call to drupal_mail will go through your drupal_mail_wrapper and will be sent using PEAR::Mail with SMPT authentication.

I'm sure some more serious drupaler would have made this a nice module, with configuration screens and the lot, but I'm too lazy today.

9 comments:

  1. Thanks for the info. Actually I'm quite a php-nitwit and simply can't find the file settings.php. Should it be somewhere in my drupal folder or is it somewhere in the server root or whatever? Is it possible that I simply can't access it?

    Thanks,

    wouter

    ReplyDelete
  2. try to find it in your drupal folder under sites/default/settings.php.

    ReplyDelete
  3. I forgot to mention that the above solution was tested with Drupal 5.2 and 5.3.

    I also forgot to mention that there is a drupal project SMTP Authentication Module that uses phpmailer

    ReplyDelete
  4. Hi Nir

    I hope you can help me.

    I have Pear installed on my Windows > Wamp server. I have tested the Pear > Mail and it works.

    I now want to use it with Drupal. I have followed your instructions however I get the following errors
    warning:

    Missing argument 2 for drupal_mail_wrapper(), called in C:\wamp\www\gallery\httpdocs\includes\mail.inc on line 177 and defined in C:\wamp\www\gallery\httpdocs\includes\smtpmail\smtpmail.inc on line 7.

    Missing argument 3, 4, 5, 6

    This makes sense because mail.inc is sending only a $message variable to the drupal_mail_wrapper function whilst drupal_mail_wrapper requires ($mailkey, $to, $subject, $body, $from, $headers).

    Perhaps I have a different version of Drupal to the one you were using, I am not sure.

    I am at a loss as to what to do because I tried using the SMTP_Auth module with PHPMailer to send my emails but my include path was incorrect and I don't know how to correct it.

    Perhaps you will be able to help me sort this out.

    Thanks in advance
    Amy

    ReplyDelete
  5. hey amy,
    which version of Drupal are you using? please post the relevant part of your mail.inc file.
    my code was written for drupal 5.x.
    /NL

    ReplyDelete
  6. Thank you so much! This worked very nicely for me. It seemed much easier than getting phpmailer installed.

    ReplyDelete
  7. By the way, for Drupal 6, all I had to do was adjust the smtpmail.inc such that it looks like:

    function drupal_mail_wrapper($message) {
    ...
    }

    and in this function, I replaced all instances of $to with $message['to'], $from with $message['from'], $body with $message['body'], etc.

    ReplyDelete
  8. This worked great for me, until I had an email going out with the following header, and the SMTP server refused to do it:

    From: Foo-books.com <info@foo-books.com>

    It was Drupal, not me, that had set up the address that way. Turned out that you need to have quotation marks around the name part of the address to make it go through.

    You can't post PHP code here, so my fix for this is over at drupal.org.

    ReplyDelete
  9. Hey Nir Levy, really great thoughts on this topic, I recently blogged about this as well Drupal 8 SMTP Mail

    ReplyDelete

[Due to much spam, comments are now moderated and will be posted after review]