PEAR::Mail
with drupal_mail_wrapper
.- Install PEAR::Mail. On linux you can just run the following command as root:
pear install --alldeps Mail
- 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;
} - 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.
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?
ReplyDeleteThanks,
wouter
try to find it in your drupal folder under sites/default/settings.php.
ReplyDeleteI forgot to mention that the above solution was tested with Drupal 5.2 and 5.3.
ReplyDeleteI also forgot to mention that there is a drupal project SMTP Authentication Module that uses phpmailer
Hi Nir
ReplyDeleteI 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
hey amy,
ReplyDeletewhich 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
Thank you so much! This worked very nicely for me. It seemed much easier than getting phpmailer installed.
ReplyDeleteBy the way, for Drupal 6, all I had to do was adjust the smtpmail.inc such that it looks like:
ReplyDeletefunction 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.
This worked great for me, until I had an email going out with the following header, and the SMTP server refused to do it:
ReplyDeleteFrom: 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.
Hey Nir Levy, really great thoughts on this topic, I recently blogged about this as well Drupal 8 SMTP Mail
ReplyDelete