3

I have a Django admin action called "Email selected members". Check some members and click the Go button and the user's mail program is opened. The emails of the selected members have been pre-entered.

This works by a Django HttpResponseRedirect(uri) with the uri being "mailto:email1,email2.. where the addresses email1, email2 ... were looked up on the server.

The only problem is that that the browser re-directs to a blank page as well a opening the client mail program.

Is there any way to avoid this?

-- Peter

peter2108
  • 5,580
  • 6
  • 24
  • 18

3 Answers3

5

This question is a little old, but I just went through this and I think I can help anyone looking for an answer in the future.

I ran into this problem because the website I was building had a built-in tracking system that tracked the URLs of outbound links for self-hosted ads. If I don't redirect, there is no way (without changing the way it was implemented) to track the click, since I'm not using an API or anything.

The easy fix was to do what you did, sending back an HttpResponse() whose content is the meta tag

<meta http-equiv="refresh" content="0;url=mailto:youremail@test.com" />

This causes the page to refresh on load, which triggers the mailto: action.

Now we're left with an open window, but we can't close the window using Javascript's window.close() method. I do believe that this solution should work, however. Call that Javascript function after the refresh has been successful.

Unfortunately, I haven't tested this, but these two methods should accomplish a mailto: redirect that does not leave a blank window/tab behind.

Hope this helps!

Jamie Counsell
  • 7,730
  • 6
  • 46
  • 81
1

Don't use HttpResponseRedirect. Just make the mailto: line a link. <a href="mailto:email1...">Email selected members</a>

Matt Phillips
  • 11,249
  • 10
  • 46
  • 71
  • 2
    I guess it is not Peter's intention to have somebody click on a link! – Bernhard Vallant Oct 18 '10 at 18:19
  • well i should have known. It seems like its all the rage lately for one-off users to post answers and then not even comment or do anything with it. – Matt Phillips Oct 19 '10 at 00:29
  • Yes, controlfreak it may be, which is a shame that. But a chap has to sleep. As lazerscience says I don't want to write client side code into a custom Django admin form. – peter2108 Oct 19 '10 at 10:11
  • well I would say that if you are getting a blank page on a response redirect then that is pretty much a browser specific thing. I get blank pages every once in awhile when i click on attachments to download them in emails. Its probably a very similar thing. Even if you do a response redirect with mailto in it mailto is a client side action. Why do you think it opens up the default email client? – Matt Phillips Oct 19 '10 at 13:10
0

I don't think it is possible. RFC 2616 says re 302 redirect:

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s)

So the blank page that I see is the (very) short hypertext note. The browser gets the redirect instruction, pops up a temporary page with the redirect message, then retrieves the redirected URL. But with a mailto: URL the temporary page obviously remains.

peter2108
  • 5,580
  • 6
  • 24
  • 18