简体   繁体   中英

Prefill an email with HTML content

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:sakthivela@yahoo.com"
                             + "?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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM