1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to setup email bouncing via a script 2014-11-30

bypass the need to manage mailboxes

  1. Jim Boy
    Compatible XF Versions:
    • 1.4
    Additional Requirements:
    Linux O/S
    Not Shared server
    Postfix
    This tutorial assumes that you are run XenForo on a linux server which you control and that you are comfortable in a non-gui environment. If you are running on a shared server, then it is unlikely that you will have the necessary access for mail configuration. If you aren't running on your own box with linux, stick to the normal way of doing things. If you are running your own box, and running something like cpanel, plesk, webmin or any other management layer then again it is inadvisable to go down this route, especially with cpanel.

    There are three components to this, Networking, mail config and XenForo changes.

    1. Networking. A domain will need to have its MX record pointing at this box. It needn't be, in fact it shouldn't be the domain of your installation, it can however be a subdomain, so if your domain is myforum.com. Set up some subdomain like b.myforum.com and point your MX record for that at your forum web server. How do you setup an MX record? It depends on the provider doing your DNS hosting, you'll have to refer to them. You'll also need to allow incoming traffic on port 25 to your web server.

    2. Mail Configuration. On our server, we have gone for postfix, although no doubt this could be done with sendmail as well. You'll have to check what mail server, if any is installed. Commonly sendmail is installed and postfix isn't. Use your package manager to install postfix (eg sudo yum install postfix or sudo apt-get install postfix). Dont forget to turn off Sendmail and stop it from autostarting
    Code:
    sudo service sendmail stop && sudo chkconfig sendmail off
    If you are doing other things with your email server, including sending from your web server relaying from other servers on your network, it should be fine to continue with that, that's what we do. However I make no guarantees.

    To configure postfix you need to do the following. In this tutorial I am using the domain "bnc.yourdomain.com" - replace this with whatever domain you are using. In this example we are going to use a 'transport map' to define a rule as to how email will get handled when sent. There are other ways of achieving the same result, but ths worked for me.

    Add domain to your mail server by editing "virtaul"

    Code:
    cd /etc/postfix
    sudo nano virtual
    at the end of the file, add the following
    Code:
    bnc.yourdomain.com  this-text-is-ignored
    postmaster@bnc.yourdomain.com  postmaster
    @bnc.yourdomain.com  @bounced
    
    you then have to put it into readable form for postfix

    Code:
    sudo postmap virtual
    
    Edit main.cf
    Code:
    sudo nano /etc/postfix/main.cf
    
    and add the following to the end of the file
    Code:
    transport_maps = regexp:/etc/postfix/redirect.regexp
    myXenForoPipe_destination_rate_delay = 0s
    myXenForoPipe_destination_concurrency_limit = 1
    
    Whilst in main.cf, you should also ensure the following line exists and is uncommented
    Code:
    virtual_alias_maps = hash:/etc/postfix/virtual
    
    Next create and edit the redirect.regexp file
    Code:
    sudo nano /etc/postfix/redirect.regexp
    
    and add the following
    Code:
    /^.*@bnc\.yourdomain\.com/  myXenForoPipe:
    /^.*@bounced/ myXenForoPipe:
    
    You then need to put that file into postfix readable form
    Code:
    sudo postmap /etc/postfix/redirect.regexp
    
    Now you have to edit master.cf
    Code:
    sudo nano /etc/postfix/master.cf
    
    And add the following (including indent) to the end of the file

    Code:
    myXenForoPipe unix - n n - - pipe
      flags=O user=nginx argv=/path/to/php /path/to/forum/base/emailbounce.php
    
    Change the user to a valid user on your server, eg www-data. Also change the path in argv to match the base location of your XenForo installation, eg where index.php is located.

    All done- you just need to stop start postfix
    Code:
    sudo service postfix restart
    
    3 The XenForo bit.

    The attachment is a single file, emailbouce.php.zip, and this should be unzipped and placed in the root of your installation , alongside index.php and admin.php. The file itself is a copy of deferred.php with a few changes including:
    * Removal of the deferred functionality
    * A check to ensure that this can only be run from the command line
    * A line to capture piped input (the email) into a variable ($data = file_get_contents("php://stdin"))
    * Execution of standard XenForo bounce processing
    Code:
      $emailModel = XenForo_Model::create('XenForo_Model_EmailBounce');
      $emailModel->processBounceEmail($data);
    
    Note that as the script expects data to be piped in, it will hang if you try and call it any other way

    "php emailbounce.php" = bad
    "echo data|php emailbounce.php" = good

    Post Implementation

    I always advise checking for an open relay after reconfguring an SMTP server. If you inadvertantly turn on open relay for your SMTP server, you'll probably get abused by spammers. To test, go to http://www.dnsgoodies.com/ and do the Openrelay check
    Robru likes this.