Forum Moderators: coopster
<?php
header ("location: mailto:me@example.net?subject=Email test");
header ("Refresh: 2; URL=http://example.net/index.html");
?>
In Firefox: Email client is activated and browser redirects to page.
In Opera: Email client is activated and browser stays on current page.
In IE: Email client is activated and browser has blank page. Actually it has the php page but of course there is nothing to see.
What do I need to do to get a consistent response?
Is there a way to send both these headers and have them responded to?
[edited by: jatar_k at 6:37 pm (utc) on April 20, 2005]
[edit reason] generalized urls [/edit]
For example, if I don't have any client set up as my default email client, the browser won't have any idea of what to do regardless of what headers you send.
Similarly, in Firefox, if I have the tabbed browsing extension on, I as the user determine the response to links. I may say that all links should open in new tabs or new windows, or that they should open in the same tab, or even that internal links open in the same tab and external links in a new tab.
Is it really likely that a user would not have a default email client. What would normally happen when they click an email link on a site?
Regarding your comment about external links and tabbed browsing. They wouldn't actually be external links. It just looks that way from the test code.
What would happen is there would be a link on a page such as
<a "href=mailout.html?id=JohnDoe">please contact John Doe</a>
The 'mailout' file would translate the id into a real email address and send the header that activates the email client.
The reason for the next header is to make sure the browser page is back where it started. So in fact, the Opera response is the ideal one as it stays put.
Firefox doesn't so the header brings it back.
And IE, well!?
Okay, I'm not proud of this and don't recommend it in any way, but what I have below does give the desired behavior (roughly) in IE and FF (didn't test any others).
mailout.html is unchanged except for testing that 'id' is getting passed (and being named mailout.php).
<?php
header ("location: mailto:me@example.net?subject=Email test - " . $_GET['id']);
header ("Refresh: 2; URL=http://tests/mail.html");
?>
mail.html looks like this (with doctype and title and such removed)
<head>
<script type="text/javascript">
function mailit(id)
{
win = window.open('mailout.php?id='+id);
setTimeout('closeit(win)', 2000);
}function closeit(win)
{
if (win && win.open &&!win.closed) win.close();
}
</script>
</head>
</body>
<a href="mailout.php?id=JohnDoe" onclick="mailit('JohnDoe'); return false;">please contact John Doe</a>
</body>
It's ugly and obtuse, but it does what you want it to in my limited testing with IE and FF. The setTimeout gives (hopefully) enough time (two seconds) to get the mail client open before the window closes.
Use at your own risk.