简体   繁体   中英

How can I send a form's contents over e-mail with JavaScript?

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?

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.

您唯一的选择是提交常规表格,然后在服务器端发送。

One way you can attempt to do this is have a

<form action="mailto:example@example.com">

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.

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

Please take a look php mail function here

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.

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

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("yourmail@mail.com","Subjects",$body))
        echo 'true';
    else echo 'false;';
}
?>

its simple, without security fix.

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.

您不能使用纯JavaScript来做到这一点,您可能也想为此使用ajax ,但是在幕后应该有一种服务器端语言来发送电子邮件。

如果您想要的东西甚至几乎可以满足一定数量的人的需求,那么在问题的约束范围内,您需要使用服务器端JavaScript (具体取决于您所使用的SSJS的特定实现, 请参见Whitebeam一个示例 )。

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.

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