naubrey
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("CDONT S.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("attachedfile path")
objMail.Send
Set objMail = Nothing
%>
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("CDONT
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("attachedfile
objMail.Send
Set objMail = Nothing
%>
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
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("attachedfile path")?
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?
naubry ~ what gets printed if you do a response.write of your request.form("attachedfile
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?
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 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.
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
Steve
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.
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.
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.
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("BESTA ND")
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 .ServerVar iables("PA TH_INFO")) ) - 14
strPath = Left(Server.Mappath(Reques t.ServerVa riables("P ATH_INFO") ), lngPathEnd)
strFile = "uploaded-" & oUpload.FileName("bestand" )
Set oFSO = Server.CreateObject("Scrip ting.FileS ystemObjec t")
Set oFile = oFSO.CreateTextFile(strPat h & strFile)
For i = 1 To LenB(oUpload.Value("bestan d"))
oFile.Write Chr(AscB(MidB(oUpload.Valu e("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-da ta" 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-da ta" 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("Scrip ting.Dicti onary")
RequestBin = Request.BinaryRead(Request .TotalByte s)
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("Scrip ting.Dicti onary")
lngPos = InStrB(lngBoundaryPos, RequestBin, GetByteString("Content-Dis position") )
lngPos = InStrB(lngPos, RequestBin, GetByteString("name="))
lngPosBegin = lngPos + 6
lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
strName = LCase(GetString(MidB(Reque stBin, 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-Typ e:"))
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.RemoveA ll
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(Na me).Item(" Value")
Else
Value = Empty
End If
End Function
Public Function ContentType(Name)
Name = LCase(Name)
If pvObjUploadRequest.Exists( Name) Then
If pvObjUploadRequest.Item(Na me).Exists ("ContentT ype") Then
ContentType = pvObjUploadRequest.Item(Na me).Item(" ContentTyp e")
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(Na me).Exists ("FileName ") Then
FileNamePath = pvObjUploadRequest.Item(Na me).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(Na me).Exists ("FileName ") Then
strFileName = pvObjUploadRequest.Item(Na me).Item(" FileName")
FileName = Right(strFileName, Len(strFileName) - InstrRev(strFileName, "\"))
Else
FileName = Empty
End If
Else
FileName = Empty
End If
End Function
End Class
%>
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("BESTA
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
strPath = Left(Server.Mappath(Reques
strFile = "uploaded-" & oUpload.FileName("bestand"
Set oFSO = Server.CreateObject("Scrip
Set oFile = oFSO.CreateTextFile(strPat
For i = 1 To LenB(oUpload.Value("bestan
oFile.Write Chr(AscB(MidB(oUpload.Valu
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-da
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-da
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("Scrip
RequestBin = Request.BinaryRead(Request
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("Scrip
lngPos = InStrB(lngBoundaryPos, RequestBin, GetByteString("Content-Dis
lngPos = InStrB(lngPos, RequestBin, GetByteString("name="))
lngPosBegin = lngPos + 6
lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
strName = LCase(GetString(MidB(Reque
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,
objUploadControl.Add "FileName", strFileName
lngPos = InStrB(lngPosEnd, RequestBin, GetByteString("Content-Typ
lngPosBegin = lngPos + 14
lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))
strContentType = GetString(MidB(RequestBin,
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,
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.RemoveA
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(
Value = pvObjUploadRequest.Item(Na
Else
Value = Empty
End If
End Function
Public Function ContentType(Name)
Name = LCase(Name)
If pvObjUploadRequest.Exists(
If pvObjUploadRequest.Item(Na
ContentType = pvObjUploadRequest.Item(Na
Else
ContentType = Empty
End If
Else
ContentType = Empty
End If
End Function
Public Function FileNamePath(Name)
Name = LCase(Name)
If pvObjUploadRequest.Exists(
If pvObjUploadRequest.Item(Na
FileNamePath = pvObjUploadRequest.Item(Na
Else
FileNamePath = Empty
End If
Else
FileNamePath = Empty
End If
End Function
Public Function FileName(Name)
Dim strFileName
Name = LCase(Name)
If pvObjUploadRequest.Exists(
If pvObjUploadRequest.Item(Na
strFileName = pvObjUploadRequest.Item(Na
FileName = Right(strFileName, Len(strFileName) - InstrRev(strFileName, "\"))
Else
FileName = Empty
End If
Else
FileName = Empty
End If
End Function
End Class
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
"Ah! as I thought a file corrupter"
Please explain? Since I have no trouble using this script to upload documents, pictures, executables etc.
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
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?
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
naubrey@aubweb.com.au
Ask it in community support.
<%
Set objCDO = Server.CreateObject("CDO.M
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.