如何使用按钮发送当前页面的电子邮件?
How do I send an email of the current page using a button?
我正在尝试发送我的 html 页面的电子邮件。这是我到目前为止的代码:
<script language="javascript">
function emailCurrentPage() {
window.location.href = "mailto:?subject=" + document.title + "&body=" + encodeURI(document.location);
}
</script>
<button onclick="emailCurrentPage()">Email page</button>
发送当前页面的 link,但我希望在电子邮件中显示整个 html 页面。有办法实现吗?
更新和编辑:所以我决定尝试使用 php 来解决这个问题,因为在我看来似乎到处都无法使用 javascript 来完成。我对 php 知之甚少。到目前为止,这是我的代码:
<?php
$to = 'john@example.com';
$subject = 'A test email!';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Put your HTML here
$message = file_get_contents('example.html');
// Mail it
mail($to, $subject, $message, $headers);
echo "Message Sent!"
}
?>
<form action="php/emailPage.php" method="post">
<input type="submit" name="submit" value="Email Page">
</form>
当我点击按钮时,我得到的只是 "Message Sent!" 消息,但没有电子邮件。我将 "john@example.com" 更改为我的电子邮件,但收件箱中没有收到电子邮件。关于问题是什么的任何想法?
如果你想坚持纯粹的javascript,不打开用户电子邮件客户端是很难做到的。这将是一种糟糕的方法,因为我建议不要使用 javascript:
window.open('mailto:test@example.com?subject=subject&body=body');
这个第三方解决方案更好,你可能想看看:https://medium.com/@mariusc23/send-an-email-using-only-javascript-b53319616782
您甚至可以打开带有预定主题和正文的如上所示的电子邮件客户端。如果您添加文本区域,您可以让用户在您的页面中编写电子邮件:
<textarea id="myText">
User writes stuff here
</textarea>
<button onclick="sendMail(); return false">Send</button>
SendMail调用你的JS脚本,你只需要替换你的window.open中的第二个word body就可以得到textarea文本。这可能 return HTML 标签中的 HTML 代码,您也可以将其插入正文而不是文本区域文本。 (或外HTML)
document.documentElement.innerHTML
window.open('mailto:test@example.com?subject=subject&body='+escape(document.getElementById('myText').value));
我也找到了这个,但不知道适不适合你。 http://www.emailjs.com/
我使用 php 和安装 XAMPP 解决了这个问题。我在 XAMPP 中使用了 apache、mysql 和 mercury。我按照 mercury here. I also had to change some things in the php.ini file and sendmail.ini file. Those steps can be found here 的步骤操作。希望这有助于减少其他人完成此任务的时间,因为我花了很长时间。 php代码和我原来的一样post。您还必须将代码放在以下目录 C:\xampp\htdocs 中。我在 htdocs 中创建了一个名为 HTMLTemplates 的文件夹。为了 运行 在我的浏览器中输入 localhost/HTMLTemplates/myfile.html
我正在尝试发送我的 html 页面的电子邮件。这是我到目前为止的代码:
<script language="javascript">
function emailCurrentPage() {
window.location.href = "mailto:?subject=" + document.title + "&body=" + encodeURI(document.location);
}
</script>
<button onclick="emailCurrentPage()">Email page</button>
发送当前页面的 link,但我希望在电子邮件中显示整个 html 页面。有办法实现吗?
更新和编辑:所以我决定尝试使用 php 来解决这个问题,因为在我看来似乎到处都无法使用 javascript 来完成。我对 php 知之甚少。到目前为止,这是我的代码:
<?php
$to = 'john@example.com';
$subject = 'A test email!';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Put your HTML here
$message = file_get_contents('example.html');
// Mail it
mail($to, $subject, $message, $headers);
echo "Message Sent!"
}
?>
<form action="php/emailPage.php" method="post">
<input type="submit" name="submit" value="Email Page">
</form>
当我点击按钮时,我得到的只是 "Message Sent!" 消息,但没有电子邮件。我将 "john@example.com" 更改为我的电子邮件,但收件箱中没有收到电子邮件。关于问题是什么的任何想法?
如果你想坚持纯粹的javascript,不打开用户电子邮件客户端是很难做到的。这将是一种糟糕的方法,因为我建议不要使用 javascript:
window.open('mailto:test@example.com?subject=subject&body=body');
这个第三方解决方案更好,你可能想看看:https://medium.com/@mariusc23/send-an-email-using-only-javascript-b53319616782
您甚至可以打开带有预定主题和正文的如上所示的电子邮件客户端。如果您添加文本区域,您可以让用户在您的页面中编写电子邮件:
<textarea id="myText">
User writes stuff here
</textarea>
<button onclick="sendMail(); return false">Send</button>
SendMail调用你的JS脚本,你只需要替换你的window.open中的第二个word body就可以得到textarea文本。这可能 return HTML 标签中的 HTML 代码,您也可以将其插入正文而不是文本区域文本。 (或外HTML)
document.documentElement.innerHTML
window.open('mailto:test@example.com?subject=subject&body='+escape(document.getElementById('myText').value));
我也找到了这个,但不知道适不适合你。 http://www.emailjs.com/
我使用 php 和安装 XAMPP 解决了这个问题。我在 XAMPP 中使用了 apache、mysql 和 mercury。我按照 mercury here. I also had to change some things in the php.ini file and sendmail.ini file. Those steps can be found here 的步骤操作。希望这有助于减少其他人完成此任务的时间,因为我花了很长时间。 php代码和我原来的一样post。您还必须将代码放在以下目录 C:\xampp\htdocs 中。我在 htdocs 中创建了一个名为 HTMLTemplates 的文件夹。为了 运行 在我的浏览器中输入 localhost/HTMLTemplates/myfile.html