Originally posted to the Sidebar by "hulver"...

At my new job, they had been having some problems with email events on a "special" PICK-based web application. When something happened in the system that needed an action, an email would be sent to the relevant person telling them to do something. Sometimes these emails would not be sent, but sometimes the emails would be sent many times. The emails that were sent many times often got sent at strange times of night when nobody was using the system. It was a bit of a mystery.

The problem of emails not being sent had been narrowed down to the customers running popup blockers. I was a bit mystified at this. Why would the client running a popup blocker prevent the web server sending an email?

The application sent emails in a somewhat roundabout fashion. Rather than calling its built in email function, the PICK code would write a JavaScript function into the results page that looked a bit like

<script>AutoEmailer('/emailpath/145ADJI5L.ASP');</script>

I found this function in one of the many external JS files used by the application.

function AutoEmailer(Url){
 if(Url != ""){
   var window1 = window.open(Url,"","height=275,width=570, left=20, top=20," +
                                 "resizable=no, maximise=no, minimise=no,scrollbars=YES");
   window1.opener = window ;
 }
}

After a bit more investigation I found another function call in the PICK code that loaded an ASP template from a database and did a replace on some text. This filled in things like email addresses, subjects, body text. It then wrote this file into another directory on the webserver with a random filename.

It was the ASP code that actually sent the email. If clients had a popup blocker running, the ASP page would never run and the email would not get sent. Not quite confident enough in my first month to refactor the code to call the internal email routine, I leveraged my knowledge of AJAX to solve the problem.

function AutoEmailer(Url){
 if(Url != ""){
   var window1 = window.open(Url,"","height=275,width=570, left=20, top=20," +
                                 "resizable=no, maximise=no, minimise=no,scrollbars=YES");
   if (window1 == null)
   {
     // We've hit a popup blocker
     var Req = GetRequestObject();
     if (Req)
     {
       var timeoutcookie;
       Req.open("GET", Url,true);
       timeoutcookie = setTimeout( function() {
         Req.abort();
         alert('An error occured while sending your email. Your email could not be sent');
       }, 4000);
       Req.onreadystatechange=function() {
         if (Req.readyState==4) {
           clearTimeout(timeoutcookie);
           if (Req.status==200)
           {
             alert('Your Email has been sent.');
           } else {
             alert('An error occured while sending your email. Your email could not be sent.')
           }
         }
       }
       Req.send(null)
     } else {
       alert ('Could not send email due to popup windows being blocked');
     }
   } else {
     window1.opener = window ;
   }
 }
}

Now there was just the problem of the extra emails being sent at odd times.

It turns out that on some site directory indexing had been left on, and a search engine was coming along and indexing the contents of the directory. Of course, that ran the ASP code which sent the emails again. A simple bit of ASP at the end of the file which caused the file to delete itself when it was run, and that problem was solved as well!

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!