dongmangsha7354 2015-06-15 11: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 11: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部