[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 532
  • Last Modified:

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

%>
0
naubrey
Asked:
naubrey
  • 5
  • 5
  • 4
  • +3
1 Solution
 
mgfranzCommented:
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.
0
 
ebosscherCommented:
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
0
 
mgfranzCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ebosscherCommented:
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?
0
 
mgfranzCommented:
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?
0
 
pjbaratelliCommented:
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.
0
 
mgfranzCommented:
Yeah... you just can't do that...

It needs to be uploaded first somehow, then it can be attached.
0
 
Wouter BoevinkCommented:
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.
0
 
mouattsCommented:
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
0
 
naubreyAuthor Commented:
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.
0
 
naubreyAuthor Commented:
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.
0
 
Wouter BoevinkCommented:
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
%>

0
 
mouattsCommented:
Wboevik: Ah! as I thought a file corrupter.
0
 
naubreyAuthor Commented:
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.


0
 
Wouter BoevinkCommented:
Mouatts:

"Ah! as I thought a file corrupter"

Please explain? Since I have no trouble using this script to upload documents, pictures, executables etc.
0
 
naubreyAuthor Commented:
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

0
 
Wouter BoevinkCommented:
If my script works fine, why didn't I get the points?
0
 
naubreyAuthor Commented:
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
0
 
Wouter BoevinkCommented:
Ask it in community support.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now