Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML page as email attachment/content from JavaScript 1

Status
Not open for further replies.

nomadsolo

Programmer
Dec 28, 2006
11
IL
I need JScript to start up the client email editor and send a mail to the support team which includes a local HTML page either as mail content or attachment, all done by JScript. I use "mailto", however I cannot figure out how to send attachment like this. Please advise.
 
You'd be best off using server side scripting to do the email. That would make the process automatic rather than having to go through the client computer's email program.

Lee
 
In order to do the email as an attachment you would have to generate a file on the client's PC then attach it to the email and that opens up a lot of issues.

You do not have access to local resources on the client's PC. You could use ActiveX to create the email file locally and then send it but that requires IE as a browser and that the client respond approvingly to the ActiveX warning message that is bound to be generated.

As Trollacious said it is better to send the email server-side so you do not have to rely on the client's email system to send it.
That being said though, it brings up the question of how to generate the HTML formatted email.

In the past I have had forms that needed to be sent as an HTML formatted email. You can do this by creating a string containing all of the HTML required for the formatting of a duplicate of the original form and pass in the values entered within the form and you send that long string in a hidden form field to your processing page. Your processing page creates the email object and inserts that string as the body of the message.
The problem with this approach is you have to maintain two copies of the form. One copy for the displayed web form and one copy as the string to pass for the email. And you have to build that string to insert the form field values into it right before it is sent to the processing page.
This is cumbersome to maintain cause if you make a change to the original form you have to also make those changes in the output string you build which is a bit more comlex since you are formatting it in JScript and accomodating inserting of variables and escaping of quotes, etc.

My solution to simplify things was to write a page parseing script. I add my form email script to my form page and with very little markup the form will upon submission automatically parse out the page creating a copy of the form to be sent as an email and storing it in a hidden form field for the processing page to pick up.
Additionally I transform form fields so they are not editable in the submitted email version. Fields that can be are set readonly and fields like select boxes have additional changes so that only the selected option(s) is displayed. It get's a bit complex handling all the possibilities but the main goal was to have the output represent the original as accurately as possible without allowing the recipient to just change values previously entered.

I use HTML with Javascript for the form page and submit to an ASP page that sends out the email. You could use any server-side scripting to handle the emailing, you just read the value of the hidden form fields containing the recipient address, subject, body, etc.

If this sounds like an approach that might work for you I will dig up my code and post it.

At my age I still learn something new every day, but I forget two others.
 
Thank you for the great responses. As I am a .net developer, I know little of the intricacies of HTML development. However, I was called upon to develop this email widget. I am dealing with a client-side HTML page ( Siberlogic knowledgebase to be precise ) where there is no connection to a server. The button on his html page is supposed to send off an email to Support with the appropriate html page ( in a different frame than the button, although it doesn't really matter ) as an attachment. I cannot rely, therefore, on any server. I have used a form to attach the page, but the result was an unreadable .att file. I do not need access to the client's file system because I wish to attach a page from the frameset on which the button exists. Can I use input type ="file" to attach that page? Or, is there another way?
 
There are several obstacles here, the first is being able to create the page you want to attach to an email. In order to create an attachment you need file system access so that you can save the file to later attach to the email.
The type="file" command is a method to create an object reference in the browser pointing to a file system object on the local computer so that the file can be safely uploaded to a server without compromising the file system of the local PC. I am unaware of any way to create an object in the browser and use type="file" to send that object anywhere.

I am unfamiliar with Siberlogic Knowledgebase so you may have capabilities I am unaware of.

The second problem is the sending of the email itself.
Using MailTo you can instigate a new email object on the clients PC but you have no control over it sending and require the client to do so manually. Unfortunately that is the smallest problem. You are relying on the idea that the client PC has email software installed and configured with a valid email account. You also rely on the idea that the client is logged into their own PC and under an account that matches the email account. It is very common for any one or all of these requirements to be missing. Many people use web based email and have no email software on their device, or they log into someone else's computer, a shared computer or under a common or guest account. Any of these would cause you significant problems.

Can you rely on the client having only IE as a browser or know that the client has MS Outlook or Outlook Express? This may be possible in a corporate environment.

If you can rely on the client having Outlook on their PC then you could use either ActiveX to get access to open and send an email or you could use HTA (HTML for Applications) to do it. Both options require the client is using Internet Explorer.

Another option is to have the form submit itself to a web server elsewhere that has server-side scripting available to send the email, rather than relying on client side email.
This would of course require that the client have an active internet connection when they submit the form.

ActiveX requires IE to run and will almost certainly fire off a warning of a potential unsafe script to the client requiring them to approve the action before it allows it to run. The warning often scares people into denying the action.

HTA files run client-side with full permission to the local file system. If the page they open is local to their machine rather than on a web site then the script can run without the security warnings as the client has run the application locally themselves. This gives you access to create files locally and to run local applications. I have code that opens a new Outlook email message and submits it even without ever displaying it to the client. This of course requires Outlook to be on the client's PC.

The only solutions I see require that you are submitting the form to a server that is then able to process it for emailing or that the client must be running IE so that you have access to the client PC to handle the operations.

There are probably no good solutions that will work for everyone when running client-side without writing your own mailer application to run on the client's PC and running outside of the browsers security.

I am not saying there is no way to do it but I am not aware of any other methods. If you find one I would like to know what it is.
If you find that you can rely on IE and/or Outlook I can give you some code examples for portions of it.
The best method I am aware of though relies on a web server receiving the post from the form. It could be that the HTML form is running on the client's PC locally but it could still submit to a web server of your own.
Or the local HTML page could open up a web server form page thereby ensuring that they must be internet connected to begin filling out the form for a support request/notification. This is better in that you can always modify the form options without worrying about older copies on the clients end.


At my age I still learn something new every day, but I forget two others.
 
My employer wants to avoid server-related issues. Our support for this widget does not have to be robust to the max. We can assume that IE is present, as well as Outlook. If something goes wrong, we can simply start the email process without an attachment. In any event, the email editor must be visible so that the client can add content to the mail. Can you give me the HTA or Activex options in a nutshell ( assuming they can open up the mail editor to the client) ?
Thanks for the response.
 
Here is a very basic script for reading/writing to a file on the local PC. If you save it with the .hta extension and run it locally it will not trigger any ActiveX messages.
Code:
<html><head> 
<SCRIPT LANGUAGE='JavaScript'> 
var password='theirpassword'; 
var name='username'; 

function WriteToFile() { 
     var filename = 'c://temp.txt'; 
     var fso = new ActiveXObject('Scripting.FileSystemObject'); 
     if (fso.FileExists(filename)) { 
          var a, ForAppending, file; 
          ForAppending = 8; 
          file = fso.OpenTextFile(filename, ForAppending, false); 
          file.WriteLine(name); 
          file.WriteLine(password); 
          } 
     else { 
          var file = fso.CreateTextFile(filename, true); 
          file.WriteLine(password); 
          file.WriteLine(name); 
          } 
     file.Close(); 
     } 

function ReadIt() { 
     var filename = 'c://temp.txt'; 
     if (confirm('Do you want to see what we put on your computer?')) { 
          var fso, a, ForReading; 
          ForReading = 1; 
          fso = new ActiveXObject('Scripting.FileSystemObject'); 
          file = fso.OpenTextFile(filename, ForReading, false); 
          var name = file.readline(); 
          var password = file.readline(); 
          file.Close(); 
          document.write(name + '<br>'); 
          document.write(password); 
     } 
} 
</SCRIPT> 
</head> 
<body onload='WriteToFile();ReadIt()'> 
</body> 
</html>

You may not end up needing the above code. If you use an HTA page to trigger sending the email through Outlook you can also populate the body of the message with whatever you like so storing the page as a file then attaching it is not necessary unless you want the message sent as an attachment rather than as the email message itself.

The below code should be saved with a .hta extension. It contains the parameters you can set for the .hta file as well. It will open up an Outlook email message and populate the TO, Subject and Body of the email and optionally attach a file.

Code:
<html>
<head>
   <HTA:APPLICATION
   ID = "oApp"
   APPLICATIONNAME = "Your application name"
   BORDER = "thick"
   CAPTION = "yes"
   ICON = "Report.ico"
   SHOWINTASKBAR = "yes"
   SINGLEINSTANCE = "yes"
   SYSMENU = "yes"
   WINDOWSTATE = "normal"
   SCROLL = "yes"
   SCROLLFLAT = "yes"
   VERSION = "1.0"
   INNERBORDER = "yes"
   SELECTION = "no"
   MAXIMIZEBUTTON = "yes"
   MINIMIZEBUTTON = "yes"
   NAVIGABLE = "yes"
   CONTEXTMENU = "yes"
   BORDERSTYLE = "normal"
   >
<title>Your title</title>

<script language="vbscript">

Sub SendEmail()
  ESubject = "Subject of message"
  SendTo = "someone@somewhere.com"
  Ebody = "Testing ability to send an email."
  NewFileName = "C:\Test.txt"  'Path and name of file to attach if needed.

  Set App = CreateObject("Outlook.Application")
  Set Itm = App.CreateItem(0)
  With Itm
    .Subject = ESubject
    .To = SendTo
    .Body = Ebody
    '.Attachments.Add (NewFileName) ' Must be complete path
    .Display ' This property is used when you want
    ' the user to see email and manually send. Then
    ' comment out rest of code except “End With” statement
    ' and "End Sub" statement.
    '.send
  End With
  'Set App = Nothing
  'Set Itm = Nothing
End Sub

</script>
</head>
<body onload="window.resizeTo 900, 600">
<form method='post' action='' NAME='emailreport'>
<br>
<center>
  <input type="button" name="email" value="Send" onclick="SendEmail()">
</center>
</form>
</body>
</html>

I have a better script that works with an exchange server to look up the ID to send to and resolve it to the exchange address but that will only work if they are on an exchange server so I gave you this cut down version. It is VBScript not JScript but it should work for you and should not be terribly difficult to convert to JScript if you must use it.


At my age I still learn something new every day, but I forget two others.
 
Awesome response from niteowl. In plain old IE it works fine - I coded without the HTA part. I just had to approve all the warnings that came my way. I tried the code out on siberlogic and it ran an error - automation server can't create object . (Siberlogic is a product which uses HTML frames in a Microsoft Help platform.) It didn't give any yellow toolbars or warnings asking if I want to use activex. It just gave me the aforementioned error . I suspect that it won't let me include any risky code like activex. Changing the security features is not an option because I have to assume the client's system is set up normally. The code is great, though, and gives me complete control over the Outlook object. I am definitely going to use it for other apps. If there is another way to access the Outlook object, let me know, one that will not be blocked by default IE .

Regarding what you mentioned earlier, the HTML page which needs to be attached is already a file on the client's file system. I do not need to create any file. It is one of the help pages which is actually an HTML file. However, when I try and attach that file via HTML form, the resulting attachment is an .att file which, when opened, is unreadable. Perhaps I am making a simple oversight in this matter. Or, perhaps HTML files can't be attached because of their complex content. In addition, when attaching via form, the mail editor is not visible.

In summmary, I can use a regular attachment process as long as the file is readable and the email editor opens up. Please advise.
 
I am not sure why the file comes in as a .att, it could be the encoding of your email.
If attaching directly through Outlook you should be able to attach the HTML file just as you would any other.

In the code above there is a line for attaching a file.
It is currently commented out:
Code:
'.Attachments.Add (NewFileName)

If you remove the single quote from in front of that line and provide a value for NewFileName that is the full path to the file to attach then it should work.

This line:
Code:
'.send
Is currently commented out. This line tells Outlook to send the new email. Since it is commented out the message will just stay open on the screen until the client submits it.

At my age I still learn something new every day, but I forget two others.
 
I have implemented your solution in the following way : The button on the HTML Help page opens up an HTA file in a popup window, which then immediately opens up Outlook. I need to use a separate HTA file ( as opposed to opening Outlook directly from the button ) because the Help page will not allow access to Activex objects. In any event, it works beautifully except for one little detail. I need to send a string datum from the opening help page to the HTA application indicating the filepath to attach. The reason for this is that the file to be attached is not known at runtime. It will depend upon which page the client is viewing at the time he sends the email. (Bear in mind that for some reason the Help page is not giving me access to the 'opener' object.) Is there a way I can send this data from the HTML to the HTA ? Thank you for your response.
 
Any chance the path can be determined relative to the location of the .hta file?
If the location of the file is pretty much static but varies by drive letter you may be able to determine that through code. When the .hta file executes you can determine it's current path easily enough and if the attachment file will always be somewhere relative to the .hta file you can figure out where it is.

From what I have read you cannot pass parameters to the .hta on the querystring but you might be able to pass them as a command line parameter.
Look at the below link for an example.

At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top