1

I am building a website using SilverStripe.

How do I make SilverStripe send an email notification every time a person visits a page? In the email I would like it to say what the user's IP address is.

3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • Hi Maciej. Do you have any code to share to show what you have tried so far? Did you get stuck somewhere? – 3dgoo Oct 22 '15 at 22:29
  • 3
    There is a number of potential issues with this if it was implemented. If many people hit the site, many emails will be sent to the point that the email server may be blacklisted for sending on the surface what could be spam. I suggest if you are certain you want IP addresses of everyone hitting any page on your site, you log it into the DB with a date/time and URL, sending yourself a summary email once a day. – Turnerj Oct 22 '15 at 22:32

1 Answers1

3

Instead of sending an email notification to track when someone visits your pages, I suggest using something like Google Analytics. Although Google Analytics does not track the IP address of the user, it does track lots of other useful information such as the pages that were visited, where the user came from, the user's location, and lots more.

That said, here is how to send an email each time a page is visited. The following code is for SilverStripe 3.1 / 3.2.

Here is how to send an email whenever any page is visited:

class Page_Controller extends ContentController {

    public function init() {
        parent::init();

        $to = 'to@example.com';
        $from = 'from@example.com'; 
        $subject = $this->Title . ' - page visit';
        $content = $_SERVER['REMOTE_ADDR'];

        $email = Email::create($from, $to, $subject, $content);
        $email->send();
    }

}

We can improve on this by adding fields to our page to control which pages are to be tracked and who to send the emails to. This will add a SendTrackingEmail check box to each page's settings tab. It will also add a to and from email address text field to the content tab when tracking is enabled.

class Page extends SiteTree {

    private static $db = array(
        'TrackingEmailTo' => 'Varchar(255)',
        'TrackingEmailFrom' => 'Varchar(255)',
        'SendTrackingEmail' => 'Boolean'
    );

    public function getSettingsFields() {
        $fields = parent::getSettingsFields();

        $fields->addFieldToTab('Root', CheckboxField::create('SendTrackingEmail', 'Send tracking email?'), 'ShowInMenus');

        return $fields;
    }

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        if ($this->SendTrackingEmail) {
            $fields->addFieldToTab('Root.Tracking', TextField::create('TrackingEmailTo', 'Send tracking email to'));
            $fields->addFieldToTab('Root.Tracking', TextField::create('TrackingEmailFrom', 'Send tracking email from'));
        }

        return $fields;
    }

}

We then tweak the emailing code to check if tracking is enabled and to use the email addresses set.

class Page_Controller extends ContentController {

    public function init() {
        parent::init();

        if ($this->SendTrackingEmail) {

            $config = SiteConfig::current_site_config();

            $ipAddress = '';
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                $ipAddress = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } else {
                $ipAddress = $_SERVER['REMOTE_ADDR'];
            }

            $subject = $config->Title . ' - ' . $this->Title . ' - page visit';
            $content = '<p>Page visited: ' . $this->Title . '</p>';
            $content .= '<p>IP Address: ' . $ipAddress . '</p>';

            $email = Email::create($this->TrackingEmailFrom, $this->TrackingEmailTo, $subject, $content);
            $email->send();
        }
    }

}

This is also using a method of getting the user's ip address as suggested in the following question: How to get the client IP address in PHP?

Another improvement we could make is storing the tracking to and from email addresses in the site config settings, so they are stored in just one place and used through out the site.

We could also save this data to the database rather than send it in an email.

Community
  • 1
  • 1
3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • Thank you for your help - I will try to use the code today and will post the result here. To explain what I need it for it's for certain page as I would like to have a kind of web presentation platform to show the customer how the website will look like (design). So it won't be viewed by the thousands it's just to know if the customer has seen the design. (and how many viewed the project) – Maciej Swierczewski Oct 23 '15 at 08:15
  • 3dgoo your solution's working out of the box - that's what I was looking for. Thank you very much. – Maciej Swierczewski Oct 23 '15 at 08:30