2

I'm creating a form (using only HTML and JavaScript) that sends emails. I want to insert a simple HTML table inside the body of the email. Something like this:

<table border=1> <tr><td>blabla</td></tr> </table>

After I click on the "Send" button a JavaScript function is called which sends the email via URL and the POST data appears in my email client - Outlook (which is okay for me) but there is a problem with the formatting. The data in body of the email is in plain text.

  1. Is there any other way to do this only with JavaScript?
  2. Is it necessary to use some kind of server-side scripts (like PHP or others) to format it properly?
szamfirov
  • 21
  • 1
  • 1
  • 3

3 Answers3

1

You can use JavaScript mailto function. Try this, hope it'll help you

<div id="mailBody"><table border=1> <tr><td>blabla</td></tr> </table></div>
<input type="button" onclick="sendMail();">

Under the script tag

function sendMail()
{
   var mailBody=document.getElementById('mailBody').innerHTML;
   window.location="mailto:yourmail@domain.com?subject=hii&body="+mailBody;
}
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0

If you want to use only the java script to send HTML email try like below...

But i have NOT Recommended this type of Mail sending....

Sending HTML Email by Javascript :

function SendEmail()
{
    var outlookApp = new ActiveXObject("Outlook.Application");
    var nameSpace = outlookApp.getNameSpace("MAPI");
    mailFolder = nameSpace.getDefaultFolder(6);
    mailItem = mailFolder.Items.add('IPM.Note.FormA');
    mailItem.Subject = "Me";
    mailItem.To = "me@me.com";
    mailItem.HTMLBody = "<table border=1> <tr><td>blabla</td></tr> </table>";
    mailItem.display(0);
}

if you want to just send a email as plain text by javascript then try like below...

Sending Plain Text by Javascript :

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

Better you use a Server Side Coding to Send a email

Pandian
  • 8,848
  • 2
  • 23
  • 33
  • 3
    I didn't downvote your answer, but I imagine it's because this would only work when you've got both Internet Explorer and 0utlook, a limitation that you failed to mention. – Martijn Feb 27 '13 at 06:55
  • 4
    It does not work on any *browser*, only on the pile of rubbish called Internet Explorer. – connexo Dec 05 '19 at 15:13
-1

What are the < characters are like in if you look at the source? if they are like &#60; or &lt; then you need to disable encoding on these. Also, you need to set formatting to HTML mail. Sorry I am not a PHP guy but I guess you will need server side code for that.

Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
Subliminal Hash
  • 13,614
  • 20
  • 73
  • 104