Solved

attachments with CDO

Posted on 2004-09-22
13
548 Views
Last Modified: 2008-12-24
I seem to be getting an unspecified error when trying to add an attachment to emails.  It works well however when I comment out the attachfile.

Is it something to do with physical paths? How can I work round this?
 <%
strCV = Request.Form("cv")  
Set sCDO = Server.CreateObject("CDONTS.NewMail")
   
               strSubject = "Subject"
            
               sCDO.To = "company name<name@domain.co.uk>"
               sCDO.From = strEmail
               sCDO.Subject = strSubject
               
               strBody = "The following visitor has submitted the following details :" & Chr(13) & Chr(10) & Chr(13) & Chr(10)
           strBody = strBody & "Name : " & strFirstName & " " &  strSurname & Chr(13) & Chr(10)
              strBody = strBody & ""
               
               sCDO.MailFormat = 0
               sCDO.Importance = 1
               
               sCDO.Body = strBody
               
               'sCDO.AttachFile = strCV

               sCDO.Send
               Set sCDO = Nothing %>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
//
function whatFile() {
window.location= 'file:///' + document.theform.cv.value;
}
//  End -->
</script>


<input name="cv" type=file class="form2" size="30">
0
Comment
Question by:aspguru28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12128806
That won't work!!!

You will have to upload the file to your server first and then attach it to your message.

So:

1) create a form that allows uploading of files
2) find some sort of upload script or component
3) upload the file to your server
4) attach the uploaded file to your message

This is a rather complicated affair.

FtB
0
 
LVL 6

Expert Comment

by:iscode
ID: 12129050
0
 
LVL 6

Expert Comment

by:iscode
ID: 12129095
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12129110
@iscode--

The big issue here is not sending the mail--the CDONTS code is already in place--but rather that you cannot send a file from a client machine without uploading the file to the server first.

FtB
0
 
LVL 6

Expert Comment

by:iscode
ID: 12129171
my second post deletes the file from server after sending it

would that work?
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12129206
>>my second post deletes the file from server after sending it<<

I couldn't see the code there without joining the forum, so I don't know.

FtB
0
 
LVL 6

Expert Comment

by:iscode
ID: 12129255

Fritz

I was posting to aspguru28 I thought it was he who was asking for help here?

and it  wasnt a question just a fact

cool down Fritz
0
 
LVL 6

Expert Comment

by:iscode
ID: 12129302
I can post that code here if you want it

let me know
0
 

Author Comment

by:aspguru28
ID: 12131096
Hi

Some code would be very handy. Thanks
0
 
LVL 6

Accepted Solution

by:
iscode earned 100 total points
ID: 12133232
aspguru28

ok its 2 files

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




cvupload2.htm
-----------------

<html>
<head>
<title>Upload your resume using this form </title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<script language="JavaScript">
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="8">
<form METHOD="POST" ENCTYPE="multipart/form-data" action="cvupload.asp?ACTION=1" name="mailform" id="mailform">
  <table border="1" align="center" width="625" bgcolor="#CCCCCC" bordercolor="#660033">
    <tr bordercolor="#FFFFFF">
      <td width="123"></td>
      <td width="486"></td>
    </tr>
    <tr bordercolor="#FFFFFF">
      <td valign="top" colspan="2" class="text">
        <h4 align="center"><font color="#660033"><strong><u>Upload Resume</u></strong></font></h4>
      </td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td class="text" align="left"><font color="#0000FF"><b>E-mail Id</b></font></td>
      <td valign="top" class="text" align="right">
        <div align="left">
          <input class="text" type=text name=MyEmailid size="35">
        </div>
      </td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td class="text" align="left"><font color="#0000FF"><b>Name</b></font></td>
      <td valign="top" align="right">
        <div align="left">
          <input class="text" name="MyName" size="35">
        </div>
      </td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td class="text" align="left"><b><font color="#0000FF">Position</font></b></td>
      <td valign="top" align="right">
        <div align="left">
          <select size="1" name=MyPosition>
            <option value="None">None</option>
            <option value="Analyst / Consultant">Analyst / Consultant</option>
            <option value="Business Development Manager">Business Development
            Manager</option>
            <option value="Bank Officer">Bank Officer</option>
            <option value="CEO/Chairman">CEO / Chairman</option>
            <option value="Company Secretary">Company Secretary</option>
            <option value="Customer Service Executive">Customer Service Executive</option>
            <option value="Engineer">Engineer</option>
            <option value="Editor / Journalist / Writer">Editor / Journalist /
            Writer</option>
            <option value="Finance Controller / Executive">Finance Controller
            / Finance Executive</option>
            <option value="Fresher">Fresher</option>
            <option value="Freelancer">Freelancer</option>
            <option value="General Manager / Director">General Manager / Director</option>
            <option value="Graphic Artist / Multimedia Programmer">Graphic Artist
            / Multimedia Programmer</option>
            <option value="Human Resources Executive">Human Resources Executive</option>
            <option value="House Wife">House Wife</option>
            <option value="Internet Programmer / Web Designer">Internet Programmer
            / Web Designer </option>
            <option value="Marketing / Sales - Manager">Marketing Manager (inc.
            Sales)</option>
            <option value="Marketing / Sales - Exec">Marketing / Sales Executive</option>
            <option value="Medical Transcriptionist">Medical Transcriptionist</option>
            <option value="Office Assistant">Office Assistant</option>
            <option value="Purchasing Manager">Purchasing Manager</option>
            <option value="Software Programmer">Software Programmer</option>
            <option value="Technical / IT Manager">Technical / IT Manager</option>
            <option value="Technician">Technician</option>
            <option value="Team Leader">Team Leader</option>
            <option value="Student">Student</option>
            <option value="Other">Other</option>
          </select>
        </div>
      </td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td valign="middle" class="text"><b>JobCode </b></td>
      <td valign="middle">
        <input type="text" name="jobcode" class="mystyle1" maxlength="8" size="9">
      </td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td valign="top"><b><font color="#660033">Any Other Info</font></b></td>
      <td valign="middle" class="text" align="right">
        <div align="left">
          <textarea class="text" rows="4" name="MyText" wrap="VIRTUAL" cols="35"></textarea>
          <b><i>(</i></b> <b><i>max 50 Words)</i></b> </div>
      </td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td valign="middle">
        <p><b></b></p>
      </td>
      <td valign="top" class="text" align="right">
        <div align="left"> </div>
      </td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td colspan="2" valign="top" class="text" align="center">
        <div align="left"><b><font color="#0000FF">Attach Resume:</font></b>
          <input TYPE="file" NAME="blob" value>
          <input TYPE="HIDDEN" NAME="where" value="Fileupload">
          <!-- change the value of this hidden field to change the directory of where uploads are stored-->
        </div>
      </td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td></td>
      <td></td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td colspan="2" valign="top" class="text"><font color="#660033"><i>Click
        <input class="text" type="submit" value="Submit" id=submit1 name=submit1>
        only once Pls !</i></font></td>
    </tr>
    <tr bordercolor="#CCCCCC">
      <td colspan="2" valign="top" class="text">
        <div align="right"><font color="#660033"> <font size="2">&nbsp;<font size="1">
          <span style='font-family:Arial'><a
  href="javascript:window.close();">CLOSE THIS WINDOW </a></span></font></font></font></div>
      </td>
    </tr>
  </table>
</form>
</body>
</html>

------------------
cvupload2.asp
------------------


<%
   Response.Buffer = true
   Function BuildUpload(RequestBin)

   PosBeg = 1
   PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
   boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
   boundaryPos = InstrB(1,RequestBin,boundary)

   Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
   Dim UploadControl
   Set UploadControl = CreateObject("Scripting.Dictionary")
   Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
   Pos = InstrB(Pos,RequestBin,getByteString("name="))
   PosBeg = Pos+6
   PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
   Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
   PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
   PosBound = InstrB(PosEnd,RequestBin,boundary)
   
   If PosFile<>0 AND (PosFile<PosBound) Then

        PosBeg = PosFile + 10
        PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
        FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
        UploadControl.Add "FileName", FileName
        Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
        PosBeg = Pos+14
        PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
        ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
        UploadControl.Add "ContentType",ContentType

        PosBeg = PosEnd+4
        PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
        Value = MidB(RequestBin,PosBeg,PosEnd-PosBeg)

   Else
 
        Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
        PosBeg = Pos+4
        PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
        Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))

   End If

        UploadControl.Add "Value" , Value
        UploadRequest.Add name, UploadControl
        BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
        Loop

   End Function

   Function getByteString(StringStr)
   For i = 1 to Len(StringStr)

        char = Mid(StringStr,i,1)
        getByteString = getByteString & chrB(AscB(char))

   Next
   End Function


   Function getString(StringBin)
        getString =""
   For intCount = 1 to LenB(StringBin)
        getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
   Next
   End Function


   If request("Action")="1" then

        Response.Clear
        byteCount = Request.TotalBytes
        RequestBin = Request.BinaryRead(byteCount)
        Set UploadRequest = CreateObject("Scripting.Dictionary")
        BuildUpload(RequestBin)
            
            '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

   If UploadRequest.Item("blob").Item("Value") <> "" Then

        contentType = UploadRequest.Item("blob").Item("ContentType")
        filepathname = UploadRequest.Item("blob").Item("FileName")
        filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
        FolderName = UploadRequest.Item("where").Item("Value")
        Path = Mid(Request.ServerVariables("PATH_TRANSLATED"), 1, Len(Request.ServerVariables("PATH_TRANSLATED")) - Len(Request.ServerVariables("PATH_INFO")))  
        ToFolder = Path & "\" & FolderName
        value = UploadRequest.Item("blob").Item("Value")
        filename = ToFolder & "\" & filename
        Set MyFileObject = Server.CreateObject("Scripting.FileSystemObject")
        Set objFile = MyFileObject.CreateTextFile(filename)
 
   For i = 1 to LenB(value)

       objFile.Write chr(AscB(MidB(value,i,1)))

   Next
       objFile.Close
       Set objFile = Nothing
       Set MyFileObject = Nothing

   End If
         jobcode = UploadRequest.Item("jobcode").Item("Value")
         MyPosition = UploadRequest.Item("MyPosition").Item("Value")
       MyText = UploadRequest.Item("MyText").Item("Value")
       MyEmailid = UploadRequest.Item("MyEmailid").Item("Value")
         MyName = UploadRequest.Item("MyName").Item("Value")
       Set UploadRequest = Nothing    
        
       body="<html><body><p align='center'><b><font size='5' face='Times New Roman' color='#0000FF'>Resume Details </font></b></p>"
      body=body & "<table align='center'>      <tr><td><font size='3' face='Times New Roman' color='#660033'><b>Post Applied for&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td><font size='3' face='Times New Roman' color='#660033'>" & jobcode & "</td></tr>"
body=body & "<table align='center'><tr><td><font size='3' face='Times New Roman' color='#660033'><b>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td><font size='3' face='Times New Roman' color='#660033'>" & MyName & "</td></tr>"
body=body & "<tr><td><font size='3' face='Times New Roman' color='#660033'><b>Current Position&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td><font size='3' face='Times New Roman' color='#660033'>" & MyPosition & "</td></tr>"
body=body & "<tr><td><font size='3' face='Times New Roman' color='#660033'><b>Other Info&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td><font size='3' face='Verdana' color='#0000FF'>" & MyText & "</td></tr>"
body = body & "</table></body></html>"

            
       Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
       ObjCDOMail.From = MyEmailid
       ObjCDOMail.To = "the mail id you want the message to be sent"
       ObjCDOMail.BodyFormat = 0
       ObjCDOmail.Mailformat = 0
       ObjCDOMail.Subject = "Application from a  " & MyPosition
       ObjCDOMail.Body = body

   If Len(filename) > 5 then

      ObjCDOMail.AttachFile filename,nikfile
   

   end if

       ObjCDOMail.Send
       Set ObjCDOMail = Nothing
        
         ' Now the file in the uploads dir can be deleted
        
                    Set fso = CreateObject("Scripting.FileSystemObject")
           
                              fso.DeleteFile filename
        set fso = nothing
        
   End If
   
 %>
<html>
<script language='javascript'>
alert("Thank You for Registering Yor Resume");
location.replace("cvupload2.htm");
</script>
</html>

-----------------

hope this helps you
0
 
LVL 46

Assisted Solution

by:fritz_the_blank
fritz_the_blank earned 25 total points
ID: 12142585
In regard to the upload portion, please see this very useful resource:

http://www.taka.nl/programming/asp/comparing_fileupload.asp

FtB
0
 

Author Comment

by:aspguru28
ID: 12341875
Guys

The spec of this has changed now and the client simply wants the file uploaded to a directory and a hyperlink within the body of the email to point to it.

Many thanks for your help though.  It's been interesting to look at.

0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12349931
Glad to have helped and good luck with the project,

FtB
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question