Create new tab with specified url and new tab with sound when new email

What you just wrote in the code is exactly what I wrote in my last extract of my code:

[php]<body <?php if ($ok=="1") echo "ONLOAD='open2pages();'";?>>

[/php]

However, the script will not work, if there is a new email. The $link is filled correctly from the content of the new email, I have checked for this. But somehow the url is not relayed to the script.

Hope we can solve this!

Well, again, when I test it, it works just fine. Two tabs are opened. One for a URL and one for the previously
working beep page… Here is the test code that I used…
[php]

<?PHP $ok = 1; $link = "http://www.google.com/"; ?> > There was an email, so $ok = '1'... Two pages will be opened...
<script>
  function open2pages() {
     window.open('<?PHP echo $link; ?>');
     window.open('testbeep.htm');
   };
[/php] It should just open Google on one page and the beep page... If it is not opening your email page, I would have to guess it is badly formed. This simple function does work... That is why I mentioned many times to check your values inside of your variables... Hope this helps!

I explain in 3 simple steps:

I insert exactlythis code that you just gave me, and echo $link to make sure that the code for retrieving the url is not the problem (and remove the refresh function):

[php]

<?PHP

$imap = imap_open("{imap.one.com}","—", “—”);
if( $imap ) {

$MC = imap_check($imap);

// Fetch an overview for all messages in INBOX
$result = imap_fetch_overview($imap,“4200:{$MC->Nmsgs}”,0);
foreach ($result as $overview) {
$nr = “{$overview->msgno}”;
$seen = “{$overview->seen}”;
if($seen == “0”) {
$text = imap_fetchbody($imap,$nr,1);
$pos = strpos($text, “http”);$http = substr($text, $pos);$end = strpos($http, ‘=’);if ($end>0); else $end = strpos($http, " ");
if ($end>0) {
$link = substr($http, 0, $end);}
else
{
$link = substr($http, 0);
}
$ok=‘1’;
echo "URL retrieved: ".$link;

}

}
}
?>

> There was an email, so $ok = '1'... Two pages will be opened...
 <script>
   function open2pages() {
      window.open('<?PHP echo $link; ?>');
      window.open('alarm.php');
    };
</script>   
[/php]

I mark an email as unread, refresh the page and nothing happens, except the correct $link is echoed.

I replace [php]window.open(’<?PHP echo $link; ?>’);[/php] with [php]window.open(‘alarm.php’);[/php] (to localize the error), mark an email as unread, refresh the page and two tabs with sounds are appearing - as well as the correct $link echoed.

My apologies… It was actually a typing error in the url retriever code. The script is obviously sensitive to spaces in the end of the text and this was why it was not working!

:smiley: :smiley: :smiley:

Thanks a LOT! This was some puzzle. But you have been very patient and professional and helpful for me!

Glad to hear it is working for you now…

Yes, it was a puzzle for sure! Glad I could help… We will be waiting to hear your next puzzle… LOL

Well actually, now that you mention it, you already kind of brought it to the table: How to create a better email checking mechanism? You mention AJAX.

Because I actually have this problem that the site stops refreshing after a while. I do not know why. It refreshes every 3rd second, so it should not be a massive encrimination.

Thanks in advance!

Well, there are three ways to refresh a page. You can use “meta” code inside the HTML or you can use a
JQuery bound to a timer. sometimes the META-code in HTML seems to time out a bit. I had that problem
once before… Third way would be AJAX. Anyways, here is that code. As you see it is only one line and
that is why a lot of people use it. (Set for five seconds…)
[php]

[/php]

Here is a version in JQuery/JS …
[php]

[/php]
I think 300000 milliseconds is 5 minutes. Also, this routine has a timer which is reset if you type or if you
move the mouse. It is set up to refresh after five minutes if nothing is happening on the page. You may
or may not want that option in place. If you use that you must link your page to JQuery…
Now, if want to use AJAX there are lots of things to learn. First, the use of AJAX basically means that you
would call out to another page automatically and load a PHP page. Since this PHP code is opened every so
many seconds and basically reloaded, you can add your email checking routines in that PHP file. Here is a
sample that I got from Stackoverflow that shows how to do the refresh and reload a page. You will be
loading your page into a

and whatever output from the PHP file is displayed there. Hope it helps…
[php] Ajax Page
contents will be changed automatically.
[/php] Note that the AJAX call is basically the similar to "POSTING" a form. It loads an entire PHP page into the insides of a
. In this case "wrapper". Whatever your PHP file does, the outputs of it will show up in the "wrapper"
. Hope you get that part...

So, more to think about… Good luck and let us know how it goes…

I tried to insert the script into the body of the page and set it down to 1000 milliseconds equals 1 second:

[php] <body <?php if ($ok=="1") echo "ONLOAD='open2pages();'";?>>

       <script>
   var time = new Date().getTime();
   $(document.body).bind("mousemove keypress", function(e) {
       time = new Date().getTime();
   });

   function refresh() {
       if(new Date().getTime() - time >= 1000) 
          window.location.reload(true);
      else 
          setTimeout(refresh, 10000);
  }

  setTimeout(refresh, 10000);
 <script>
   function open2pages() {
      window.open('-/alarm.php', '_blank');
      window.open('<?PHP echo $link; ?>');
    };
</script>   
[/php]

However, nothing happens, when I mark an email as unread… Hope I didn’t do anything wrong here.

Well, that version is JQuery, not Javascript. So, I think you need to add in the link to JQuery which I normally
add in a “lib” folder. For some reason, it works just fine on two of my servers that I use, but, on another one,
I have to add in the link to it… You can download the minimum JQuery library here:
https://jquery.com/download/

OR, just use the JQuery link in the third example and pull it from Google.com… Either way…

Allright. Nice. This is new territory to me, and I have downloaded the jquery-2.1.4.min file now.

What now? What do you mean exactly by:

So, I think you need to add in the link to JQuery which I normally add in a "lib" folder. For some reason, it works just fine on two of my servers that I use, but, on another one, I have to add in the link to it...
?

Sorry for being a bit a donky! :wink:

No problem… This is just a basic step…

So, there are two ways of doing this. Google.com stores it on their server for you to add if you want to do
it that way. I showed this earlier. It is done with this command:

OR, since you downloaded the library, you would have to upload the library to your server. I usually place
them in a folder called “lib” or “libraries” or something similar. You could load it in the same folder as your
other code if you want to. You really only need the “jquery.min.js” file. It can be stored in the same folder
as the other pages. In this case, you would add it into your page that has JQuery code with this line:

This is usually placed inside your tags…
Basically, you just change the “src” to point at where you saved the jquery.min.js file…

Hope that makes sense… Let us know if it does not…

Brilliant - thanks - this works perfect!

You mentioned something about the alarm tab could close down automatically after the sound is finished, if this is inserted (_blank):

[php] function open2pages() {
window.open(’-/alarm.php’, ‘_blank’);[/php]

I did this. But the tab remains after the sound is finished?

Well, you would have to set up a timer in JS and make it run upon ONLOAD in the beep page. (alarm.php)
Something like this should work… (Not tested)

setTimeout(“window.close();”,10000);

You would have to add the ONLOAD to the body tag in the BEEP page. Then, have that call a function that
would do the above. This would close the current page after 10 seconds. You can make it however long
you would want it…

Hope that makes sense…

Hi Everyone

I know I am a bit late to this thread but maybe looking into a PHP daemon will help? The page refresh is a great idea, but I would not rely on Javascript for accurate timing and the refresh requests may not make it to the server. You can still have the page refresh to look into the database or log file to see when it must play your audio. The daemon can handle flagging of the emails etc while keeping a constant connection.

Here is a good article about daemons.

I needed a system to reply to emails or requests every 5 seconds and Linux chron job minimum runtime was 1 minute! The only way I could do it was to write a daemon script.

Some observations:

[ul][li]There is no error tolerance on a daemon script, errors seem to make the script die. So error capturing is vital. You may not use STDOUT, which is basically what an echo does, so any error that goes to STDOUT is a killer[/li]
[li]I have only tested this on Linux, which is my prefered place to host PHP solutions and it works really well, there is no reason it won’t run on windows but running the daemon may be different[/li]
[li]The daemon makes use of the php-cli or php.exe file found in the system path / terminal which is not necessarily the one which could be operating on your webserver, the ini files may differ![/li]
[li]If you change code on the script, you need to kill the running processes for the code to take effect[/li]
[li]Make sure you clean up memory, this is a process which could run forever, ie set variables to null, close database connections is a definite must![/li][/ul]

My apologies if this is a bit off topic or if you have solved the problem already.

Well, Andrev, yes and no.

Headfallingoff has solved his code. He was just looking for one more item to make it perfect for his uses.

Your daemon idea is sound, but, you would have to run server tests to see if it works better than just doing
a refresh of the page. Servers are designed to send out pages quickly, but, a daemon might speed it up.
A simple four or five lines in his web page solves what he needs and takes nearly no extra resources. It
might work better with one or the other, but, timing tests would have to be done to know which is best.

I also would never suggest refreshing a page at a 1 second time frame. Especially, if you are just checking
email servers. There is no logical reason to check emails every second. It just make the server refresh too
often, in my opinion. And, it might place too much demand on the server. If you really need to check that
often, just rewrite your email server to send out a notification each time it receives a new email. But, that,
of course, is another thread topic… Thanks for your input… Maybe he will try it and let us know which is
the best for this project…

Maybe he will try it and let us know which is the best for this project...

I am happy to help with the daemon solution if that is the route that is taken.

Yes, it sounds not logical this need to check for new emails every second, but if you buy and sell online, it actually makes a difference. Just ask the Wall Street sharks!

And I am happy to announce that I am making great use of the code, you have given me this weekend, ErnieAlex. However, there has been some stability issues and the refreshing stops in periods. Not a big thing, but a loss of some seconds has occured.

Timing tests could resolve what is the fastest of the 3 solutions?

  1. The current solution refreshing the page
  2. I know I am a bit late to this thread but maybe looking into a PHP daemon will help?
  3. rewrite your email server to send out a notification each time it receives a new email.

I know that my email server from one.com is fast, because I used Thunderbird to give me notifications before…


I inserted this code in the alarm.php, but it does not close after 3 seconds:

[php]

Your browser does not support the audio element.

[/php]

I am sure there is a simple solution to this.

Hi Headfallingoff

The window will only be able to close if you opened it from some Javascript within your page, also you need the close functionality to be within that same page. The link below references chrome but this should work across all browsers.

So the script to close the page should probably be in the page that opened the page in the first place.

Well, Andrev is correct… I ran a hundred different tests. It seems that Javascript can not actually close
itself. There is a way to do it in JQuery, but, it seems more complicated than JS… BUT, JS can close ANY
window that IT opens.

While testing various routines for you to try, I found this one and it seems to work nicely. You will have to
change your code where you open the two pages to also close the beep page. So, where you call the
function open2pages, you can use this code to close the second beep page…

I used this example that opens a pop-up page with sounds playing in it. Then, it closes after 8 seconds.
[php]

Test JS Beep! BEEP test... Opening another window... [/php] This is just so you can test opening a second window and then closing it. What it does is create a new window and keeps a point to it. Then, after 8 seconds, it closes that window. I have tested it and it does work. One problem I had is that it was set to 3 seconds and the sound would not play. I think I tracked it down to the loading time for the sound wave file. You have to use SMALL files. You can loop that file so it will be longer, but, you can work out the timing of it...

Hope that helps finish it all up… Let us knw!

The sound file is 1.07 MB and lasts 12 secs… Too much?

The window will open, but not close after 15 secs (in Firefox):

[php] <body <?php if ($ok=="1") echo "ONLOAD='openWindow();open2pages();'";?>>
Checking for new emails…

 <script>
   function open2pages() {
      window.open('<?PHP echo $link; ?>');
    };
</script>   
[/php]

Also, I need the alarm tab/window to be in the background - or - I need to point/focus not to the alarm tab but to the url tab. Not to keep a point to the alarm tab.

Thanks very much for both of your help!

Sponsor our Newsletter | Privacy Policy | Terms of Service