Link to home
Start Free TrialLog in
Avatar of naubrey
naubreyFlag for Australia

asked on

Sending email attachment using ASP and CDONT

Question: How to send an attached file using ASP CDONT coding. Here is the method as outlined in the book “ASP Developers Guide” Buczek, MCSD,  MCT. I can’t get it to work?

I am attempting to include an attached file via a form into this page
(A form is collecting and sending the following info into this page for processing)
The form includes a field using the following:
<input name="filepath" type="file" id="filepath" size="50">
Should this general method work?
Is there a better way?
<%@Language=VBScript%>
<%Option Explicit%>
<!--#include file="adovbs.inc"-->
<%
     Dim objMail
     Dim TheMessage
     Set objMail = Server.CreateObject("CDONTS.NewMail")
     objMail.To = "send@address.com"
     ObjMail.From = "from@address.com"
     ObjMail.Subject = "Job Application"
     TheMessage= "<HTML>" _
          & "<HEAD>" _
          & "<TITLE>Job Application</TITLE>" _
          & "<BODY>" _
          & "I am applying for job id:" & request.form("jobid") _
          & "<BR>The position is: " & request.form("position") _
          & "<BR>My full name is: " & request.form("fullname") _
          & "<BR>My email address is: " & request.form("emailfrom") _
          & "<BR>My contact phone is: " & request.form("contactphone") _
          & "<BR>Notes for the job are: " & request.form("note") _
          & "</BODY>" _
          & "</HTML>"
     objMail.Body = TheMessage
     objMail.Importance = 2
     objMail.BodyFormat = 0
     objMail.MailFormat = 0
     objMail.AttachFile & request.form("attachedfilepath")
     objMail.Send
     Set objMail = Nothing

%>
Avatar of Mark Franz
Mark Franz
Flag of United States of America image

Yes, sending an attachment is easy, here is what I use;

<%
Set objCDO = Server.CreateObject("CDO.Message")

     With objCDO
          .To       = strTo
          .From     = strFrom
          .Subject  = strSubject
          .AddAttachment "c:\data\file.doc"
          .HtmlBody = strBody
          .Send
     End With
     Set objCDO = Nothing
%>

Now remember, the path has to be absolute and the full path.
Avatar of ebosscher
ebosscher

naubrey ~

what you are getting with the file input type in html is not text (as far as i can tell) what you are getting is actually the binary format of the file (at least it is in the application where I upload files)  because you need a path and a physical file I think you may have to upload the file first?  Just a thought
ebosscher, it's an Attachment, it has nothing to do with MIME... Read here for more; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_denali_newmail_object_cdonts_library_.asp
ok, mgfranz - I guess I just need more information before I give an answer as pat as yours.

naubry ~ what gets printed if you do a response.write of your request.form("attachedfilepath")?

is this file local to the server you are running the script on, or does it reside on the local machine?

how do you acquire the path - is it using an input type of 'file' from an html form?
YOu bring up a good point ebosscher, if the "filePathName" is being passed by <input name="filepath" type="file">, then there is more work to do...

naubrey, you can't just get the filepath with this input type.  What are you using to upload the file?
I think ebosscher has a point. It looks like he is trying to upload the file and then email it the uploaded file as an attachment.
Yeah... you just can't do that...

It needs to be uploaded first somehow, then it can be attached.
It can be done.

submit the file to the send page.
In the send page you catch the binarystream in a string.
uudecode the string.
create the text message. Manualy add the attachment headers add the uuencoded string and send it.

But I guess that's a lot of work.

A better way is to cath the uploaded file stream, write it to a temp file. Attach the temp file and send it, then delete the temp file.

Take a look at the script I wrote at http://www.aspnl.com/aspnl/nl/artikelen/uploadscript.asp

This script script takes an uploaded file and put's it on the server, so there's no need for an upload component.
wboevink: Loved to have a look but your server was down. This isn't one of those componentless scripts that writes the file from ASP is it? Just that you can only write files in text format which means that the file can be corrupted by 10 and 13s appearing in it.

Steve
Avatar of naubrey

ASKER

To mgfranz
Are you presuming that the file has been uploaded to the server first.
Or
should it work from the remote machine?

I tried your script and I received an error stating it cant find the file.
Avatar of naubrey

ASKER

To mgfranz
Are you presuming that the file has been uploaded to the server first.
Or
should it work from the remote machine?

I tried your script and I received an error stating it cant find the file.
mouatts:

The script lets you write any file to the server uploaded from the browser. Not just text, binary files also.


uploadscript1.asp
--------------------------------------------
<%
Option Explicit

Dim oUpload

Set oUpload = New FileUpload

'Read the file and send it to the browser
Response.ContentType = oUpload.ContentType("BESTAND")
Response.BinaryWrite oUpload.Value("Bestand")

Set oUpload = Nothing
%>
<!--#include file="clsFileUpload.asp"-->

--------------------------------------------
uploadscript2.asp
---------------------------------------------
<%
Option Explicit

Dim oUpload
Dim oFSO, oFile
Dim lngPathEnd
Dim strPath, strFile
Dim i

Set oUpload = New FileUpload

lngPathEnd = Len(Server.MapPath(Request.ServerVariables("PATH_INFO"))) - 14
strPath = Left(Server.Mappath(Request.ServerVariables("PATH_INFO")), lngPathEnd)
strFile = "uploaded-" & oUpload.FileName("bestand")

Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(strPath & strFile)
For i = 1 To LenB(oUpload.Value("bestand"))
   oFile.Write Chr(AscB(MidB(oUpload.Value("bestand"), i, 1)))
Next

oFile.Close
Set oFile = Nothing
Set oFSO = Nothing

Response.Write "Thank you" & oUpload.Value("Naam") & ".<br>"
Response.Write "The file is saved as " & strPath & strFile

Set oUpload = Nothing
%>
<!--#include file="clsFileUpload.asp"-->

---------------------------------------------
uploadform.asp
---------------------------------------------
<HTML>
<BODY>
<FORM METHOD="Post" ENCTYPE="multipart/form-data" ACTION="uploadscript1.asp">
Naam: <INPUT TYPE="Text" NAME="Naam"><BR>
Bestand: <INPUT TYPE="file" NAME="Bestand"><BR>
<INPUT TYPE="submit" Value="Send to uploadscript1.asp">
</FORM>
<HR>
<FORM METHOD="Post" ENCTYPE="multipart/form-data" ACTION="uploadscript2.asp">
Naam: <INPUT TYPE="Text" NAME="Naam"><BR>
Bestand: <INPUT TYPE="file" NAME="Bestand"><BR>
<INPUT TYPE="submit" Value="Send to uploadscript2.asp">
</FORM>
</BODY>
</HTML>

---------------------------------------------
clsFileUpload.asp
---------------------------------------------
<%
Class FileUpload

   Private pvObjUploadRequest
   
   Private Sub Class_Initialize
      Dim RequestBin, Boundary, Value
      Dim lngPosBegin, lngPosEnd, lngBoundaryPos
      Dim lngPos, lngPosFile, lngPosBound
      Dim strName, strFileName, strContentType
      Dim objUploadControl

      Set pvObjUploadRequest = Server.CreateObject("Scripting.Dictionary")
     
      RequestBin = Request.BinaryRead(Request.TotalBytes)
     
      lngPosBegin = 1
      lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))
      Boundary = MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)
      lngBoundaryPos = InStrB(1, RequestBin, Boundary)

      Do Until (lngBoundaryPos = InStrB(RequestBin, Boundary & getByteString("--")))
         'Variabelen van objecten worden opgeslagen in Dictionary
         Set objUploadControl = Server.CreateObject("Scripting.Dictionary")

         lngPos = InStrB(lngBoundaryPos, RequestBin, GetByteString("Content-Disposition"))
         lngPos = InStrB(lngPos, RequestBin, GetByteString("name="))
         lngPosBegin = lngPos + 6
         lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
         strName = LCase(GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)))
         lngPosFile = InStrB(lngBoundaryPos, RequestBin, GetByteString("filename="))
         lngPosBound = InStrB(lngPosEnd, RequestBin, Boundary)

         If lngPosFile <> 0 And lngPosFile < lngPosBound Then
            lngPosBegin = lngPosFile + 10
            lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
            strFileName = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))

            objUploadControl.Add "FileName", strFileName
            lngPos = InStrB(lngPosEnd, RequestBin, GetByteString("Content-Type:"))
            lngPosBegin = lngPos + 14
            lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))

            strContentType = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))
            objUploadControl.Add "ContentType", strContentType

            lngPosBegin = lngPosEnd + 4
            lngPosEnd = InStrB(lngPosBegin, RequestBin, Boundary) - 2
            Value = MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)
         Else
            lngPos = InStrB(lngPos, RequestBin, GetByteString(Chr(13)))
            lngPosBegin = lngPos + 4
            lngPosEnd = InStrB(lngPosBegin, RequestBin, Boundary) - 2
            Value = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))
         End If

         objUploadControl.Add "Value" , Value  

         pvObjUploadRequest.Add strName, objUploadControl

         lngBoundaryPos = InStrB(lngBoundaryPos + LenB(Boundary), RequestBin, Boundary)
      Loop
   End Sub


   Private Sub Class_Terminate
      Dim objDictionary

      For Each objDictionary In pvObjUploadRequest.Items
         objDictionary.RemoveAll
         Set objDictionary = Nothing
      Next
      pvObjUploadRequest.RemoveAll
      Set pvObjUploadRequest = Nothing
   End Sub


   Private Function GetByteString(strString)
      Dim Char
      Dim i

      For i = 1 To Len(strString)
          Char = Mid(strString, i , 1)
         GetByteString = GetByteString & ChrB(AscB(Char))
      Next
   End Function


   Private Function GetString(strBin)
      Dim intCount

      GetString = ""

      For intCount = 1 To LenB(strBin)
         GetString = GetString & Chr(AscB(MidB(strBin, intCount, 1)))
      Next
   End Function


   Public Function Value(Name)
      Name = LCase(Name)
      If pvObjUploadRequest.Exists(Name) Then
         Value = pvObjUploadRequest.Item(Name).Item("Value")
      Else
         Value = Empty
      End If
   End Function


   Public Function ContentType(Name)
      Name = LCase(Name)
      If pvObjUploadRequest.Exists(Name) Then
         If pvObjUploadRequest.Item(Name).Exists("ContentType") Then
            ContentType = pvObjUploadRequest.Item(Name).Item("ContentType")
         Else
            ContentType = Empty
         End If
      Else
         ContentType = Empty
      End If
   End Function


   Public Function FileNamePath(Name)
      Name = LCase(Name)
      If pvObjUploadRequest.Exists(Name) Then
         If pvObjUploadRequest.Item(Name).Exists("FileName") Then
            FileNamePath = pvObjUploadRequest.Item(Name).Item("FileName")
         Else
            FileNamePath = Empty
         End If
      Else
         FileNamePath = Empty
      End If
   End Function


   Public Function FileName(Name)
      Dim strFileName

      Name = LCase(Name)
      If pvObjUploadRequest.Exists(Name) Then
         If pvObjUploadRequest.Item(Name).Exists("FileName") Then
            strFileName = pvObjUploadRequest.Item(Name).Item("FileName")
            FileName = Right(strFileName, Len(strFileName) - InstrRev(strFileName, "\"))
         Else
            FileName = Empty
         End If
      Else
         FileName = Empty
      End If
   End Function
End Class
%>

ASKER CERTIFIED SOLUTION
Avatar of mouatts
mouatts

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 naubrey

ASKER

Thank you so much for such a complete answer. I tested the script as you advised and it all works very nicely.

I now have to piece together how to use this code for my particular solution i.e.
I can now provide a form to enable job applicants to upload their resume.

How can I now make this uploaded resume available for reading by the site owner?

Can they use the script you provided to read the file into their browser? IF so how do you provide the file path. I am presuming they would be able to view a list of uploaded files and choose which one to look at.


Mouatts:

"Ah! as I thought a file corrupter"

Please explain? Since I have no trouble using this script to upload documents, pictures, executables etc.
Avatar of naubrey

ASKER

Sorry if my reply was confusing. Your script works just fine. I was very pleased with how easily it all worked.

I just have to apply it to my particular usage.
Provide the ability for regime's (job applications) to be uploaded for someone to view later.

This will involve having a directory/folder with permissions set in the server to allow files to be uploaded. The site administrator will then have to view the contents of the directory and read the resime's

If my script works fine, why didn't I get the points?
Avatar of naubrey

ASKER

A thousand apologies. I thaught I had awarded you the points. I must have clicked on the wrong answer link! its easy to do. Can I still award you points despite my mistake.
naubrey@aubweb.com.au
Ask it in community support.