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

Hello guys,

I am using a php code to check for unseen mails every second or so (using a refresh function), and when I do receive a new email, the content of this email I can also retrieve and pass on a url from this content to a php variable ($url).

Now, I want this system to automatically open a new tab in the browser, when the new email is opened and the $url retrieved. Not only that. I also need the url of the new tab to correspond to $url.

Finally, I also need a second tab to be opened automatically. On this tab I will refer to an existing url (sound.php) from my own website. On this page I need to play a sound on page load (alarm.wav). In Firefox nonetheless.

This is meant to be an alert system to minimize the amount of time used to access the emailed url. I have tried with different solutions from stackoverflow.com, but failed to pass on the php variable to the java script opening the tab, and also, did not succeed with the sound playing in Firefox.

Thank you in advance!

Well, headfallingoff,

First, we can give you ideas on how to handle these items, but, usually we assist here in fixing coding
errors that members can not solve. We usually need to see some code and then we can help you fix the
problem errors. But, let’s discuss some of your items without code at this point…

You are using PHP code to access your mail system. To be able to use PHP for this application, you would
need to be able to tell the emails apart somehow. Most mail systems use some sort of ID that you can use
in your coding. That email would have to be used to pass from browser page to browser page. You can
just add that ID to the page you are opening and use the $_GET array to pull the data into PHP and then
retrieve the correct email.

Now, opening new pages in a tab is an option set inside the browser itself. There is an option to open it
in a new window or in a new tab. So, this might work differently inside another computer’s browser. The
Firefox vs. IE vs. Chrome should not really matter. You are just opening a new tab with an URL that has a
ID in it for an email. Same code for Firefox or IE… You just set the link to “target blank” which forces the
url to open in a new tab BUT, you can not open two URL’s using one link unless you use something like JS.
Here is a simple way to open two different pages at the same time. It basically just makes the link call a
small JQuery routine that opens two pages. Should work…
HTML:
[php]
Click Here To Open Two Pages
[/php]
Javascript/JQuery
[php]
$(‘a.open2pages’).click(function(e) {
e.preventDefault();
window.open(‘http://mysite.com/readmail.php’);
window.open(‘http://mysite.com/ringbell.php’);
});
[/php]
Although, why don’t you ring the bell alert when you load the email into a new tab?

Passing PHP data into JS code is easy. But, you must understand how they both work. First, PHP is on
the server. It is SERVER-SIDE only code. You will never be able to read PHP code by looking at what is on
a page in your browser. PHP does not exist there. All PHP code is processed on the server and it’s outputs
along with the HTML code are combined then sent to the browser. The JS code along with JQuery is inside
the browser and runs CLIENT-SIDE only. The browser takes the server’s combined output and adds in the
CSS styling and then runs any Javascript. So, to put data from PHP into a Javascript, you just remove the
old data and have PHP place the data where it is needed. Let’s take my example above for displaying a URL
in a new window. ( " window.open(‘http://mysite.com/readmail.php’); " ) If you wanted to use this to
display a email you can add to the URL an ID number for the email to be displayed. Just change it to show
the ID. Something like: window.open(‘http://mysite.com/readmail.php?email_id=3’); for the ID of 3 !
In the readmail.php file, you would grab this ID number like: $email_id=$_GET[“email_id”];

Now, that would allow you to pass the email ID as needed to the new page. You just need to figure out
how to create the URL inside the Javascript routine. To do this, you would need to simply replace the ID as
it is now hard-coded as #3 to a variable which of course you would load from your database with the right
ID for the link you are creating… Something like this might work:
[php]

<?PHP // Do something here to get the ID you want inside the link, right now I will just hard-code it to 13... $email_id = 13; ?>

$(‘a.open2pages’).click(function(e) {
e.preventDefault();
window.open(‘http://mysite.com/readmail.php?email_id=<?PHP echo $email_id; ?>’);
window.open(‘http://mysite.com/ringbell.php’);
});
[/php]
As you can see, it uses the PHP value of the email_id that you got from the database (in this example 13)
and then places it into the Javascript code. Remember, the PHP does not exist after it gets to the browser,
so the results of that link would be
window.open(‘http://mysite.com/readmail.php?email_id=13’);
Which is what you want. Then, when the user clicks on that link, the PHP code inside the second window
would read the ID number and open that email for displaying.

As far as the sound goes, you can just have the email display page play the sound upon loading using a
JS script. Or, you can use the second URL version as you indicated and have that second page play the
sound and then close the page so it goes away… Depends on the numbers of emails you get each hour.

Well, not really sure how helpful that is, but, I think it should help you get started. Once you get it coded
up, if you have problems show us the code areas that you are having issues with and we can help…

Hello, ErnieAlex - and thanks very much for your elaborate answer.

I think there might be some misunderstanding. I need the tab to open automatically. Not via a link. In order to minimize the time.

Also, the specified url is in the email text I receive, so I retrieve it using this code:

In the HEAD:

<?php $imap = imap_open("{imap.one.com}","MYEMAIL", "PASSWORD"); if( $imap ) { $MC = imap_check($imap); // Fetch an overview for all messages in INBOX $result = imap_fetch_overview($imap,"2600:{$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); } echo ''; echo ''; } } } ?>

You ask me: “Although, why don’t you ring the bell alert when you load the email into a new tab?” Because I need to be able to turn off the alarm which I do by closing the tab. But I guess you ask the question thinking I am opening only urls from my own website and not an external one.

I think it is possible to integrate your solution with these thoughts?

Hope to hear from you again!

Thanks!
headfallingoff

Well, it is not very easy to open two links in PHP. Perhaps, you could create a call-back system and
not wait for the first call back before opening a second window, but, PHP is not designed to do this.
That is why you need to use Javascript which is run CLIENT-SIDE. In the "client’ or in other words the
browser, you can open as many windows as you wish. You can use the JS function “ONLOAD” to run a
command when a window opens. In there, you can open a second window. That should work.

BUT, I gave you the code to open two windows at one time. But, you could change it to open one window
and in that code have the JS-ONLOAD open the second window. But, you said you wanted a URL link
to open both of them. I showed you how to do that. In your code that you just posted, you show two
links, which do not work as you did not place the JS into an ONCLICK function. So, clicking on these do
not work. You need to use a standard URL link which is an anchor as I showed you. You would need to
change it to something like this:

NOT
[php]
echo ‘’;
echo ‘’;
[/php]
Change to:
[php]
echo ‘Click Here To Open Two Pages’;
[/php]
As you see, you did not include the ANCHOR tag and therefore your two pages would open, but, not create
any link for the user to see… Also, note the embedded double-quotes and outside single-quotes.

Next, you need to add the JQuery code in with your two URL’s inserted by the PHP code. Something like
this might work:
[php]
$(‘a.open2pages’).click(function(e) {
e.preventDefault();
window.open(’<?PHP echo $link; ?>’);
window.open(‘MYWEBSITE/alarm.php’);
});
[/php]
So, what that does is use my code which creates a generic link. (You would change it to something that
would make sense for the user.) When the link is clicked on, it runs the JS/JQuery code that opens your
two pages. The two pages are listed, one just the alarm page, the other is formed using your $link info.

Hope that helps…

Hello again,

Thank you for your answer. I feel that we are getting closer to a solution.

However, you write: “When the link is clicked on, it runs the JS/JQuery code[…]” But I need the tabs to open automatically after the $link is retrieved from the content of the new email. The point is that the new tab with the url from $link is already open in the browser once I hear the alarm sound and sit by the computer. So I save time.

Also, it was not clear from your answer how exactly I play a sound using Firefox. Maybe a plugin is missing, but I did not find it.

This will be an awesome solution, I am sure, but not an easy one to produce.

Look forward to hear from you again!

And again: Thanks in advance.

Yours,
headfallingoff

Okay, well, again, you can NOT open a window using PHP. But, at the end of that script,
you can redirect to a page. You can also redirect to the same page. Then, you can use a hidden
variable to tell it to open another page. You would do that inside a JS script that would check for
the hidden variable and then open the two pages. In your first post you mentioned URL LINKS, so
I assumed you meant you wanted to show a list of links that would open when you click on them.

So, you have PHP code that locates a new email. You create a link to it. You would just need to store
that link inside a hidden field; <input type=“hidden” name=“new_email” value="<?PHP echo $link; ?> />
and that would store the link inside a hidden form field. Then, you can pull it out in the JS code and see
if it has content or not. If it does, then, process it by opening the second window and playing a beep.
If no content inside of the field, then just continue on.

If you want to force an new window to open, you have to either post the current page or redirect to
itself. Once you do that, you can use the JS ONLOAD command to check a variable and open pages if
there are any to open.

Also, to play a “beep”, it is easy in JS. You can use a line something like this: ( Found online… )
[php]


[/php]
I did a bit of reading and it seems it works better if you do it this way by “EMBEDDING” the sound file
inside the HTML page. Otherwise, there are odd delays while it reads the file and starts to play it.

You could add this inside the ONLOAD code so that it would play the sound if there are new emails…

Gotta run out for many hours… I will check in later and see if you got it working…

It looks great. I will implement it tomorrow (time zone: Copenhagen) and feed back to you!

Hope you had a great time being out.

And sorry for the miscommunication by me about the links.

  • headfallingoff

No problem. Thanks for the Karma…

One further thing, when you use the PHP code to save a value in a hidden field, you have to make sure it is
loaded into the HTML area of the page. Otherwise, the JS will not be able to find it. Here is a site that
explains how to read the hidden field into JS… Might help further…

http://www.mkyong.com/javascript/how-to-get-hidden-field-value-in-javascript/

I suppose I have to create a new .php-page that would then redirect to the desired url, since PHP cannot open new pages. We can call it: tab.php. Okay, you say I need the JS code on this page, but the code you gave me was with the click function - or am I wrong? So which code exactly do you mean that will open the two tabs? (on page load)

I tried to input the new code from you, but there is no sound? Also, I am afraid that the -tag will not work with the setup I have done, but I can’t insert <?PHP echo $link; ?> into an php echo sentence…

Hope we can work this out! Thanks!

[…]

<?php $imap = imap_open("{imap.one.com}","MYEMAIL", "PASSWORD"); if( $imap ) { $MC = imap_check($imap); // Fetch an overview for all messages in INBOX $result = imap_fetch_overview($imap,"3800:{$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); } echo ''; echo ''; } } } ?>

What setup do you have where an input will not work?

Check this again, src="/alarm.wav" it does not look correct.

Well, Astonecipher might be correct. if you add a “/” to the beginning of your source ( src= ) clause, that
means that the wav file must be in the root folder of your server. If the wav file is in the same folder as
your page that calls it, then no slash is needed. If you have the sound in a special folder like “sounds”,
then you have to point at that folder. That might help clear that part up.

Now, Please do us all a favor and always post your code inside the PHP tags. Press the PHP button above
the text box when inserting code and place it inside or between the tags. This make is easier to read and
much easier to copy when we grab your code to test on our systems…

Astonechipher’s first comment is mine also. How is this system planned to work? What setup is it? What
we are asking is , and I am just assuming this part, you must have one page you are running and have it
set up to refresh automatically every minute. In this page, you check your emails and if there is a new one,
it is wanted to open a new tab with the email in it… If this is your logic, then I see that you should have
only two pages for this system. The original one that keeps refreshing itself, checking for emails. In that
page, you would have the JS script that would open the second page. Since PHP can not make the JS run
on it’s own as they are in different sides of the system (client vs server), the JS would have to run upon
loading the new page. Which means that the page is refreshed, then, the JS handles opening other pages.
The second page would just need to know the email link so it can open it for you to review and of course
play the beep to make you look at the screen.

Is that basically your “setup”??? Also, for testing the beep page, just make a test page with just the JS
to call the beep and view it. If it works, you can refresh the page to make it play the beep a second time.

One problem I foresee with this system is how do you handle multiple emails that come in at the same time?
Your PHP code that looks at the emails to see if any new ones have appeared would have to make a list of
them if many came in at the same time. (Assuming you are searching them on time they were received.)
You could code the JS to open multiple pages which should work. Just have the PHP side alter the JS to
include more than one open-tab…

Well, more info for you to think on…

One further note. I reviewed the newer HTML settings for background sounds and found there is a very
simple way to play an audio file when a page loads. Here is a sample page. Just save it to your server and
make sure your wave file is in that folder, too. I went to the folder on my computer “C:\windows\media” and
took alarm6.wav or whatever. I named it alarm.wav and stored it with the page below. When viewed, it does
show the text and play the sound. I set it to repeat 10 times to get my attention. Worked very very well!

So, in your PHP page that keeps refreshing looking for new emails, you would have to alter the PHP to add the
background sound line if a new email exists. So, it would just have to echo the one line inside the body of the
displayed page. Did that make sense? Hope so. Here is a sample that plays a sound…
[php]

Test HTML Beep! BEEP! [/php] Simple ! You would just need to echo line #8 if you have a new email.

Thanks to both of you. I will look into this tomorrow or Saturday at the latest. I am sure it will work fine this time.

Also, you are right about simultaneous emails, even though it checks for new emails every second actually. But, emails can still come within the same second.

[php]I will report back. Thanks![/php]

  • headfallingoff

Well, as far as the simultaneous emails, usually, you can access your date and time that an email was sent.
You should be able to use them to keep a flag as the last date/time you read emails. Just take that first one
and deal with it and update the date/time to that one email. Then, when it rechecks for more, compare the
new email’s date/time and make sure it is newer than the saved ones. Should work, just a bit of logic code
to sort out depending on your email system you are using.

You might not want it to be every second. This is overkill for a server, especially if you have other things going
on there. Most automatically updated email systems are set to 1 to 10 minutes. One second could cause a
lot of timing issues. It really depends on how often your mail system updates itself. If it updates once per
minute, then once per second is useless… Just a thought!

Let us know how it goes once you get to it…

I am a bit confused… Did you answer my question: “We can call it: tab.php. Okay, you say I need the JS code on this page, but the code you gave me was with the click function - or am I wrong? So which code exactly do you mean that will open the two tabs? (on page load)” Because I did not find it.

You gave me:

[php] $(‘a.open2pages’).click(function(e) {
e.preventDefault();
window.open(’<?PHP echo $link; ?>’);
window.open(‘MYWEBSITE/alarm.php’);
});[/php]

Which I insert in the if-clause:

[php] 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);
}

$(‘a.open2pages’).click(function(e) {
e.preventDefault();
window.open(’<?PHP echo $link; ?>’);
window.open(‘MYWEBSITE/alarm.php’);
});

    }[/php]

Correct?

Also, I tested the sound, but it did not work with the BGSOUND-tag (without using your script). I am using Firefox, and I think this could cause the problem.

Yes, and I agree with the timing issues when refreshing every second. I am “competing” with Thunderbird, which is updating for new emails roughly every second, but still this is faster, when I tested it opening tabs with a predefined url :smiley:

Looking forward to hear from you!

  • headfallingoff

Well, yes and no. So, that JS code will open two pages or tabs for you.

Then, you have to “CALL” that routine for it to work. You would have to use the ONLOAD function to call
that routine to make it work. So, if there is no new email, make the PHP code to just do nothing. If there
is a new email, make the PHP code to echo the ONLOAD line inside the tag as I explained before.
So, instead of using just for the start of your HTML body, you would add in the call to make the
“click” of that routine. Something like or you can remove it from
the click function and just call it something like But, you really do not
need to use two pages. Just place the sound code in the HTML using your PHP code optionally depending
on if a new email is present or now. Then, refresh the page and it will play the sound.

This brings us back to Astonecipher’s comment. What layout are you using in your system? Is this just one
page that keeps refreshing itself? Or do you REDIRECT it to itself using a “header()” function or do you do
a “post” to itself? Each one of these versions would need a different way of passing on the data to the
next page when it is loaded. Most programmers, I would guess, would just use JS and AJAX and load a
second PHP page that would load any new emails into the current page and then parse thru them… Lots
of ways to layout this type of code…

I just did some reading for you and the BGSOUND is NOT supported under Firefox. Crazy! And, of course,
my testing was done under IE. But, I found a HTML5 version that works on my Firefox. Here it is
[php]

Test Beep!

Your browser does not support the audio element.

BEEP! [/php] The line inside the tags is to tell people who do not have HTML5 that they can not here the sounds... Not needed if it is just for you... You can place an MP3 there and you can loop it if needed and you can also play a video that way, too if needed...

Well, hope all that helps…

HTML5 sits on the browser, so having it is up to you, not the users browser, although depending on the browser all tags may not work.

You haven’t really gone into the code for how you know when an email has been received, but the client side would best be handled with an Ajax call, long poll or interval. When the call returns that a new message had been received is when the new window/ tab should open.

The new message window, should contain the sound and should be a template that just adds the new message to the content of the body.

Yes, Astonechipher, I have told him that several different ways. I only suggested AJAX as that is the way
I would do it. But, the little bit of code he has shown us does not help at all to know how he is getting the
new emails. As with most projects, you have to have the basic overview of how it all works before you can
truly help someone with it…

Good news! The sound is now playing in Firefox. I will not play it on the page checking for new emails, because this page is refreshed every second: [php][/php]

Now, this code:
[php] $(‘a.open2pages’).click(function(e) {
e.preventDefault();
window.open(’<?PHP echo $link; ?>’);
window.open(‘MYWEBSITE/alarm.php’);
});[/php]
Where should I place it? I understand the code to insert in the body tag, but what about this code? I get an error, if I place it outside the php tags.

Is this enough? So how do you activate this script, if there is a new email?

Also, this “click” word is confusing me. It is not a real click function or what? Because I need minimal response time, so that the tab is opened automatically once the new email arrives.

Thank you for your answers!

Sponsor our Newsletter | Privacy Policy | Terms of Service