Tuesday, November 4, 2014

Fix Bluehost wikipedia email problem

I'm managing a website hosted by Bluehost.com.

From CPanel I installed wikipedia. However the email function does not work. It says: "bluehost Mailer returned: Failed to add recipient: [email] … Mailer returned: Unknown error in PHP's mail() function."

I followed instruction at http://www.mediawiki.org/wiki/Manual:$wgSMTP to add email SMTP setting to wiki/LocalSettings.php:

$wgSMTP = array(
 'host'     => "mail.example.com", 
 'IDHost'   => "example.com",      
 'port'     => 26,                 
 'auth'     => true,               
 'username' => "my_user_name",     
 'password' => "my_password"       
);

Still it does not work. I followed Bluehost's email setting page, use either SSL/TLS setting or non-SSL setting. Still it fails.

Finally I tried to find out where $wgSMTP is used: grep -i wgSMTH */*, and found it being used in /wiki/includes/UserMailer.php. It comes down to the function send().  When $wgSMTP is used, it uses Pear Mail; otherwise, it uses PHP Mail. Since I'm using PHP Mail in a personal project on the same server, I tried to check what's different it uses PHP Mail from my use. On line 359 of UserMailer.php, it has this code:

foreach ( $to as $recip ) {
      if ( $safeMode ) {
          $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers );
      } else {
          $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers, $wgAdditionalMailParams );
      }
}

I print out $headers, it is:

From: CareerAssist Wiki Return-Path: apache@www.careerassist.org Date: Tue, 04 Nov 2014 04:41:29 -0700 Message-ID: X-Mailer: MediaWiki mailer MIME-Version: 1.0 Content-type: text/plain; charset=UTF-8 Content-transfer-encoding: 8bit

Since I'm using the PHP's mail() function in a personal project on the same server and it works well, where I simply use: $headers = "From: [email]";

So I replace the $headers here with this simpler rendition:

foreach ( $to as $recip ) {
      if ( $safeMode ) {
          $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers );
      } else {
          $headers = "From: [email]";
          $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers, $wgAdditionalMailParams );
      }
}

Then it works! This also shows it takes the 2nd path here ($safeMode is false).

Thus I got wikipedia email working on Bluehost, with this hack into wikipedia's source code.


1 comment:

Unknown said...

Exclusive Discount: BlueHost At $3.45/Mo
Free ‘.com’ domain
24/7 technical support
WordPress integration
Unlimited traffic
30-day money-back guarantee
BlueHost WEb Hosting

Blog Archive

Followers