views:

370

answers:

2

I've been working on a project with a feature so that when I click a button, Outlook will open and the corresponding value stored in a variable will be in the in the body of the mail. I've tried the following code:

<html>
<head>
    <title>Email This Code Snippet</title>
    <script language="javascript">
    function TriggerOutlook()
    {         
        var sub = "Hi friend";
        var bodycont = "<html><body>welcome</body></html>";
        var body = escape(bodycont + String.fromCharCode(13));        
        window.location.href = "mailto:[email protected]"
                             + "?body=" + body
                             + "&subject=" + sub
        ;                
    }    
</script>
</head>
<body>
<form id="form1">
    <a href="#" onclick="TriggerOutlook()">Email this Codesnippet</a>
    <input type="text" name="txtbody" id="txtbody">
</form>
</body>
</html>

But the body of the mail is <html><body>welcome</body></html> in plain text, not HTML. How do I get it formatted as HTML?

+4  A: 

You can't. The mailto specification doesn't have that kind of control over the e-mail client invoked by the browser.

Welbog
+1: It all depends on the default setting in Outlook, or any other mail client. If the user has set the app to compose messages in HTML by default, then that's probably how it will appear. Otherwise, plain text. Again... the user decides, not the mailto line.
Jarret Hardie
+1. ‘body=’ is already unreliable just for plain text as it is. For reliable and flexible mail-sending, use server-side scripts.
bobince
+1  A: 

Interestingly, your code works for me as-is when Thunderbird is the default system mailer. Apparently Thunderbird is smart enough to notice that the body starts with <html> and switches to HTML-mail mode.

It would seem at first that to convince Outlook to behave similarly, you might try setting the Content-Type header in the email to "text/html". However, I don't see how this is possible using mailto since you do not have control over the headers.

David Citron