问题
Ive got abit of a problem ive got an email web form that send the input to an email address but what I now need is a file input field were the user can also send an image as an attachment.
So contact name, logo (attachment).
Ive been told in order to send the attachment it needs to be saved in a folder on my hosting before it can be sent. Ive spoken to the hosting company and they dont have anything in place to make this easier such as aspupload.
In the form name="contactname" and name="logo" I have a folder in the root directory called logos (this asp page also exists in the root directory)
Man I hope someone can help me spent along time looking for answers
Dim contactname, logo
contactname = request.form("contactname")
If request("contactname") <> "" THEN
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Form"
myMail.From="web@email"
myMail.To="web@email"
myMail.HTMLBody = "" & contactname & ""
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "relay.host"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
myMail.Configuration.Fields.Update
myMail.Send
set myMail=nothing
回答1:
Can't you just use one of the following, depending on the format of the location you get from the input field? You shouldn't have to save it to the server.
<%
' ...
myMail.AddAttachment Server.MapPath("file1.txt")
myMail.AddAttachment "d:\file2.txt"
myMail.AddAttachment "file://d:\file3.txt"
' ...
%>
回答2:
You need to use a file input control. The basic idea is that you declare your form like this:
<form method="POST" action="YourScript.asp" enctype="multipart/form-data">
<input name="ContactName" type="text" size="50">
<input name="LogoFile" type="file">
<input type="submit" value="Send">
</form>
YourScript.asp should then use an ASP Upload control to store the uploaded file somewhere on the server, and then use the AddAttachment method of CDOSYS.
Note: When using this upload component, the normal Request.Form is no longer available (due to Response.BinaryRead being called). You can get to the ContactName value by using the Fields collection of this upload control instead.
Such an ASP Upload control can be found here:
http://www.asp101.com/articles/jacob/scriptupload.asp
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=7361&lngWId=4
回答3:
erm yeah I could use one of the above if the file was stored already but it not. The file is coming off the end users pc.
So they click browser in the form and navigate to the file on there pc. Which I think then needs to be saved to a file on my hosting then file location inserted kinda like you have done in order for it to be sent.
回答4:
For classic asp you need to first get file uploaded in binary data form:
Dim binaryData
For getting uploaded file in binary form just google and you shall get a lot of solutions for classic asp.You can go to for : http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=7361&lngWId=4
If you go by the above link, it shall be:
Dim binaryData = objUpload("File1").BLOB & ChrB(0)
Thereafter,you need not store the binary data in database.Just create a recordset object as given below:
set rset = server.createobject("ADODB.RECORDSET")
rset.fields.append "FileName", 205, LenB(binaryData)
rset.open
rset.addnew
rset.fields(0).AppendChunk binaryData
Then you can create CDO message object and proceed as:
Set myMail=CreateObject("CDO.Message")
'//your mail code here
myMail.Configuration.Fields.Update
binaryData = rset.fields("FileName").value
Const cdoContentDisposition = "urn:schemas:mailheader:content-disposition"
Const cdoBase64 = "base64"
Dim attach : Set attach = myMail.Attachments.Add
attach.ContentMediaType = "application/octet-stream"
attach.ContentTransferEncoding = cdoBase64
'//Here I am just attaching an jpeg image file with fixed name 'myimage.jpg'
attach.Fields(cdoContentDisposition).Value="attachment;filename=""myimage.jpg"""
attach.Fields.Update
Dim oStreamOutput: Set oStreamOutput = attach.GetDecodedContentStream
oStreamOutput.Write binData
oStreamOutput.Flush
myMail.Send
set myMail=nothing
This way you need not store the uploaded file in database or to your server space.You can just attach the file on the fly. Hope it helps.
来源:https://stackoverflow.com/questions/2639495/classic-asp-email-form-with-attached-file-please-help