SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    VBScript - Email Issue with Email Attachment

    I have a peice of code which allows you attach a file and store in a folder and then send it in an email and My problem is when I add a text field and when I hit send I get a error message 'Bad file name or number' and when I remove the text field it works perfectly.
    Please let me know where I have missed Thanks
    My code is on a single page



    Code:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <%
    
        Func = Request("Func")
    	if isempty(Func) then
    
        		Func = 1
    
    	end if
        Select case Func
    
    		case 1
    
        %>
    </HEAD>
    <BODY>
    <P><FONT face="Arial" color="blue" size="4"><STRONG>Select a File 
      
      to send as attachment.</STRONG></FONT> </P>
    <form enctype="multipart/form-data" action="sendmail.asp?Func=2" method="post" id="form1" name="form1">
      <table>
        <tr>
          <td><FONT face="Arial"><STRONG></STRONG></FONT>
        <tr>
          <td><FONT face="Arial"><STRONG>File 
            
            Name...</STRONG></FONT></td>
        </tr>
        <tr>
          <td><input name="file1"  type="file">
            <br></td>
        </tr>
        <tr>
          <td>name:
            <input name="ename" type="text" >
            <br></td>
        </tr>
        <!--  <tr><td>phone:<input NAME="phone" type="text" /><br></td></tr>
                   <tr><td>email:<input NAME="email" type="text" /><br></td></tr>-->
        <tr>
          <td align="left"><input type="submit" value="Send!" ></td>
        </tr>
        <tr>
          <td></td>
        </tr>
      </table>
      <%
    
    
    		case 2
    
    			ForWriting = 2
    
        			adLongVarChar = 201
    
        			lngNumberUploaded = 0
    
        	
    
    				'Get binary data from form		
    
        			noBytes = Request.TotalBytes 
    
        				binData = Request.BinaryRead (noBytes)
    
        
    
    				'convert the binary data to a string
    
        			Set RST = CreateObject("ADODB.Recordset")
    
        				LenBinary = LenB(binData)
    
        	
    
        					if LenBinary > 0 then
    
        						RST.Fields.Append "myBinary", adLongVarChar, LenBinary
    
        							RST.Open
    
        								RST.AddNew
    
        									RST("myBinary").AppendChunk BinData
    
        								RST.Update
    
        						strDataWhole = RST("myBinary")
    
        					End If
    
        
    
        'Creates a raw data file for with all data sent. Uncomment for debuging.	
    
        	'Set fso = CreateObject("Scripting.FileSystemObject")
    
        	'Set f = fso.OpenTextFile(server.mappath(".") & "\raw.txt", ForWriting, True)
    
        	'f.Write strDataWhole
    
        	'set f = nothing
    
        	'set fso = nothing
    
        
    
        'get the boundry indicator
    
        strBoundry = Request.ServerVariables ("HTTP_CONTENT_TYPE")
    
        lngBoundryPos = instr(1,strBoundry,"boundary=") + 8 
    
        strBoundry = "--" & right(strBoundry,len(strBoundry)-lngBoundryPos)
    
        
    
        'Get first file boundry positions.
    
        lngCurrentBegin = instr(1,strDataWhole,strBoundry)
    
        lngCurrentEnd = instr(lngCurrentBegin + 1,strDataWhole,strBoundry) - 1
    
    
    
    do while lngCurrentEnd > 0
    
    			'Get the data between current boundry and remove it from the whole.
    
    			strData = mid(strDataWhole,lngCurrentBegin, lngCurrentEnd - lngCurrentBegin)
    
    				strDataWhole = replace(strDataWhole,strData,"")
    
        			
    
    			'Get the full path of the current file.
    
        		lngBeginFileName = instr(1,strdata,"filename=") + 10
    
        			lngEndFileName = instr(lngBeginFileName,strData,chr(34)) 
    
        
    
    			'Make sure they selected at least one file.	
    
        		if lngBeginFileName = lngEndFileName and lngNumberUploaded = 0 then
    
        	
    
        			Response.Write "<h2> The following error occured.</h2>"
    
        			Response.Write "You must select at least one file to upload"
    
        			Response.Write "<br><br>Hit the back button, make the needed corrections and resubmit your information."
    
        			Response.Write "<br><br><input type='button' onclick='history.go(-1)' value='<< Back' id='button'1 name='button'1>"
    
        			Response.End 
    
        	
    
        		end if
    
        
    
    			'There could be one or more empty file boxes.	
    
        		strFilename = mid(strData,lngBeginFileName,lngEndFileName - lngBeginFileName)
    
        
    
    			'Creates a raw data file with data between current boundrys. Uncomment for debuging.	
    
        		'Set fso = CreateObject("Scripting.FileSystemObject")
    
        		'Set f = fso.OpenTextFile(server.mappath(".") & "\raw_" & lngNumberUploaded & ".txt", ForWriting, True)
    
        		'f.Write strData
    
        		'set f = nothing
    
        		'set fso = nothing
    
        		
    
    			'Loose the path information and keep just the file name.	
    
        		tmpLng = instr(1,strFilename,"\")
    
        			
    
        			do while tmpLng > 0
    
        				PrevPos = tmpLng
    
        				tmpLng = instr(PrevPos + 1,strFilename,"\")
    
        			loop
    
        		
    
        				FileName = right(strFilename,len(strFileName) - PrevPos)
    
        	
    
    			'Get the begining position of the file data sent.
    
    			'if the file type is registered with the browser then there will be a Content-Type
    
        			lngCT = instr(1,strData,"Content-Type:")
    
        	
    
        				if lngCT > 0 then
    
        					lngBeginPos = instr(lngCT,strData,chr(13) & chr(10)) + 4
    
        						
    
        						else
    
        			 				lngBeginPos = lngEndFileName
    
        				end if
    
        
    
    			'Get the ending position of the file data sent.
    
        		lngEndPos = len(strData) 
    
        		
    
    			'Calculate the file size.	
    
        		lngDataLenth = lngEndPos - lngBeginPos
    
    				'Get the file data	
    
        			strFileData = mid(strData,lngBeginPos,lngDataLenth)
    
    				
    
    				' Create the file, change the path to the path U use for temp storage of the file
    
    				' make sure you have the IUSR_YOURCOMPUTER granted WRITE access to the uploads dir
    
        			Set fso = CreateObject("Scripting.FileSystemObject")
    
        				Set f = fso.OpenTextFile(server.mappath("temp") & "\" & FileName, ForWriting, True)
    
        					f.Write strFileData
    
        
    
    						' Get full Path, you need it for deleting the file later  	
    
        					Dim ThisFile
    
        				ThisFile = server.MapPath("temp") & "/" & filename
    
        
    
           			lngNumberUploaded = lngNumberUploaded + 1
    
        		
    
        		' destroy f, free memory
    
      		    set f = nothing
    
       			
    
    		'Get then next boundry postitions if any
    
       		lngCurrentBegin = instr(1,strDataWhole,strBoundry)
    
    	    lngCurrentEnd = instr(lngCurrentBegin + 1,strDataWhole,strBoundry) - 1
    
    loop
    
      
    
    dim ename
    ename	= Request("ename")
    BodyMsg = BodyMsg & "<tr bgcolor=#D6D6D6><td><b>Employee Name:</b></td><td>" & ename & "</td></tr>"
    'email	= Request("email")
    'phone	= Request("phone")
    '
    ' 	
    'BodyMsg = BodyMsg & "<style>.t9{font-family:arial;font-size:9pt;}</style>"
    'BodyMsg = BodyMsg & "<table width=500 bgcolor=#808080 class=t9 border=0 cellpadding=2 cellspacing=1>"
    'BodyMsg = BodyMsg & "<tr><td height=25 colspan=2 align=center><font color=#FFFFFF><b>Campaign Request Form:</b></font></td></tr>"
    'BodyMsg = BodyMsg & "<tr bgcolor=white><td><b>Email:</b></td><td>" & email & "</td></tr>"
    
    'BodyMsg = BodyMsg & "<tr bgcolor=white><td><b>Phone:</b></td><td>" & phone	 & "</td></tr>"
    '
    'BodyMsg = BodyMsg & "</table>"
        
        
        ' CDO mail object
    
        Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
    
        
    
        ' CDO mail setttings
    
    		with objCDOMail
    
    			.Body = BodyMsg
    
    			.Subject = "testing 123"
    
    			.From = "youraspwebapp@withattachment.com"
    
    			.To = "myemail@mail.com"
    
    			'.Cc = strCC				
    
    			.MailFormat = 0			
    
    			.AttachFile ThisFile
    
    			.Send
    
    		end with
    
    	
    
    	' destroy object, free memory
    
    	  set objCDOMail = nothing
    
        
    
        ' Now the file in the uploads dir can be deleted
    
          fso.DeleteFile ThisFile
    
        
    
        ' Destroy FileSystemObject, Free memory
    
           set fso = nothing
    
            		
    
        		Response.Write "Email Sent</h2>"
    
        		Response.Write "<br><br><input type='button' onclick='document.location=" & chr(34) & "sendmail.asp" & chr(34) & "' value='<< Back' id='button'1 name='button'1>"	 						
    
        	
    
    end select	
    
        
    
      
    
        %>
    </FORM>
    </BODY>
    </HTML>
    Attached Files Attached Files

  2. #2
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it probably fails because you cannot use the Request object after a BinaryRead.

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so what are you suggesting?

  4. #4
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    collect all text values such as email, name etc., and assign them to variables before you invoke the BinaryRead. You could do this at the same location where you do the Func variable assignment:


    Dim Func,ename
    Func = Request("Func")
    ename = Request("ename")

    ... etc.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    .. and you will have to pass the field values in the querystring; you could use a javascript function to append the text field vales to the action of the form.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •