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.
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.
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.