dongmangsha7354 2015-06-15 19:53
浏览 57
已采纳

如何使用按钮发送当前页面的电子邮件?

I am trying to send an email of my html page. This is the code I have so far:

<script language="javascript">
    function emailCurrentPage() {
        window.location.href = "mailto:?subject=" + document.title + "&body=" + encodeURI(document.location);
    }
</script>

<button onclick="emailCurrentPage()">Email page</button>

Which sends the link of the current page but I want the whole html page shown in the email. Is there anyway of accomplishing this?

UPDATE and EDIT: So I have decided to try and go about this by using php since everywhere I look it seems impossible to accomplish with javascript. I know very little about php. Here is my code so far:

<?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' . "
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

    // 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>

When I click the button all I get is the "Message Sent!" message but no email. I changed the "john@example.com" to my email but I do not receive an email in my inbox. Any ideas on what the problem is?

  • 写回答

3条回答 默认 最新

  • dsh1956 2015-06-18 19:54
    关注

    I figured it out using php and installing XAMPP. I used apache, mysql, and mercury in XAMPP. I followed the steps for mercury here. I also had to change some things in the php.ini file and sendmail.ini file. Those steps can be found here. Hope this helps cut down the time for other people to accomplish this as it took me quite a while. The php code is the same as my original post. Also you have to put your code in the following directory C:\xampp\htdocs. I created a folder in htdocs called HTMLTemplates. In order to run this in my browser I typed in localhost/HTMLTemplates/myfile.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系