Click to See Complete Forum and Search --> : How to insert Attachments to email form


ric73
10-12-2006, 10:51 AM
Hello,

I am creating a form that sends an email with some text fields and allows the user to choose a file from his computer and attach it to the email and send it to my web server. I receive the email with all the text fields but the attachement field is just the file location and not the file!

I am using FORM method="post" and I have tried to use enctype="multipart/form-data" with INPUT type="file" but then I do not receive anything at all.

Does my server not support the enctype attribute am I doing something wrong in the ASP code I send:

Sub SendContactEmail()
Dim strFrom
Dim strTo
Dim strSubject
Dim strBody

strFrom = Request.Form("Name") & " <" & ValidateFrom(Request.Form("EMail")) & ">"
strTo = "email@email.com"
strSubject = "Contact"
strBody = _
"Name: " & Request.Form("Name") & vbCrLf & _
"Telephone: " & Request.Form("Telephone") & vbCrLf & _
"EMail: " & Request.Form("EMail") & vbCrLf & _
"Architect: " & Request.Form("Architect") & vbCrLf & _
"Year Built: " & Request.Form("Year") & vbCrLf & _
"Location: " & Request.Form("Location") & vbCrLf & _
"Country: " & Request.Form("Country") & vbCrLf & _
"File: " & Request.Form("filefield") & vbCrLf & _
"Comments: " & Request.Form("Comments") & vbCrLf

EmailText strFrom, strTo, "", strSubject, strBody
End Sub

The form code is as follows without the enctype:

<FORM method="post" class="ContactField">

<DIV class="ContactLabel">Name:</DIV>
<DIV><INPUT type="text" name="Name" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Telephone:</DIV>
<DIV><INPUT type="text" name="Telephone" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">EMail:</DIV>
<DIV><INPUT type="text" name="EMail" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Architect:</DIV>
<DIV><INPUT type="text" name="Architect" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Year Built:</DIV>
<DIV><INPUT type="text" name="Year" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Location:</DIV>
<DIV><INPUT type="text" name="Location" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Country:</DIV>
<DIV><INPUT type="text" name="Country" class="ContactField" size="20"></DIV>-->

<DIV class="ContactLabel">Upload Image:</DIV>
<DIV><input type="file" name="filefield" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Comments:</DIV>
<DIV><TEXTAREA name="Comments" class="ContactField" scroll="auto" rows="1" cols="20"></TEXTAREA></DIV>

<DIV class="ContactSubmit"><INPUT type="image" src="/images/submit-acc.gif" width="87" height="17" align="middle"></DIV>
<DIV class="ContactNote">NOTE: Your information will never be shared with a third party!</DIV>
</FORM>


I am getting desperate! Can anyone help? THANK YOU

nihan
10-12-2006, 11:41 AM
I am trying to do the exact same thing. Let's let eachoter anything new :)

jvanamali
10-12-2006, 02:43 PM
send the code that you actually using to the send the email.

for sending attachments, first you have to upload to your server then attach the file

there is no mention of the file youe uploading in the code you sent

here is sample code to send email with attachment

Dim MyMail
Set MyMail = Server.CreateObject("CDONTS.NewMail")
Dim fs,f
MyMail.From = FromMe
MyMail.To = ToMe
MyMail.CC = Copy
MyMail.Subject = MySubject
MyMail.Body = MyBody
MyMail.BodyFormat = 1

MyMail.MailFormat = 1
if Attachment <> "" then
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.GetFile(Server.MapPath(Attachment))
MyMail.AttachFile f
end if
MyMail.Send
Set MyMail = Nothing
set f=nothing
set fs=nothing

Attach file should relative path of the file uploaded

ric73
10-12-2006, 03:13 PM
Thank you.

here is the complete code for the ASP page with the form:

<!--#include file="email.asp"--><%

If Request.Form.Count > 0 Then

'Form submission
SendContactEmail()
SendResponseEmail()
End If

Sub SendContactEmail()
Dim strFrom
Dim strTo
Dim strSubject
Dim strBody

strFrom = Request.Form("Name") & " <" & ValidateFrom(Request.Form("EMail")) & ">"
strTo = "ricardo@redcube.org"
strSubject = "Contact"
strBody = _
"Name: " & Request.Form("Name") & vbCrLf & _
"Telephone: " & Request.Form("Telephone") & vbCrLf & _
"EMail: " & Request.Form("EMail") & vbCrLf & _
"Architect: " & Request.Form("Architect") & vbCrLf & _
"Year Built: " & Request.Form("Year") & vbCrLf & _
"Location: " & Request.Form("Location") & vbCrLf & _
"Country: " & Request.Form("Country") & vbCrLf & _
"File: " & Request.Form("filefield") & vbCrLf & _
"Comments: " & Request.Form("Comments") & vbCrLf
EmailText strFrom, strTo, "", strSubject, strBody
End Sub


Sub SendResponseEmail()
Dim strFrom
Dim strTo
Dim strSubject
Dim strBody

strFrom = "Ricardo B <ricardo@redcube.org>"
strTo = Request.Form("Name") & " <" & ValidateFrom(Request.Form("EMail")) & ">"
strSubject = "Your message to Ricardo has been sent"
strBody = _
"Dear Architecture Aficionado," & vbCrLf & vbCrLf & _
"Your message:" & vbCrLf & _
Request.Form("Comments")
EmailText strFrom, strTo, "", strSubject, strBody
End Sub


%>

<FORM method="post" class="ContactField">
<!--<DIV><IMG src="/images/contact-acc.gif" width="174" height="27" style="margin-bottom:20px;"></DIV>-->

<DIV class="ContactLabel">Name:</DIV>
<DIV><INPUT type="text" name="Name" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Telephone:</DIV>
<DIV><INPUT type="text" name="Telephone" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">EMail:</DIV>
<DIV><INPUT type="text" name="EMail" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Architect:</DIV>
<DIV><INPUT type="text" name="Architect" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Year Built:</DIV>
<DIV><INPUT type="text" name="Year" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Location:</DIV>
<DIV><INPUT type="text" name="Location" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Country:</DIV>
<DIV><INPUT type="text" name="Country" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Upload Image:</DIV>
<DIV><input type="file" name="filefield" class="ContactField" size="20"></DIV>

<DIV class="ContactLabel">Comments:</DIV>
<DIV>
<TEXTAREA name="Comments" class="ContactField" scroll="auto" rows="1" cols="20"></TEXTAREA></DIV>

<DIV class="ContactSubmit"><INPUT type="image" src="/images/submit-acc.gif" width="87" height="17" align="middle"></DIV>
<DIV class="ContactNote">NOTE: Your information will never be shared with a third party!</DIV>
</FORM>

and here is the code on the include email.asp

<%

Sub SendEmail(strFrom, strTo, strBcc, strSubject, strBody, intBodyFormat)
Dim cdoMsg
Dim cdoConfig
Set cdoMsg = Server.CreateObject("CDO.Message")
Set cdoConfig = Server.CreateObject("CDO.Configuration")

With cdoConfig.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1 '1=local, 2=remote, 3=exchange
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "http://127.0.0.1"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "C:\inetpub\mailroot\pickup"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With

Set cdoMsg.Configuration = cdoConfig
cdoMsg.To = strTo
'cdoMsg.Cc = strCc
cdoMsg.Bcc = strBcc
cdoMsg.From = strFrom
cdoMsg.Subject = strSubject
cdoMsg.TextBody = strBody
cdoMsg.Fields("urn:schemas:httpmail:importance").Value = 1
cdoMsg.Fields.Update
'cdoMsg.AddAttachment "local path"
cdoMsg.Send
Set cdoMsg = Nothing
Set cdoConfig = Nothing

End Sub


Sub EmailText(strFrom, strTo, strBcc, strSubject, strBody)
Dim intBodyFormat

intBodyFormat = 1

SendEmail strFrom, strTo, strBcc, strSubject, strBody, intBodyFormat
End Sub


Sub EmailHtml(strFrom, strTo, strBcc, strSubject, strBody)
Dim intBodyFormat

intBodyFormat = 0

SendEmail strFrom, strTo, strBcc, strSubject, strBody, intBodyFormat
End Sub


'Functions
'---------
Function ValidateFrom(strFrom)
Dim regEx
Dim intRetVal
Set regEx = New RegExp
regEx.Pattern = ".*@.*\..*"
intRetVal = regEx.Test(strFrom)

If Not intRetVal Then
ValidateFrom = strFrom & " <invalid-address@" & Request.ServerVariables("REMOTE_ADDR") & ">"
Else
ValidateFrom = strFrom
End If

Set regEx = Nothing
End Function

%>

but somehow I am not uploading the file to the server. thanks for helping.
Best
Ricardo