Forum Moderators: open

Message Too Old, No Replies

How to send email from a page?

         

davilism

5:00 pm on Nov 17, 2004 (gmt 0)

10+ Year Member



I have a page that's intended to be sent by email after the viewer enters the information. How to do that in Javascript? Thanks.

Sanenet

5:05 pm on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll have to open the users email client and send that way. JS can't access serverside email clients.

You could try to do it using a cgi script like formmail or an asp or php server side script. Check your hosting information, it might already be included.

If you still want to go the js way check out resources like javascript.internet.com, they have loads of cool scripts.

Rambo Tribble

9:41 pm on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you mean by "a page", a form, the simplest way is to use a mailto URL as the value of the form's action attribute. Unfortunately, this fails to perform properly on some IE installations.

JavaScript can call an email client, though, of course, the client's browser must have the interpreter active. The following script demonstrates a JavaScript function to compose and send the email as well as a backup of a mailto action. Note that a mailto address in a page can be harvested by spambots.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function sendMail(_frm){
var eml="you@youraddress.com";
var bod="&body="+_frm.selOne.value+" ¦¦ "+_frm.txtOne.value;
var subj="?subject=Whatever you want";
document.location="mailto:"+eml+subj+bod;
}
</script>
</head>
<body>
<form action="mailto:you@youraddress.com" enctype="text/plain"
method="POST" onsubmit="sendMail(this);return false;">
<select name="selOne">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
</select>
<textarea name="txtOne"></textarea>
<input type="submit" value="Submit" />
</form>
</body>
</html>

davilism

2:42 am on Nov 18, 2004 (gmt 0)

10+ Year Member



Thanks, Rambo, but I wanted the whole page including the html format sent instead of the data. And is there any way to avoid invoking the client's email and still be able to send; I mean like submitting the page to some address?

Rambo Tribble

6:34 am on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While you can send a link to the page, there is no provision to package HTML pages for transmission outside of the server serving it up across HTTP. If it is your own page, you could read it and reduce it to a text string (with innerHTML, for example), but with pages from other domains you can't do much, security and all.

You can submit forms to the server without invoking the email client. You would need server-side scripts to handle the forms, for instance in PHP. Forms and email are the main ways of sending information from the client.

davilism

6:57 am on Nov 18, 2004 (gmt 0)

10+ Year Member



Thanks, then I think I will choose to send the page by email. Do u know how to write in javascript to invoke the "Send page by email" command with the specified Subject and to whom?

Rambo Tribble

1:55 pm on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the example I posted above.

davilism

4:04 pm on Nov 18, 2004 (gmt 0)

10+ Year Member



I tried but it just sends the values in the form, not the whole page. How to send the whole page just like I hit the "Send page by emial" that the web page appears in the email text area? Thanks again.

Is it possible to do it in jsp to send the page to an email address? I have some knowledge of jsp but not of sending page to email.

kaled

7:44 pm on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only way to send the entire page would be to use a server-side script (in perl for instance) and pass the url to it as a parameter. The script could then mail the page to the required address.

Writing such a script from scratch would be a considerable effort unless you already know perl or php, etc. You might be able to find something similar and adapt it, but this would still entail some effort.

Browsers are simply not designed to perform this function.

Kaled.

Rambo Tribble

2:03 pm on Nov 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you wish to reduce the page's markup to a string which can be included in an email, an example follows of innerHTML's use. If you are trying to create HTML email, you will certainly need more than JavaScript.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getInH(){
var bod=document.getElementById("pgBod").innerHTML;
alert(bod);
}
</script>
</head>
<body id="pgBod" onload="getInH();">
<p>Stuff to see</p>
</body>
</html>