How to set up email bounce handling using SilverStripe and cPanel - Burnbright Websites

How to set up email bounce handling using SilverStripe and cPanel

Posted by Jeremy on 2 December 2009 | 3 Comments

Tags: ,

These are instructions for setting up bounce handling for SilverStripe, and assumes you use cPanel for site managment. You should be able to mirror these steps accross to other systems also.

Here is the SilverStripe documentation on the silverstripe bounce handling: http://doc.silverstripe.org/doku.php?id=email_bouncehandler

  • Download this file: BounceEmailTask.php , and store it in your SilverStripe /mysite/code/ directory. This class handles incoming emails, and determines if they are bounces, and then records the bounce accordingly. Sam Minnee from SilverStripe provided the original code, and I converted it into a SS ScheduledTask (even though its not a cron job task). More about ScheduledTasks here
  • Set up a bounces@yoursite.com email address
  • Set up a cPanel filter that pipes emails that are "To: bounces@yoursite.com" to a program:

    "| php -q /home/yourusername/public_html/sapphire/cli-script.php /BounceEmailTask"

    (Everything before  /sapphire/cli-script.php may differ on your hosting)
    I also set another rule for the bounces to be delivered to the inbox, just incase they are needed.
  • Make sure you have your site path set in your _ss_environment.php:

    global $_FILE_TO_URL_MAPPING;
    $_FILE_TO_URL_MAPPING['/home/yourusername/public_html'] = 'http://www.yoursite.com';

Again the site path could differ on your server.

Test by sending a newsletter, or email to an address that is very likely to bounce, eg: bouncemeplease23jkzkkkd@gmail.com

 

Feel free to modify the BounceEmailTask class to suit your needs.

Let me know of any problems or issues you have setting this up.


Post your comment

Comments

  • Thanks for code!

    Posted by torrent, 28/07/2010 1:55am (3 days ago)

  • good job!

    Posted by tadd, 05/06/2010 1:13am (2 months ago)

  • Hi there,
    Thank you for posting this helpful piece on handling bounces with CPanel. I'm currently in the process of troubleshooting an issue with emails not being sent from Silverstripe on shared hosting. In tracking down the problem, I'm focusing on lines 156-158 from Mailer.php:

    if(!($result = @mail($to, $subject, $fullBody, $headers, "-f$bounceAddress"))) {
       $result = mail($to, $subject, $fullBody, $headers);
    }

    I'm assuming that mail() is accepting the arguments passed to the first mail() command the conditional, including -f$bounceAddress, but something is going wrong thereafter.

    In testing this, I have included a bunch of kitchen-sink commands just to send anything at all.

    function SendContactForm($data) {
       //Set data
       $From = $data['Email'];
       $To = $this->Mailto;
       $Subject = "Website Contact message";    
       $email = new Email($From, $To, $Subject);
       //set template
       $email->setTemplate('ContactEmail');
       //populate template
       $email->populateTemplate($data);
       //send mail
       $email->send();
       //return to submitted message
       $e = new Email();
       $e->To = "bouncemeplease23jkz23222kkkd@gmail.com";
       $e->Subject = "Plaintext Hi there";
       $e->Body = "Plaintext I just really wanted to email you and say hi.";
       $e->send();
       $f = new Email();
       $f->To = "alternium@gmail.com";
       $f->Subject = "Plaintext Hi there";
       $f->Body = "Plaintext I just really wanted to email you and say hi.";
       $f->send();

       Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
    }

    I have defined, in _config.php:
    define('BOUNCE_EMAIL','bounces@alternium.net');

    I have defined, in _ss_environment.php:
    global $_FILE_TO_URL_MAPPING;
    $_FILE_TO_URL_MAPPING['/home/emadsen/public_html/s/'] = 'http://www.alternium.net/s/';

    I have saved BounceEmailTask.php into mysite/code, built the DB and created the table Email_BounceRecord.

    I have set up an email address to catch bounces: bounces@alternium.net and, since my host's CPanel automatically prepends forwards with the home location, I've tried both of these pipes at different times:

    |/home/emadsen/php -q /home/emadsen/public_html/sapphire/cli-script.php/BounceEmailTask
    |/home/emadsen/public_html/sapphire/cli-script.php/BounceEmailTask

    Obviously the first didn't work, but I had to try. I'm still getting no emails and Email_BounceRecord is empty. Can you give me a tip where you think I might start looking next? I'm really banging my head against a wall, here.

    Posted by Erin, 11/03/2010 9:56am (5 months ago)