Writing /home2/kitto/data/metakitto/data/cache/8/87bad6c3a52b74c313f078892f114f25.i failed
Writing /home2/kitto/data/metakitto/data/cache/8/87bad6c3a52b74c313f078892f114f25.xhtml failed

DokuWiki SMTP Patch

UPDATE: This patch can also be applied as-is to the 2007-06-26b version. I used WebFaction's one-click DokuWiki installation script to install a new DokuWiki installation and was able to apply the patch. There are two caveats however:

  1. I had to apply the patch from my home directory on WebFaction, because users don't have write permission to the webapps directory. That means I had to search/replace the “dokuwiki” paths in the patch to “webapps/dokuwiki”
  2. In the process I found a bug in the patch which basically caused the change-password emails to fail. The body text isn't being properly encoded for quoted-printable. To fix this, you have to add the line:
$body = mail_quotedprintable_encode($body);

right before the line:

$smtp = Mail::factory('smtp', array(

after the patch is applied.

It would be nice if I had time to redo the patch to make this easier for you. Sorry about that.

Requirements

This patch requires that your PHP installation have the PEAR Mail package already installed. Apparently this is quite common, and it was already installed here at WebFaction.

The patch works on versions 2007-06-26b and 2006-11-06. See note above for a bug that was introduced in the patch and how to fix it.

Patch instructions

  1. Copy this code into a new file on your server, say, patch.txt. Put it at the same level as your dokuwiki-2006-11-06 folder.
  2. Apply the patch something like this: patch -Np0 < patch.txt
    1. If you don't know what patch is, see Patch (Unix). There may be help there for Windows users. Mac users can access patch from the Terminal command line.
  3. As you can see from the patch itself, it only modifies a few files. The main code changes are in inc/mail.php, while there are other patches to make the Configuration Manager work with the new configuration settings.

Setup instructions

  1. Once the patch has been applied and you have verified that the basic DokuWiki installation is successful, login as an admin user and go to the Configuration Settings section of the Admin page.
  2. Way at the bottom you'll see some new fields:
    • smtp host: set this to the hostname of the SMTP server, e.g. mailx.webfaction.com
    • smtp auth: select the check box to use SMTP authentication
    • smtp from: the From address to use for outgoing mail 1)
    • smtp user: username for SMTP authentication
    • smtp pass: password for SMTP authentication
  3. If you leave smtp host blank, DokuWiki will operate as it did pre-patch. In other words, it will use PHP's mail() function and will not use SMTP.
  4. Don't forget to click Save. :-)

Caveats

This patch works for me, but it probably isn't fully complete for inclusion in DokuWiki's tree. Mostly it's missing the following features:

  • Nice names for the Configuration Settings boxes.
  • As noted by a FIXME comment in the code, if SMTP is activated, extra headers and mail params that DokuWiki code may be setting are ignored. As far as I can tell, these are not currently in use, so it's no big deal today. But it's not clean code to fail silently like that.
  • In theory, I suppose there should be an @ in front of the $smtp→send() call in mail.php, but I forgot to put one there.
  • if $to is empty, my SMTP-server rejected the mail. It tried the adress '@localhost' (nothing@localhost) I added these lines above the smtp-factory (see patch below)
//use $from, if $to is empty.
if ($to=='') {
  $to=$from;
}

The patch

Copy this code into a file on your server and follow the instructions above.

diff -Nbaur -X diff-exclude.txt dokuwiki-2006-11-06/conf/dokuwiki.php dokuwiki/conf/dokuwiki.php
--- dokuwiki-2006-11-06/conf/dokuwiki.php	2006-11-06 09:32:07.000000000 -1000
+++ dokuwiki/conf/dokuwiki.php	2006-12-15 19:18:06.000000000 -1000
@@ -130,3 +130,9 @@
 $conf['ftp']['pass'] = 'password';
 $conf['ftp']['root'] = '/home/user/htdocs';
 
+/* new conf settings for smtp */
+$conf['smtp']['host'] = '';     // if blank will use regular PHP mail()
+$conf['smtp']['auth'] = 0;
+$conf['smtp']['from'] = '';
+$conf['smtp']['user'] = '';
+$conf['smtp']['pass'] = '';
diff -Nbaur -X diff-exclude.txt dokuwiki-2006-11-06/inc/mail.php dokuwiki/inc/mail.php
--- dokuwiki-2006-11-06/inc/mail.php	2006-11-06 09:32:08.000000000 -1000
+++ dokuwiki/inc/mail.php	2006-12-15 19:31:16.000000000 -1000
@@ -14,6 +14,7 @@
   if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n");
   #define('MAILHEADER_ASCIIONLY',1);
 
+
 /**
  * UTF-8 autoencoding replacement for PHPs mail function
  *
@@ -34,6 +35,9 @@
  * @see    mail()
  */
 function mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){
+  global $conf;
+
+      
   if(defined('MAILHEADER_ASCIIONLY')){
     $subject = utf8_deaccent($subject);
     $subject = utf8_strip($subject);
@@ -47,6 +51,8 @@
   // No named recipients for To: in Windows (see FS#652)
   $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;
 
+  if ($conf['smtp']['host'] == '') {
+      // old version of mail_send, using php's mail()
   $to = mail_encode_address($to,'',$usenames);
   $header .= mail_encode_address($from,'From');
   $header .= mail_encode_address($cc,'Cc');
@@ -64,6 +70,44 @@
   }else{
     return @mail($to,$subject,$body,$header,$params);
   }
+    }
+    else {
+      // use Mail's SMTP facility
+      // FIXME ignores incoming $headers and $params arguments!!
+      require_once "Mail.php";
+      
+      // see if From is overriden from settings
+      if ($conf['smtp']['from'] != '')
+        $from = $conf['smtp']['from'];
+      
+      $from = mail_encode_address($from, '');
+      $to = mail_encode_address($to, '', $usenames);
+      $cc = mail_encode_address($cc, '');
+      $bcc = mail_encode_address($bcc, '');
+      
+      $headers = array(
+        'From' => $from,
+        'Subject' => $subject,
+        'Cc' => $cc,
+        'Bcc' => $bcc,
+        'MIME-Version' => '1.0',
+        'Content-Type' => 'text/plain; charset=UTF-8',
+        'Content-Transfer-Encoding' => 'quoted-printable',
+        );
+        
+        $smtp = Mail::factory('smtp', array(
+          'host' => $conf['smtp']['host'],
+          'auth' => $conf['smtp']['auth'],
+          'username' => $conf['smtp']['user'],
+          'password' => $conf['smtp']['pass'],
+          ));
+        
+        $result = $smtp->send($to, $headers, $body);
+        if (PEAR::isError($result))
+          return FALSE;
+        else
+          return TRUE;
+    }
 }
 
 /**
diff -Nbaur -X diff-exclude.txt dokuwiki-2006-11-06/lib/plugins/config/settings/config.metadata.php dokuwiki/lib/plugins/config/settings/config.metadata.php
--- dokuwiki-2006-11-06/lib/plugins/config/settings/config.metadata.php	2006-11-06 09:32:08.000000000 -1000
+++ dokuwiki/lib/plugins/config/settings/config.metadata.php	2006-12-15 19:17:57.000000000 -1000
@@ -177,3 +177,8 @@
 $meta['ftp____pass']  = array('password');
 $meta['ftp____root']  = array('string');
 
+$meta['smtp____host'] = array('string');
+$meta['smtp____auth'] = array('onoff');
+$meta['smtp____from'] = array('string');
+$meta['smtp____user'] = array('string');
+$meta['smtp____pass'] = array('string');

Comments

1) E.g., WebFaction's SMTP servers requires that the From address be a known WebFaction-assigned address.
 
wiki/dokuwiki_smtp_patch.txt · Last modified: 2007/12/13 01:13 by kitto
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki