Popup blocker email wtf.



  • I was settling into my new job. The interview process had been a bit WTFy, but I was happy to be away from the old place with their horrible management processes and working for a smaller company.

    I knew the code was a bit old, and the platform was a bit... odd. It was a web app, written in a PICK language, with lots of bits of JavaScript to make it all hang together.

    I was being shown around the system by one of the old hands. She'd been at the company many years, and had seen the development team of 4 turn over completely nearly 7 times in 4 years, so she was used to introducing new people to the code.

    They'd been having some problems with email events. 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!



  •  You know how some people will discipline a dog by smacking it on the face with a rolled up newspaper while yelling "No!"? I get the tendency to do that exact same thing to programmers that do stuff like this. 



  • Heh. My "solution" to the problem was almost as WTFy as the original code. Thankfully I've been here a while now, and have removed that particular bit of horribleness.

    There are plenty more though. This code full of them.




Log in to reply