Link to home
Start Free TrialLog in
Avatar of nickmacd
nickmacd

asked on

ASP Form emailing uploaded file

I have a form which enables someone to upload a file, now i want it to email me as a attachment along with other data entered inthe form.

The code im using is below.

<%@ Language=VBScript %>
<%
option explicit
Response.Expires = -1
Server.ScriptTimeout = 600
%>
<!-- #include file="freeaspupload.asp" -->

<%


' ****************************************************
' Change the value of the variable below to the pathname
' of a directory with write permissions, for example "C:\Inetpub\wwwroot"
  Dim uploadsDirVar
  uploadsDirVar = "E:\domains\m\miliberty.com\user\htdocs\Allegro_locum\form"
' ****************************************************

' Note: this file uploadTester.asp is just an example to demonstrate
' the capabilities of the freeASPUpload.asp class. There are no plans
' to add any new features to uploadTester.asp itself. Feel free to add
' your own code. If you are building a content management system, you
' may also want to consider this script: http://www.webfilebrowser.com/

function OutputForm()
%>
  <form name="frmSend" method="POST" enctype="multipart/form-data" action="uploadTester_2.asp" onSubmit="return onSubmitForm();">
      <B>File names:</B><br>
    File 1: <input name="attach1" type="file" size=35><br>
    <br>
    <B>File names:</B><br>
    <input name="Title" type="text" ><br>
    <input style="margin-top:4" type=submit value="Upload">
    </form>
   
<%
end function

function TestEnvironment()
    Dim fso, fileName, testFile, streamTest
    TestEnvironment = ""
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    if not fso.FolderExists(uploadsDirVar) then
        TestEnvironment = "<B>Folder " & uploadsDirVar & " does not exist.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
        exit function
    end if
    fileName = uploadsDirVar & "\test.txt"
    on error resume next
    Set testFile = fso.CreateTextFile(fileName, true)
    If Err.Number<>0 then
        TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have write permissions.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
        exit function
    end if
    Err.Clear
    testFile.Close
    fso.DeleteFile(fileName)
    If Err.Number<>0 then
        TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have delete permissions</B>, although it does have write permissions.<br>Change the permissions for IUSR_<I>computername</I> on this folder."
        exit function
    end if
    Err.Clear
    Set streamTest = Server.CreateObject("ADODB.Stream")
    If Err.Number<>0 then
        TestEnvironment = "<B>The ADODB object <I>Stream</I> is not available in your server.</B><br>Check the Requirements page for information about upgrading your ADODB libraries."
        exit function
    end if
    Set streamTest = Nothing
end function

function SaveFiles
    Dim Upload, fileName, fileSize, ks, i, fileKey

    Set Upload = New FreeASPUpload
    Upload.Save(uploadsDirVar)

      ' If something fails inside the script, but the exception is handled
      If Err.Number<>0 then Exit function

    SaveFiles = ""
    ks = Upload.UploadedFiles.keys
    if (UBound(ks) <> -1) then
        SaveFiles = "<B>Files uploaded:</B> "
        for each fileKey in Upload.UploadedFiles.keys
            SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "
        next
    else
        SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
    end if
end function
%>

<HTML>
<HEAD>
<TITLE>Test Free ASP Upload 2.0</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
BODY {background-color: white;font-family:arial; font-size:12}
</style>
<script>
function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "" && formDOMObj.attach2.value == "" && formDOMObj.attach3.value == "" && formDOMObj.attach4.value == "" )
        alert("Please press the Browse button and pick a file.")
    else
        return true;
    return false;
}
</script>

</HEAD>

<BODY>
<br>
<%
Dim diagnostics
if Request.ServerVariables("REQUEST_METHOD") <> "POST" then
    diagnostics = TestEnvironment()
    if diagnostics<>"" then
        response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">"
        response.write diagnostics
        response.write "<p>After you correct this problem, reload the page."
        response.write "</div>"
    else
        response.write "<div style=""margin-left:150"">"
        OutputForm()
        response.write "</div>"
    end if
else
    response.write "<div style=""margin-left:150"">"
    OutputForm()
    response.write SaveFiles()
    response.write "<br><br></div>"
end if



'declare the variables that will receive the values
'receive the values sent from the form and assign them to variables
'note that request.form("name") will receive the value entered into the textfield
'called name, and so with email and message
%>

<!-- Please support this free script by having a link to freeaspupload.net either in this page or somewhere else in your site. -->
<br>
<br>
</BODY>
</HTML>
Avatar of Om Prakash
Om Prakash
Flag of India image

You can use following code to send the attacment in the email after file uploading code:

Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "Example CDO Message" 
objMessage.From = "me@my.com" 
objMessage.To = "test@paulsadowski.com" 
objMessage.TextBody = "This is some sample message text." 
objMessage.AddAttachment "c:\temp\readme.txt" ' This will be the location where files are saved or can be from Upload.UploadedFiles(fileKey).FileName 
objMessage.Send 

Open in new window

Avatar of nickmacd
nickmacd

ASKER

Hi,

Thanks for your post...I'm having trouble on where to put that peice of code into my code as it keeps giving me errors.

I was wondering if you knew where to place your code in my code to get it working??
After this block :

   for each fileKey in Upload.UploadedFiles.keys
            SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "
        next
    else
        SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
    end if
'check the file is uploaded properly and then send mail or you can create function to send mail too...
Im sorry im only a beginner at ASP and having trouble getting this to work and write the function and was wondering if you would be able to write the code to get this working.

I know I must be a pain, but would be greatly appreciated for this.
ASKER CERTIFIED SOLUTION
Avatar of Om Prakash
Om Prakash
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok I have now got this working but cant get the  attachment path to work unless i hardcode the path in.....

objMessage.Subject = "Example CDO Message"  
objMessage.From = "me@my.com"  
objMessage.To = "mhayward@miliberty.com"  
objMessage.TextBody = "This is some sample message text."  
objMessage.AddAttachment "E:\domains\m\miliberty.com\user\htdocs\Allegro_locum\form\test.txt"
objMessage.Send

you will see the AddAttachment line.....i want this to pull through automatically.

Thanks for all your help

The reason was because I was directed to a website which was different to my code and I am still only learning and took me hours to get to work on my own from a number of different resources.