简体   繁体   English

如何使用JavaScript通过电子邮件发送表单的内容?

[英]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. 我有一个反馈HTML表单,需要将其结果发送到电子邮件地址。 How do I do that in JavaScript? 如何在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. 您需要使用某种背景技术,例如python,ruby,coldfusion,php或ASP才能将其发送到您的电子邮件中。

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. 这是一个免费的PHP服务 ,可让您执行类似的操作,但请记住,这种服务通常是不可靠的。

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

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. 有关某些陷阱,请参见当心Mailto Action表单

Impossible, for only with javascript, but with the help of server side script, you could. 不可能,仅适用于javascript,但可以借助服务器端脚本来实现。

Please take a look php mail function here 在这里看看php邮件功能

You have two options: 您有两种选择:

1) Use a mailto link. 1)使用mailto链接。 Javascript isnt really necessary although you could use Javascript to "click" the mailto link. 尽管您可以使用Java语言“单击” mailto链接,但Java语言并不是真正必需的。 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). mailto链接 mailto链接不可靠的原因有很多(请参阅下面的评论)。 They are not recommended. 不推荐使用它们。

2) Submit a form back to the server then send the email from the server. 2)将表单提交回服务器,然后从服务器发送电子邮件。 Whatever the server side code is (php, c# etc) should have the ability to send email. 无论服务器端代码是什么(PHP,C#等),都应该能够发送电子邮件。 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. 除了可能的AJAX,我没有其他办法。

EDIT: To clarify - AJAX would call another function like PHP to actually send the mail. 编辑:澄清-AJAX将调用另一个函数,如PHP来实际发送邮件。 You'd need to implement it serverside. 您需要在服务器端实现它。

EDIT2: Here's an article EDIT2: 这是一篇文章

its impossible :) but, you can use jQuery and backend application, written by python(django), php, asp.net, etc. 这是不可能的:),但是,您可以使用jQuery和后端应用程序,它们由python(django),php,asp.net等编写。

Simple jQuery script: 简单的jQuery脚本:

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

And PHP script: 和PHP脚本:

<?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. 您不能使用客户端 javascript发送邮件。 Submit the form to a server side script like PHP/JSP/ASP etc and send mail from that script. 将表单提交到服务器端脚本(如PHP / JSP / ASP等),然后从该脚本发送邮件。

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

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

you have to use server side scripting like php or perl 您必须使用服务器端脚本,例如php或perl

send the form content to the server side php. 将表单内容发送到服务器端php。 write using sendmail function in php. 在PHP中使用sendmail函数编写。

your content will be sent to that email. 您的内容将发送到该电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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