简体   繁体   English

通过电子邮件发送一个由 function 返回的值

[英]E-mail a value which is returned by a function

I was wondering if it is possible to email a value which is returned by a function in javascript?我想知道是否可以 email 一个由 javascript 中的 function 返回的值? or do i have to use php/ajax?还是我必须使用 php/ajax?

From the following example, I want to email abc to myself?从下面的例子中,我想把 email abc 给自己? How can it be done?怎么做到呢?

<html>
<head><script>
var t = "abc";
function test(){
return t;}
</script></head>
<body onload = "test()">
</body></html>

You'll need to post it to your server via XHR, then your server can email it.您需要通过 XHR 将其发布到您的服务器,然后您的服务器可以 email 它。

There's no direct method.没有直接的方法。 I would use jQuery' $.post to post the relevant information to a PHP page which would then mail it using, at it's simplest, the aptly-named mail function.我会使用 jQuery 的$.post将相关信息发布到 PHP 页面,然后使用最简单的命名邮件function 将其邮寄。

In one of the script tags of the main page or in a separate.js file:在主页的脚本标记之一或单独的 .js 文件中:

$.post("mailer.php", {data: "abc"}, function(data) {
  // Alert yourself whether it was successful if you want
});

And mailer.php (replace these with your own values):和 mailer.php (用您自己的值替换这些):

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = $_POST['data'];
$headers = 'From: webmaster@example.com';

mail($to, $subject, $message, $headers);
?>

Here is a very good explanation from another similar question :这是另一个类似问题的一个很好的解释:

You can't send an email with javascript , the closest you can get would be a mailto which opens the default email client - but that won't send anything.您不能使用 javascript 发送 email ,您可以获得的最接近的将是打开默认 email 客户端的 mailto - 但不会发送任何内容。

Email should be sent from the server - submit the form in the normal way, and construct the email on the server and send it.... Email 应该从服务器发送 - 以正常方式提交表单,并在服务器上构造 email 并发送它....

Another answer there gives some details about using mailto .那里的另一个答案提供了有关使用mailto的一些详细信息。

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

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