mailto fails in IE where there is a long body text. Is there any way to resolve this?

后端 未结 5 1603
清酒与你
清酒与你 2020-12-03 03:26

I am having a problem using Internet Explorer 8 (IE8) to open mailto links with long messages.

After the user clicks on the link, IE changes to an about:blank page

相关标签:
5条回答
  • 2020-12-03 04:00

    it appears that the following hyperlink seems to work. It is 512 characters long and seems to work every time. You can increase the length of the To, but you must subtract either from subject or body. Same with subject.

    I wish this was documented somewhere; I am just guessing from experimenting with this.

    <a href="mailto:name@name.com?subject=123456789&amp;body=111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111234">
    

    I will gladly accept any answer from a person that can track down documentation that indicates that this is a known issue.

    0 讨论(0)
  • 2020-12-03 04:01

    I never could get the location.href = mailtoHref hack to work. However, I have found that following works.

    $('body').append($('<iframe id="mailtoHack" src="' + mailtoHref + '"/>');
    $('#mailtoHack').remove();
    

    EDIT

    Here is a way to do it without jQuery:

    function mailtoHack(href) {
        var iframeHack;
        if (href.indexOf("mailto:") === 0) {
            iframeHack = document.createElement("IFRAME");
            iframeHack.src = href;
            document.body.appendChild(iframeHack);
            document.body.removeChild(iframeHack);
        }
    }
    

    And, for good measure, here is a Knockout custom binding usable as data-bind="mailto: foo":

    ko.bindingHandlers.mailto = {
        init: function (element, valueAccessor) {
            ko.utils.registerEventHandler(element, "click", function (e) {
                var href = ko.unwrap(valueAccessor()), iframeHack;
                if (href.indexOf("mailto:") === 0) {
                    iframeHack = document.createElement("IFRAME");
                    document.body.appendChild(iframeHack);
                    document.body.removeChild(iframeHack);
                } else {
                    e.preventDefault();
                }
            });
        }
    };
    
    0 讨论(0)
  • 2020-12-03 04:13

    I also tried with the solution CBono posted but I coudn't make it work either. I found that when you directly paste the mailto URL in the browser it has no problem no matter how long the string is. So, I implemented something like this:

    <a href ="#" onclick="javascript:doMailto(); return false;">Send Email</a>
    
    <script type="text/javascript">
       var sMailto = "mailto:mail@something.com;mail@something.com;mail@something.com?subject=A really long subject can go here if you want and it will work Im telling you&body=Click for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profileClick for detailed profile&cc= &bcc= mail@something.com";
    
       function doMailto() {
          window.open(sMailto);
       }
    
    </script>
    

    As CBono mentioned, if you want this to be dynamic, you just need to pass the mailto string as parameter to the function and assing it to the sMailto variable.

    Hope this helps.

    0 讨论(0)
  • 2020-12-03 04:25

    I too ran into this problem with IE8. Amazing that this is happening; it seems like a problem we'd have run into circa 1998!

    Anyway, I did some testing and came up with a workaround that uses Javascript:

    <a href="javascript:doMailto()">Send Email</a>
    

    Then define this script:

    <script type="text/javascript">
       var sMailto = "mailto:name@name.com?subject=123456789&amp;body=111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111234567890";
    
       function doMailto() {
          document.location.href = sMailto;
       }
    
    </script>
    

    For whatever reason, IE has no problem "forwarding" to long URLs (my testing scenario uses 800+ characters). In cases with multiple mailto links, you could define multiple URL variables and send a parameter into doMailto() that lets it switch to the appropriate one.

    Is it ugly? You bet. But it works in a pinch.

    0 讨论(0)
  • 2020-12-03 04:26

    My advice would be to stop abusing the poor mailto link. If you want to prepopulate that much information, build a contact form for the user.

    0 讨论(0)
提交回复
热议问题