Zen Cart Logo
Forums / All Other Contributions/Addons / Generate an e-mail on page-not-found (404) errors [support thread]

Generate an e-mail on page-not-found (404) errors [support thread]

Views: 9,926

Results 1 to 20 of 37
9 Apr 2010, 2:25 PM
#1
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Generate an e-mail on page-not-found (404) errors [support thread]

I got tired of scouring my Apache logs for 404 errors, so wrote this little update that will send an e-mail to the address of your choice as part of the page_not_found processing.

Installation is short and simple (see the readme). You'll need to edit /includes/languages/english/YOURTEMPLATE/page_not_found.php to contain your desired e-mail address.

See download here: http://www.zen-cart.com/downloads.php?do=file&id=1090

9 Apr 2010, 3:17 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Generate an e-mail on page-not-found (404) errors [support thread]

I'm going to suggest a slightly different approach ...

What you've done requires editing 2 existing files.

The following uses the notifier/observer system to do the same thing as your original did, but doesn't touch any existing files.

(Attachment removed. See Addons area to download)*

9 Apr 2010, 3:31 PM
#3
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Cool, DrByte. Thanks for the update. I had a feeling that I could use a notifier to get around the core-file updates ...

9 Apr 2010, 3:53 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Generate an e-mail on page-not-found (404) errors [support thread]

If you like that approach, please make any applicable updates to the documentation, and submit it to the Addons section.

9 Apr 2010, 3:56 PM
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Will do ...

10 Apr 2010, 8:26 PM
#6
g1smd avatar

g1smd

Zen Follower

Join Date:
Mar 2010
Location:
UK
Posts:
449
Plugin Contributions:
0

Re: Generate an e-mail on page-not-found (404) errors [support thread]

If you have a Google WebmasterTools account, there's a neat little report in there listing all the URLs that Google finds as 'not found' and all the pages that have those links on.

Most of the time, the list will contain URLs for removed products, but it's worth a scan every now and again.

11 Apr 2010, 10:28 AM
#7
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Thanks for the info, g1smd. My focus for developing this little add-on was more to get a notification for accesses that are likely someone groping around my site, of the like

https://www.example.com/admin

... when I've followed the security guidelines and renamed that folder.

11 Apr 2010, 10:36 AM
#8
g1smd avatar

g1smd

Zen Follower

Join Date:
Mar 2010
Location:
UK
Posts:
449
Plugin Contributions:
0

Re: Generate an e-mail on page-not-found (404) errors [support thread]

OK. That works too. :smile:

Additionally, I like to add this in the .htaccess file in the /admin/ folder:

Order Deny,Allow
Deny from all
Allow from 11.22.33

which allows access from a small range of IP addresses.

13 Apr 2010, 6:32 PM
#9
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Oops! There was an error in the first release (the observer class was not being instantiated).

I've just uploaded v1.1 to correct the error in class.emailFor404.php from

 function emailFor404() {
    if (SEND_404_ERRORS_EMAIL_TO_STATUS == '1' and SEND_404_ERRORS_EMAILS_TO != '') {
      $this->attach($this, array('NOTIFY_HEADER_END_PAGE_NOT_FOUND'));
    }
  }

to

 function emailFor404() {
    global $zco_notifier;
    if (SEND_404_ERRORS_EMAIL_TO_STATUS == '1' and SEND_404_ERRORS_EMAILS_TO != '') {
      $zco_notifier->attach($this, array('NOTIFY_HEADER_END_PAGE_NOT_FOUND'));
    }
  }
13 Apr 2010, 6:38 PM
#10
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Generate an e-mail on page-not-found (404) errors [support thread]

The $this-> approach will work fine in 1.3.9 and newer.
For 1.3.8 and older you may have to use the old $zco_notifier-> approach instead.

13 May 2012, 7:57 PM
#11
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

I've updated this add-on to v1.2 (http://www.zen-cart.com/downloads.php?do=file&id=1090), including a switch to enable/disable the archiving of these emails.

14 May 2012, 9:46 AM
#12
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,874
Plugin Contributions:
7

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Hi,
Just tried out this latest version on ZC 1.5.

I receive the email but the text is not passed, its always:

A 404 Error Was Generated

$EMAIL_MESSAGE_HTML

Strangely the email is in html format irrespective of the format chosen in the admin.
I get the same results with an (almost) vanilla 1.5 installation.
It tried it local/Xampp and remote hosting, same result.

regards
Steve

14 May 2012, 1:03 PM
#13
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

I'm having a devil of a time replicating this. I've tried changing my CHARSET to utf-8 (which I'm assuming you're using), tried running as a logged-in or guest customer, changing my admin email format from HTML to Text and back. Regardless of these settings, I'm always receiving a Text-format message:

A user at host address [xxxx] with IP address [yyyy] generated a 404 using the query string [/index.php?main_page=elsewhere].

The settings in my Configuration->Email Settings are

E-Mail Transport Method 	PHP 	 
E-Mail Linefeeds 	LF
Use MIME HTML When Sending Emails 	true

What are your email settings?

14 May 2012, 1:48 PM
#14
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

torvista, would you try this updated version of /includes/classes/class.emailFor404.php and let me know if it works?

<?php
class emailFor404 extends base {

 /**  constructor method !
   *
   * Attach observer class to the global $zco_notifier and watch for a single notifier event.
   */
  function emailFor404() {
    global $zco_notifier;
    if (SEND_404_ERRORS_EMAIL_TO_STATUS == '1' and SEND_404_ERRORS_EMAILS_TO != '' and SEND_404_ERRORS_EMAILS_TO != '[email protected]') {
      $zco_notifier->attach($this, array('NOTIFY_HEADER_END_PAGE_NOT_FOUND'));
    }
  }


 /**   Actual Method that does the desired activity
   *
   * Called by observed class when any of the notifiable events occur
   *
   * @param object $class
   * @param string $eventID
   */
  function update(&$class, $eventID, $paramsArray = array()) {
    // BOF Send email to admin for 404 errors
    $email_text = sprintf(EMAIL_PAGE_NOT_FOUND_CONTENT, $_SESSION['customers_host_address'], $_SESSION['customers_ip_address'], $_ENV['REQUEST_URI'])."\n\n";
    $email_subject = EMAIL_PAGE_NOT_FOUND_SUBJECT;
    $module = (SEND_404_ERRORS_EMAILS_ARCHIVE != '0') ? 'default' : 'no_archive';
    $html_msg['EMAIL_SUBJECT'] = EMAIL_PAGE_NOT_FOUND_SUBJECT;
    $html_msg['EMAIL_MESSAGE_HTML'] = str_replace('\n','',$email_text);
    zen_mail('', SEND_404_ERRORS_EMAILS_TO, $email_subject, $email_text, STORE_NAME, EMAIL_FROM, $html_msg, $module);
    // EOF Send email to admin for 404 errors
  }
}
?>
14 May 2012, 8:26 PM
#15
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Got it! If the email address to which the 404 emails are to be sent was used as a test-customer account email and that customer account uses HTML email settings, the email described by torvista will be sent. I've submitted v1.2.1 to the plugins area to correct the problem and describe how the HTML/TEXT emails will be sent.

14 May 2012, 10:31 PM
#16
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,874
Plugin Contributions:
7

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Got it!
Well done! Excellent service!

15 May 2012, 12:30 PM
#17
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Thank you for your kind words! Here's the entire updated "update" function in the class module:

  function update(&$class, $eventID, $paramsArray = array()) {
    // BOF Send email to admin for 404 errors
    $email_text = sprintf(EMAIL_PAGE_NOT_FOUND_CONTENT, $_SESSION['customers_host_address'], $_SESSION['customers_ip_address'], $_ENV['REQUEST_URI'])."\n\n";
    $email_subject = EMAIL_PAGE_NOT_FOUND_SUBJECT;
    $module = (SEND_404_ERRORS_EMAIL_ARCHIVE != '0') ? 'default' : 'no_archive';
    $html_msg['EMAIL_SUBJECT'] = EMAIL_PAGE_NOT_FOUND_SUBJECT;
    $html_msg['EMAIL_MESSAGE_HTML'] .= str_replace('\n','',$email_text);
    zen_mail('', SEND_404_ERRORS_EMAILS_TO, $email_subject, $email_text, STORE_NAME, EMAIL_FROM, $html_msg, $module);
    // EOF Send email to admin for 404 errors
  }
17 May 2012, 4:51 PM
#18
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,874
Plugin Contributions:
7

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Thanks for that.

There is now content in the message body:
the host ip and user ip always show up but the query never.

I have tried different emails, text/html, local-remote hosting, switched off the CEON URI mapping..but the email never shows the page requested.

17 May 2012, 7:03 PM
#19
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Generate an e-mail on page-not-found (404) errors [support thread]

torvista, do you happen to have the SuperGlobals plugin installed? I'm wondering what the information in your store's $_ENV and $_SERVER are for the request-related information.

I'm successfully using the previous version of this plugin on a 1.3.9h store with Ceon URI mapping, so that's not it.

Does it make a difference if you change

    $email_text = sprintf(EMAIL_PAGE_NOT_FOUND_CONTENT, $_SESSION['customers_host_address'], $_SESSION['customers_ip_address'], $_ENV['REQUEST_URI'])."\n\n";

to

    $email_text = sprintf(EMAIL_PAGE_NOT_FOUND_CONTENT, $_SESSION['customers_host_address'], $_SESSION['customers_ip_address'], $_SERVER['REQUEST_URI'])."\n\n";
17 May 2012, 9:09 PM
#20
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,874
Plugin Contributions:
7

Re: Generate an e-mail on page-not-found (404) errors [support thread]

Yes I have Superglobals installed and I have yet to see _ENV even appear in the globals listing.
Using _SERVER as you suggested fixed the problem.

So for curiosity, what is and when should I see _ENV set?

Thanks
Steve