Link to home
Start Free TrialLog in
Avatar of bb2000
bb2000

asked on

Create Email from JavaScript

Hi,

I have created a form which user enters in name, email, phone number and I want the details to get emailed to me when they click submit. I want the contents of the input boxes to appear in the email formatted as I would like them so that they can add additional information to the email is they wish. How is this possible - at the moment when they click submit to open up an email address to me but the message is blank.

Avatar of ahosang
ahosang
Flag of United Kingdom of Great Britain and Northern Ireland image

What code do you have at the moment?
Avatar of bb2000
bb2000

ASKER

<FORM ENCTYPE="text/plain" NAME="Conflict" METHOD='POST'
ACTION='mailto:test@test.com' onSubmit="return submitForm()">

I need to know how to add the contents of the input boxes into the message section of the email

That SHOULD work I think!?
So what's happening? What's the submitForm() function doing?
Avatar of Michel Plungjan
Only works if the mailclient is set up to handle it. Only client that works straight out of the box is netscape3 to 4.7

Michel
Avatar of bb2000

ASKER

It works fine I just want to know  how to create a messaage part. I want the contents of the input boxes to apear in the message section
try this:
<body>
<form name=myForm>
<input type="text" name="f1" value="">
<input type="text" name="f2" value="">
</form>
<a href="#"
onClick="this.href='mailto:michel@irt.org?subject=My%20Subject&body='+
escape(document.myForm.f1.value)+'%0A'+escape(document.myForm.f1.value); return true">Mail</a>


</body>
Avatar of bb2000

ASKER

is this possible in a function to create the message as i have about 10 input boxes and would like to format them neatly on the message. what does 'escape' refer to?
The escape is to convert all non a-z+&~0-9 to %xx type url encoded values.

<body>
<form name=myForm>
<input type="text" name="f1" value="">
<input type="text" name="f2" value="">
.
.
.
</form>
<a href="#"
onClick="loc='mailto:michel@irt.org?subject=My%20Subject&body=';
for (i=0;i< document.myForm.elements.length;i++)  {
   if (document.myForm.elements[i].type=='text')
      loc += document.myForm.elements[i].name+':'+
escape(document.myForm.elements[i].value)+'%0A';
}
this.href=loc;
return true">Mail</a>
or
<script>
function doMail() {
   var loc='mailto:michel@irt.org?subject=My%20Subject&body=';
   for (i=0;i< document.myForm.elements.length;i++)  {
      if (document.myForm.elements[i].type=='text')
      loc += document.myForm.elements[i].name+':'+
         escape(document.myForm.elements[i].value)+'%0A';
      }
   return loc;
}
</script>
<a href="#"
onClick="this.href="doMail();return true">Mail</a>

Michel
What you really need is mailform cgi on the server. This works exactly the way you want it to work and more, every time.
Avatar of bb2000

ASKER

can you explain how that works please and how to do it
The most used is Matt's Formmail:

http://worldwidemart.com/scripts/formmail.shtml

You need a cgi-bin directory - but most ISPs will have this script as canned script  - ask them!

Michel
Avatar of bb2000

ASKER

thanks but it doesn't really help as most of the links on that page appear to have expired. I'd prefer to use javascript if possible.
??? None of the links I clicked on were expired - including the download link...

You can use the script we have provided. If you want to make sure you get the data you need a script like Matt's
Avatar of bb2000

ASKER

Using the following code I create an email from the contents of a form. However I need to have each line on the BODY formatted so that they print on individual lines. How is this possible - at the moment it creates the Body section in one long line and characters seem to get cut off after about 256. Any ideas

function getbody()    
{
     strMessage =  "Conflict Posted By:" + document.emailForm.txt1.value
     strMessage1 = "Matter Partner:" + document.emailForm.lstClientPart.value
     strMessage2 = "Client:            " + document.emailForm.txtClient.value
     strMessage3 = "Industry Sector: " + document.emailForm.txtIndSector.value
     strMessage4 = "Client Connections: " + document.emailForm.txtClientConn.value
     strMessage5 = "Adverse Party: " + document.emailForm.txtAdParty.value
     strMessage6 = "Adverse Party Industry Sector: " + document.emailForm.txtAdPartyIS.value
     strMessage7 = "Adverse Party Connections: " + document.emailForm.txtAdPartyConn.value
     strMessage8 = "Brief description of matter: " + document.emailForm.txtBriefDesc.value
     strMessage9 = "Additional Information: " + document.emailForm.txtAddInfo.value
     strMessage10 = "Answer requested by: " + document.emailForm.lstAnswer.value
     strAll = strMessage + strMessage1 + strMessage2 + strMessage3 + strMessage4 + strMessage5 + strMessage6 + strMessage7 + strMessage8 + strMessage9 + strMessage10
}
     
function update(what) {
    what.action = 'mailto:' + document.hiddenForm.emailAddress.value +
                  '?SUBJECT=' + document.hiddenForm.subjectLine.value +
            '?BODY=' + strAll;
    what.elements[' '].value += '\n\n';
}
                 function getbody()    
                     {
                         strMessage =  "Conflict Posted By:" + document.emailForm.txt1.value
                         strMessage1 = "Matter Partner:" + document.emailForm.lstClientPart.value
                         strMessage2 = "Client:            " + document.emailForm.txtClient.value
                         strMessage3 = "Industry Sector: " + document.emailForm.txtIndSector.value
                         strMessage4 = "Client Connections: " + document.emailForm.txtClientConn.value
                         strMessage5 = "Adverse Party: " + document.emailForm.txtAdParty.value
                         strMessage6 = "Adverse Party Industry Sector: " + document.emailForm.txtAdPartyIS.value
                         strMessage7 = "Adverse Party Connections: " + document.emailForm.txtAdPartyConn.value
                         strMessage8 = "Brief description of matter: " + document.emailForm.txtBriefDesc.value
                         strMessage9 = "Additional Information: " + document.emailForm.txtAddInfo.value
                         strMessage10 = "Answer requested by: " + document.emailForm.lstAnswer.value
                         strAll = strMessage + "%A" +
strMessage1 + "%A" +
strMessage2 + "%A" +
 strMessage3 + "%A" +
 strMessage4 + "%A" +
 strMessage5 + "%A" +
 strMessage6 + "%A" +
strMessage7 + "%A" +
strMessage8 + "%A" +
strMessage9 + "%A" +
strMessage10
                     }

and I woudl use

"Conflict Posted By:" + escape(document.emailForm.txt1.value)

Michel
ANd the %A shoudl be %0A
sorry

Michel
Avatar of bb2000

ASKER

This does not work. I have tried exactly what you said but the message of the email is still one continuous string.

function update(what) {
    what.action = 'mailto:' + document.hiddenForm.emailAddress.value +
                  '?SUBJECT=' + document.hiddenForm.subjectLine.value +                
            '?BODY=' + "Conflict Posted By:" + escape(document.emailForm.txt1.value) + "%0A" + "Conflict Posted By:" + escape(document.emailForm.lstClientPart.value)
    //what.elements[' '].value += '\n\n';
}
Try
%0D%0A

Michel

%0A works in messenger

Michel
Avatar of bb2000

ASKER

I am using MS Outlook 97 and editing the code using visual interdev. The new one you mention does not work either.

'?BODY=' + "Conflict Posted By:" + escape(document.emailForm.txt1.value) + "%0D%0A" + "Conflict Posted By:" + escape(document.emailForm.txt1.value)
Then I cannot help you...

Michel
Avatar of bb2000

ASKER

Copy and paste the script below into a notepad and save as HTM file and view to see what I mean.,

<script language="JavaScript">
<!--
function Reset() {
      document.emailForm.PostedBy.value = "";
      document.emailForm.txtClient.value = "";
      document.emailForm.txtIndSector.value = "";
      document.emailForm.txtClientConn.value = "";
      document.emailForm.txtAdParty.value = "";
      document.emailForm.txtAdPartyIS.value = "";
      document.emailForm.txtAdPartyConn.value = "";
      document.emailForm.txtBriefDesc.value = "";
      document.emailForm.txtAddInfo.value = "";
      document.emailForm.lstAnswer.value = "";
}
function getbody()      
{
      strMessage =  "Conflict Posted By:" + escape(document.emailForm.txt1.value)
      strMessage2 = "Client:              " + escape(document.emailForm.txtClient.value)
      strMessage3 = "Industry Sector: " + escape(document.emailForm.txtIndSector.value)
      strMessage4 = "Client Connections: " + escape(document.emailForm.txtClientConn.value)
      strMessage5 = "Adverse Party: " + escape(document.emailForm.txtAdParty.value)
      strMessage6 = "Adverse Party Industry Sector: " + escape(document.emailForm.txtAdPartyIS.value)
      strMessage7 = "Adverse Party Connections: " + escape(document.emailForm.txtAdPartyConn.value)
      strMessage8 = "Brief description of matter: " + escape(document.emailForm.txtBriefDesc.value)
      strMessage9 = "Additional Information: " + escape(document.emailForm.txtAddInfo.value)
      strMessage10 = "Answer requested by: " + escape(document.emailForm.lstAnswer.value)
      strAll = strMessage + "%0A" + strMessage2 + strMessage3 + strMessage4 + strMessage5 + strMessage6 + strMessage7 + strMessage8 + strMessage9 + strMessage10
}
      
function update(what) {
    what.action = 'mailto:' + document.hiddenForm.emailAddress.value +
                  '?SUBJECT=' + document.hiddenForm.subjectLine.value +                
              '?BODY=' + "Conflict Posted By:" + escape(document.emailForm.txt1.value) + "%0D%0A" + "Conflict Posted By:" + escape(document.emailForm.txt1.value)
    //what.elements[' '].value += '\n\n';
}

                  var bV=parseInt(navigator.appVersion);
                  if (bV>=4)
                        {
                            document.write('<style>');
                            document.write(' b.gray {color:003366}');
                            document.write(' b.darkslategray {color:darkslategray}');
                            document.write(' font.grayback {background:dddddd}');
                            document.write(' td {font-family:verdana;font-size:11.5px; color:darkslategray}');
                            document.write(' td.bigger {font-family:verdana;font-size:12.5px; color:333333}');
                            document.write(' td.list {font-family:verdana;font-size:11.5px; color:333333}');
                            document.write(' td.header {font-family:verdana;font-size:12.5px; color:white;background:663300}');
                            document.write(' td.descriptor {font-family:arial; font-size:12px; color:000033}');
                            document.write(' td.footer {font-family:arial; font-size:11px; color:336666}');
                            document.write(' td.ct {font-family:verdana;font-size:10.5px; color:333333}');
                            document.write(' span {font-family:verdana; font-size:11.5px; color:003333}');
                            document.write(' pre {font-family:verdana; font-size:11px; COLOR:green; text-decoration: none;}');
                            document.write(' span.projectlinksec {color:336666}');
                            document.write(' span.dark {color:330000;font-size:13px}');
                            document.write(' span.grayedlink {color:slategray}');
                            document.write(' span.datestamp {font-family:arial; font-size:10px; color:gray}');
                            document.write(' span.clock {font-family:verdana; font-size:10.5px; color:333333}');
                            document.write(' a {font-family:verdana; font-size:11px; COLOR:navy; text-decoration: none;}');
                            document.write(' menubar a {font-family:verdana; font-size:11px; COLOR:navy; text-decoration: none;}');
                            document.write(' menubar a:hover {COLOR:#ffffff; TEXT-DECORATION: none;}');
                            document.write(' .divnav a {font-family:verdana; font-size:11px; COLOR:navy; text-decoration: none;}');
                            document.write(' .divnav a:hover {font-family:verdana; font-size:11px; COLOR:navy; text-decoration: underline;}');
                            document.write(' a.under {font-family:verdana; font-size:11.5px; COLOR:006666; text-decoration: underline;}');
                            document.write(' a.grayed {font-family:verdana; font-size:12px; COLOR:999999; text-decoration: none;}');
                            document.write(' a:hover {COLOR:#990033; TEXT-DECORATION: none;}');
                            document.write(' a.grayed:hover {COLOR: 999999; TEXT-DECORATION: none;}');
                            document.write(' a:active {COLOR: 990033; TEXT-DECORATION: none;}');
                            document.write(' a.grayed:active {COLOR: 999999; TEXT-DECORATION: none;}');
                            document.write(' option {font-family:verdana; font-size:12px; background-color:ffffee}');
                            document.write(' input.button {font-family:verdana; width:50; font-size:11px; cursor:hand}');
                            document.write(' select {font-family:verdana; width:415; font-size:12px;}');
                            document.write(' input {font-family:verdana; width:415; font-size:12px;}');
                            document.write(' textarea {font-family:verdana; width:415; font-size:12px;}');
                            document.write('</style>');
                        }
            //-->

//--></script>

<form name="hiddenForm" onSubmit="return false">
<input type="hidden" name="emailAddress" value="bcc">
<input type="hidden" name="subjectLine" value="Conflict Check">
</form>

<form name="emailForm" method="post" enctype="text/plain">
<TABLE>
            <tr>
                  <td width="30">&nbsp;</td>
                  <td colspan="2">
                        <b class="darkslategray">Conflict Checking</b>
                  </td>
            </tr>
            <tr>
                  <td width="30">&nbsp;</td>
                  <td colspan="2">
                  </td>
            </tr>
            <tr>
                  <td width="30">&nbsp;</td>
                  <td colspan="2">
                        <hr>
                  </td>
            </tr>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Conflict check posted by:</td>
      <td><INPUT name="txt1" rows="1" cols="20" onchange=getbody()></INPUT></TD>
</TR>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Client:</td>
      <td><INPUT name="txtClient" rows="1" cols="20" onchange=getbody()></INPUT></TD>
</TR>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Industry Sector:</td>
      <td><INPUT name="txtIndSector" rows="1" cols="20" onchange=getbody()></INPUT></TD>
</TR>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Client Connections:</td>
      <td><INPUT name="txtClientConn" rows="1" cols="20" onchange=getbody()></INPUT></TD>
</TR>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Adverse Party:</td>
      <td><INPUT name="txtAdParty" rows="1" cols="20" onchange=getbody()></INPUT></TD>
</TR>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Adverse Party Industry Sector:</td>
      <td><INPUT name="txtAdPartyIS" rows="1" cols="20" onchange=getbody()></INPUT></TD>
</TR>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Adverse Party Connections:</td>
      <td><INPUT name="txtAdPartyConn" rows="1" cols="20" onchange=getbody()></INPUT></TD>
</TR>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Brief description of matter:</td>
      <td><textarea name="txtBriefDesc" rows="3" cols="15" onchange=getbody()></textarea></TD>
</TR>
<TR>
      <td width="30">&nbsp;</td>
      <td align="right">Additional Information:</td>
      <td><textarea name="txtAddInfo" rows="3" cols="20" onchange=getbody()></textarea></TD>
</TR>
<tr>
      <td width="30">&nbsp;</td>
      <td align="right">Answer requested by:</td>
      <td>
            <select name="lstAnswer" onChange=getbody()>
                  <option value>Close of business today</option>
                  <option value>Close of business tomorrow</option>
                  <option value>ASAP</option>
                  <option value>End of this week</option>                              
                  <option value>End of this month</option>
            </select>
      </td>
</tr>
<tr>
      <td width="30">&nbsp;</td>
      <td colspan="2">
            <input type="submit" onClick=update(this.form) id=submit1 name=submit1>
      </td>
</tr>
<tr>
      <td width="30">&nbsp;</td>
      <td colspan="2">
            <input type="reset" onClick="reset" id=reset1 name=reset1>
      </td>
</tr>
</TABLE>


<input type="hidden" name="Internet Related Technologies - <URL" value="http://www.irt.org/>">
</form>

This is what I get
Subject:
               Conflict Check?BODY=Conflict Posted By:Poster%0D%0AConflict Posted
               By:Poster
         Date:
               Tue, 19 Jun 2001 17:47:02 +0200
        From:
               myemail@myserver.org
 Organization:
               myorg
           To:
               adressant@somewhere.com




txt1=Poster
txtClient=Client name
txtIndSector=sector 53
txtClientConn=none
txtAdParty=I hate you
txtAdPartyIS=
txtAdPartyConn=
txtBriefDesc=
txtAddInfo=
lstAnswer=Close of business today
submit1=Submit Query
Internet Related Technologies - <URL=http://www.irt.org
Avatar of bb2000

ASKER

When I use fill in the details I get the email and the subject filled in. The body section contains

Conflict Posted By:PosterConflict Posted by:Poster

What email system are you using as I am using outlook 97.


             
Netscape  Messenger - as I told you would work :}

Avatar of bb2000

ASKER

Well that's all very well but I'm using outlook so how do I get that to work.

Thanks anyway
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bb2000

ASKER

Ok - I'll start again and try that - thanks
This question has been abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.
<note>
   In the absence of responses, I may recommend DELETE unless it is clear
   to me that it has value as a PAQ.  Silence = you don't care
</note>

Cd&
Me for effort
It is time to clean this abandoned question up.  

I am putting it on a clean up list for CS.

<recommendation>
points to mplungjan

</recommendation>

If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.

Cd&
Comment from expert accepted a answer

Computer101
E-E Admin