views:

161

answers:

11

I've got a feedback HTML form, the results of which I need to send to an e-mail address. How do I do that in JavaScript?

+1  A: 

Your only option is to submit it is a regular form and then send on server side.

Konrad Garus
A: 

Impossible, for only with javascript, but with the help of server side script, you could.

Please take a look php mail function here

S.Mark
A: 

You have two options:

1) Use a mailto link. Javascript isnt really necessary although you could use Javascript to "click" the mailto link. This will cause the user's email client to open with a new email prepopulated with the recipient's email address. mailto links There are numerous reasons why mailto links are unreliable (see comments below). They are not recommended.

2) Submit a form back to the server then send the email from the server. Whatever the server side code is (php, c# etc) should have the ability to send email. This way you can guarantee the email was sent.

You cannot silently send email from the browser. It has to either happen with user assistance or on the server.

andyjdavis
Don't use a mailto link, it relies on that user actually have an email client installed with a valid email address they can send emails from. Most emails that are attempted using a mailto link never actually get sent. Edit: For example, I don't even have an email client installed on my computer, so any mailto link I clicked would automatically fail because I don't have anything on my computer to handle that link request.
animuson
Yeah. mailto links are pretty old fashioned and won't work if the user isn't using their own computer or doesn't use a local email client. I'm sure there are plenty of other reasons it will fail.Server side email sending is the best way to do it if that is at all possible. mailto is an absolute last resort.
andyjdavis
im trying with forms. Can u pls tell me if u know that?
A: 

Other than possibly AJAX I know of no other way.

EDIT: To clarify - AJAX would call another function like PHP to actually send the mail. You'd need to implement it serverside.

EDIT2: Here's an article

McAden
A: 

its impossible :) but, you can use jQuery and backend application, written by python(django), php, asp.net, etc.

Simple jQuery script:

$('#button').click(function(){
    $.post('backend.php',
        {message:$('#message').val(),
         name: $('#name').val(),
         email:$('#email').val()},
         function(data) {
             $('.result').html(data);
         });
});

And PHP script:

<?php
if($_POST['message']) 
{
    $body = "Name: ".$_POST['name'];
    $body .= "<br>Email: ".$_POST['email'];
    $body .= "<br>Message: ".$_POST['message']
    if(mail("[email protected]","Subjects",$body))
        echo 'true';
    else echo 'false;';
}
?>

its simple, without security fix.

Vadik
im trying with forms. Can u pls tell me if u know that?
Do u mine jQuery Form plugin? I can help you, please email me: [email protected]
Vadik
A: 

You cannot send mails with client side javascript. Submit the form to a server side script like PHP/JSP/ASP etc and send mail from that script.

Amarghosh
-2? So I guess that should be possible: would appreciate if down-voter can teach me how to do that ;)
Amarghosh
e.g. with Whitebeam: http://www.whitebeam.org/library/guide/TechNotes/smtpreq.rhtm
David Dorward
@David Correct me if I am wrong: Whitebeam is a server side technology like php whose scripting language is similar to javascript. The example email script on the page you linked runs at the server and not in the browser like traditional client side javascript.
Amarghosh
Whitebeam is a server side technology, but the scripting language is not "like JavaScript" it **is** JavaScript. JavaScript is a programming language, and is not limited to client side use in web browsers.
David Dorward
@David Agreed. But I guess it is safe to assume that OP is looking for a client side javascript solution even though he hasn't explicitly mentioned it.
Amarghosh
Updated the post.
Amarghosh
+1  A: 

You can't. You will need to use some sort of background technology, such as python, ruby, coldfusion, php or ASP in order to send it to your email.

There's a few free services on the nets that will allow you to do that using their own resources, but if you are dealing with secure information, you better use your own resources.

Here's a free PHP service that will allow you to do something like that, but just remember that this kind of service is normally unreliable.

Marcos Placona
A: 

You can not do that with pure javascript, you might want to use ajax for that too but yet behind the scenes there should be a server-side language to send the email.

Sarfraz
why down vote.............?
Sarfraz
no idea - brought u back to 0.
Amarghosh
@ Amarghosh: thanks :)
Sarfraz
+1  A: 

One way you can attempt to do this is have a

<form action="mailto:[email protected]">

when submitting this will attempt to email the form to the given email address, this is a pretty nasty solution as it relies on the client having a correctly configured mail client. Also you have no controll over the presentation of the email. see Beware of Form Mailto Action for some of the pitfalls.

David Waters
A: 

If you want something that even comes close to working for a reasonable number of people then, keeping within the constraints of the question, you need to use server side JavaScript (the specifics depend on the particular implementation of SSJS you are using, see Whitebeam for an example).

David Dorward
A: 

you have to use server side scripting like php or perl

send the form content to the server side php. write using sendmail function in php.

your content will be sent to that email.

coder
-1: Duplicate of a number of earlier answers that doesn't add anything to them.
David Dorward