faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

69 of 103 people (67%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Is it possible to use Javascript to attach a document to a "mailto" email?
Can I send the current page as an email/attach it to an email?

Feb 26th, 2001 08:44
Martin Honnen, Dil Sheikh,


Browsers have menu functions like
  File->Send page
to send a html page attached to an email.
To my knowledge this functionality is not exposed to JavaScript.
If you have a 
  mailto:
link you are able to put (plain) text into the mail's body with
  mailto: whomever@whereever.tld?body=line1\nline2\n\tline3

With NN4 using the fetchURL function described in
http://www.faqts.com/knowledge-base/view.phtml/aid/1268/fid/126/lang/en
that allows you to pass the document's html source as plain text to the 
mail body, and with NN6 you can reconstruct the HTML source from
  '<HTML>' + document.documentElement.innerHTML + '<\/HTML>'
:

<HTML>
<HEAD>
<SCRIPT>
function fetchURL(url) {
    if ((location.host == '' && url.indexOf(location.protocol) == -1)   
         ||
         url.indexOf(location.host) == -1) 
      {
      netscape.security.PrivilegeManager.enablePrivilege
("UniversalConnect");
      }
    var dest = new java.net.URL(url);
    var dis = new java.io.DataInputStream(dest.openStream());
    var res = "";
    while ((line = dis.readLine()) != null) {
      res += line;
      res += java.lang.System.getProperty("line.separator");
    }
    dis.close();
    return res;
  }

function mailPage (addressee) {
  var body;
  if (document.documentElement && document.all)
    body = document.documentElement.outerHTML;
  else if (document.all)
    body = document.body.innerHTML;
  else if (document.getElementById)
    body = '<HTML>' + document.documentElement.innerHTML + '<\/HTML>';
  else if (document.layers)
    body = fetchURL(location.href);
  var url = 'mailto:';
  url += addressee ? addressee : '';
  url += body ? '?body=' + escape(body) : '';
  location.href = url;
}
</SCRIPT>
</HEAD>
<A HREF="javascript: mailPage('whomever@whereever.tld'); void 0"
>
send page
</A>
</BODY>
</HTML>

The mailPage function also contains code intended for IE4/5 to grab the 
document's html and pass it on to the mailto: link. It doesn't work on 
my system however complaining about the email agent not being set up 
properly. I have no idea how to remedy that as simple mailto: urls 
without a ?body part work.