[1.10.3] [2.4.0][2.4.2] HOWTO: use an external mailserver

« HOW-TO import/export databases - create backup :: [2.3.0] Custom Pages using /index.php?disp= »
Author Message
afwas
Posted: Thu Jan 31, 2008 07:03     Topic subject: [1.10.3] [2.4.0][2.4.2] HOWTO: use an external mailserver

If you cannot send mail through B2evo, probably because your host has disabled PHP's mail() function you must use another mailserver. There are other situations where you deliberately want to use this.
This solution will work with any SMTP server you have access to (can authenticate through username/password).

I had a successful go with [url=http://www.swiftmailer.org/]swift mailer[/url] in B2evo 1.10.3 and in 2.4.0 rc2. and in B2evolution 2.4.2 This is how to set it up:

  • 1) Download swift-smtp from http://www.swiftmailer.org/download
  • 2) Rename the folder of the unzipped package to /swift/
  • == OLD INFORMATION ==Unpack and upload the /lib/ folder. I made a directory in /inc/_misc/ called swift-smtp where I put the lib folder. So this looks like /inc/_misc/swift-smtp/lib/. Feel free to add the folder to /htsrv/ where I think it belongs.

  • 3) Upload the /swift/ folder to /blogs/htsrv/ on your server
  • 4) B2evolution 1.10.3: Open /inc/_misc/_misc.funcs.php and replace lines 2348 - 2362. If you are using B2evo v2.4.0 rc 2 look for this codesnippet in inc/misc/_misc.funcs.php line ~1568. In B2evolution v 2.4.2 the code is in /blogs/inc/_core/_misc.funcs.php starting at line 1565:
  • PHP

    if( $debug )
        {    // We agree to die for debugging...
            if( ! mail$to$subject$message$headerstring ) )
            {
                debug_die'Sending mail from
     «'.htmlspecialchars($from).'» to
     «'.htmlspecialchars($to).'», Subject
     «'.htmlspecialchars($subject).'» FAILED.' );
            }
        }
        else
        {    // Soft debugging only....
            if( ! @mail$to$subject$message$headerstring ) )
            {
                $Debuglog->add'Sending mail from
     «'.htmlspecialchars($from).'» to
     «'.htmlspecialchars($to).'», Subject
     «'.htmlspecialchars($subject).'» FAILED.''error' );
                return false;
            }
        }

    by:

    PHP

    //setup swift-smtp
        global $htsrv_path;
        require_once$htsrv_path '/swift-smtp/lib/Swift.php' );
        require_once$htsrv_path '/swift-smtp/lib/Swift/Connection/SMTP.php' );
        global $smtp_server$smtp_user$smtp_password$smtp_port;
        $smtp =& new Swift_Connection_SMTP$smtp_server$smtp_port);
        $smtp->setUsername$smtp_user );
        $smtp->setpassword$smtp_password );
     
        $swift =& new Swift($smtp);
        $mail =& new Swift_Message$subject$message);
     
        $to_name preg_replace'/^.*?<(.+?)>$/''$0'$to );
        $from_name preg_replace'/^.*?<(.+?)>$/''$0'$from );
        $to_email preg_replace'/^.*?<(.+?)>$/''$1'$to );
        $from_email preg_replace'/^.*?<(.+?)>$/''$1'$from );
     
        $new_to = new Swift_Address$to_email$to_name );
        $new_from = new Swift_Address$from_email$from_name );
     
        if( $debug )
        {    // We agree to die for debugging...
            if( ! $swift->send$mail$new_to$new_from ))
            {
                debug_die'Sending mail from
     &laquo;'.htmlspecialchars($from).'&raquo; to
     &laquo;'.htmlspecialchars($to).'&raquo;, Subject
     &laquo;'.htmlspecialchars($subject).'&raquo; FAILED.' );
            }
        }
        else
        {    // Soft debugging only....
            if( ! @$swift->send$mail$new_to$new_from ))
            {
            $Debuglog->add'Sending mail from
     &laquo;'.htmlspecialchars($from).'&raquo; to
     &laquo;'.htmlspecialchars($to).'&raquo;, Subject
     &laquo;'.htmlspecialchars($subject).'&raquo; FAILED.''error' );
                return false;
            }
        }
  • 5 I prefer the variables in /conf/_advanced.php. Somewhere put this block after you changed the data:
  • PHP

    /**
     * SMTP
     * uses swift-smtp
     */
    $smtp_server 'smtp.server.tld';
    $smtp_port 25;
    $smtp_user 'user';
    $smtp_password 'password';

    [/list:u]
    == UPDATE ==
    This snippet is obsolete: If you want to put the Swift mailer files in the /htsrv/ folder (/htsrv/swift-smtp/lib/) you change these lines of the code:

    PHP

    require_oncedirname(dirname(dirname(__FILE__))).'/htsrv/swift-smtp/lib/Swift.php' );
    require_oncedirname(dirname(dirname(__FILE__))).'/htsrv/swift-smtp/lib/Swift/Connection/SMTP.php' );

    Once again: I took the /lib/ folder from the package and put it in a custom made /swift-smtp/ folder. You may copy the /lib/ folder from swift-smtp directly to the /htsrv/ folder, but change the path accordingly.

    Thanks to dcihon for testing.

    Good luck

Back to top
 
afwas
Posted: Fri Feb 1, 2008 07:28

== UPDATE ==
Very unfortunately I found a bug when it is trying to send to and from 'fancy emailaddresses' like 'somebody <someone@something.org>'. The code presented above is the corrected one.

Back to top
 
afwas
Posted: Mon Aug 25, 2008 22:40

== UPDATE ==
This hack still works in B2evo v 2.4.2 but the location of the mail sending function has changed.

The code to replace is in /inc/_core/_misc.funcs starting on line 1565.

Some remarks:

  • I strongly advise to download Swift v 2.3.3 for PHP v4 since B2evolution is entirely written in PHP v4
  • On your server: in the folder /blogs/htsrv/ create a folder called /swift/
  • Upload the contents of the Swift package to that folder on the server. The download from swift comes in the folder /Swift-3.3.3-php4/ but it's easier if it's in /swift/ on the server.
  • The code above is a little changed (only the first lines) so it finds the /swift/ folder in /blogs/htsrv/
  • Tested on my blog
  • [/list:u]

    Good luck

Back to top