0

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' . "\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>

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?

Anthony
  • 1,439
  • 1
  • 19
  • 36

3 Answers3

1

It is very hard to do it without opening the users email client if you want to stick to pure javascript. This would be a terrible way to do it as I suggest not to do it using javascript:

window.open('mailto:test@example.com?subject=subject&body=body');

This third party solution is better and you may want to check it out: https://medium.com/@mariusc23/send-an-email-using-only-javascript-b53319616782

urnotsam
  • 770
  • 7
  • 24
  • How would I get the body to show the current html page? Also, this is just for testing purposes. – Anthony Jun 15 '15 at 20:22
0

You can open the email client as shown above with even predetermined subject and body. If you add a textarea you can let users write the email already within your page:

<textarea id="myText">
    User writes stuff here
</textarea>
<button onclick="sendMail(); return false">Send</button>

SendMail calls your JS script, you only need to replace the second word body in your window.open to get the textarea text. This might return the HTML code within the HTML tags, which you can plug into body as well instead of the textarea text. (or outerHTML)

document.documentElement.innerHTML


window.open('mailto:test@example.com?subject=subject&body='+escape(document.getElementById('myText').value));

I also found this, but I don't know if this will suit you. http://www.emailjs.com/

Nils
  • 23
  • 1
  • 9
  • Thanks for the response but I am looking for the body of the email to be the entire html page. – Anthony Jun 17 '15 at 19:37
  • Doesn't document.documentElement.outerHTML return everything, like I mentioned? You might have to add the doctype yourself inside window.open since this does not inlude that I believe. You can also use document.getElementsByTagName('html')[0] to get everything within the head tags, and then add the doctype + html tags. Like in paulo's comment here: http://stackoverflow.com/questions/817218/get-entire-document-html-as-string – Nils Jun 17 '15 at 20:52
  • It doesn't work. Anyway I don't think it is possible to accomplish with javascript. So I decided to go about it using php. If you know anything about php check the update and edit section from my original post to see if you could help out. Thanks. – Anthony Jun 18 '15 at 14:26
0

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

Community
  • 1
  • 1
Anthony
  • 1,439
  • 1
  • 19
  • 36
  • Also, if you have a problem starting appache, my solution to the problem was just to quit skype then to start appache so it can be run on port 80. – Anthony Jun 19 '15 at 12:49