Solved

Sending email attachment using ASP and CDONT

Posted on 2002-07-11
19
515 Views
Last Modified: 2008-03-10
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
Comment
Question by:naubrey
  • 5
  • 5
  • 4
  • +3
19 Comments
 
LVL 18

Expert Comment

by:mgfranz
ID: 7146221
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
 
LVL 6

Expert Comment

by:ebosscher
ID: 7146247
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
 
LVL 18

Expert Comment

by:mgfranz
ID: 7146269
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
 
LVL 6

Expert Comment

by:ebosscher
ID: 7146352
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
 
LVL 18

Expert Comment

by:mgfranz
ID: 7146374
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
 
LVL 1

Expert Comment

by:pjbaratelli
ID: 7146390
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
 
LVL 18

Expert Comment

by:mgfranz
ID: 7146411
Yeah... you just can't do that...

It needs to be uploaded first somehow, then it can be attached.
0
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 7146842
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
 
LVL 11

Expert Comment

by:mouatts
ID: 7150607
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:naubrey
ID: 7151034
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
 

Author Comment

by:naubrey
ID: 7151039
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
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 7151069
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
 
LVL 11

Accepted Solution

by:
mouatts earned 100 total points
ID: 7151150
Wboevik: Ah! as I thought a file corrupter.
0
 

Author Comment

by:naubrey
ID: 7151203
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
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 7151492
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
 

Author Comment

by:naubrey
ID: 7153319
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
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 7153774
If my script works fine, why didn't I get the points?
0
 

Author Comment

by:naubrey
ID: 7155841
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
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 7156495
Ask it in community support.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now