0
votes

I am new to classic ASP working on an application and I have a requirement of sending attachment with email. I have tried so many things but still not able to find a proper solution. I am able to send the emails without attachment.

Everything is working fine except attachments. I have seen many solutions suggesting before sending attachment I need to upload files to the server. I have called a page uploaddocument.asp (this page is in VBscript) in the form action that is uploading the documents on the server after clicking on send it is uploading the documents but it is not sending emails.

Please suggest if there is any way to store those attachment in a temp folder so that they will be removed automatically after sending.

Is it possible to design the functionality of attachment like yahoomail, or gmail with the progress bar, option of removing the attachment using classic asp javascript/VBscript.

Please find the dummy code.

MailTest.asp

<%@ Language=JavaScript%>
<%
Server.ScriptTimeout = 900;
var fso = new ActiveXObject("Scripting.FileSystemObject");
function mailObject() 
{
var mailObject = new ActiveXObject("CDO.Message");
mailObject.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;

mailObject.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtprelay.p3chem.net";
mailObject.Configuration.Fields.Update();


mailObject.From = "[email protected]";

return (mailObject);
}


var mailMessage = new mailObject();
messageText = "Test";
emailID="[email protected]";
AssetmailID ="[email protected]";
messageSubject ="test";
var op = String(Request("op"));

%>

<%

Response.write("<form method ='Post' action ='UploadDocument.asp' enctype='multipart/form-data'> \n");

Response.write("\n\nRecipient(s):
<textarea name=to rows=3 >"+emailID+"</textarea>*
\n\
(list of email addresses, separated by semicolon)

\n");
Response.write("\n\nCc:
<textarea name=cc rows=2 >" + AssetmailID + "</textarea>
\n\
(list of email addresses, separated by semicolon)

\n");
Response.write("Subject:
<input type=text name=subject size=60 >*

\n");

Response.write("Message:
<textarea name=message rows=15 cols=100 >" + messageText + "</textarea>*

\n");

Response.write("<form id = form2 method ='Post' action ='UploadDocument.asp' enctype='multipart/form-data'> \n"); 
Response.write("Attachment 1 : <input type=file name=File1>") ;


Response.write( "
<Input Type=Button Value='Add a file' >
") ;

Response.write("</form>\n");

Response.write("<input type=submit name=send value='Send Message'>

\n");
Response.write("<input type=hidden name=op value=send>");

Response.write("</form>\n");

switch (op) {
case ("Select"):
case ("send"):


try {
mailMessage.to =emailID;
mailMessage.Cc= AssetmailID 
mailMessage.Bcc = "vikrant.mittalMNC.com";

mailMessage.From = "[email protected]";
mailMessage.Subject = String(Request("subject"));
mailMessage.HtmlBody = String(Request("message"));


mailMessage.Send();
Response.Write("<script language='javascript'>alert('Mail successfully sent')</script>");
Response.Write("\nMail has been sent successfully ");

}
catch (err) {
Response.Write(err.message);

mailMessage.Bcc = "";

mailMessage.From = "";
mailMessage.Subject = String(Request("subject"));
mailMessage.HtmlBody = "ERROR: Mail was not sent succesfully, Check whether All Mail ID's are Valid.";
mailMessage.Send();
Response.Write("<script language='javascript'>alert('Mail was not sent succesfully')</script>");

Response.Write("Mail was not sent succesfully, Check whether All Mail ID's are Valid.\n\n" + Assessors);
}
break;
default:
Response.write ("Done");
}


%>


<Script>
//Script To add a attachment file field 
var nfiles = 1;
function Expand()
{
nfiles++
var adh = '
Attachment '+nfiles+' : <input type="file" name="File'+nfiles+'" action ="UploadDocument.asp" enctype="multipart/form-data">';
files.insertAdjacentHTML('beforeend',adh);
return false;
}
</Script>

code of UploadDocument.asp


<% @ Language= VBScript%> 
<%Option Explicit%>
<html>
<body>
<!-- #include file="lib\lib_vbFunctions.asp" -->
<%
Server.ScriptTimeout = 900

Dim Uploader, File, startTime, endTime, markTime1, totalTime, uploadTime, encType
Set Uploader = New FileUploader
startTime = Time
' This starts the upload process
Uploader.Upload()
markTime1 = Time
' Check if any files were uploaded
If Uploader.Files.Count = 0 Then
Response.Write "File(s) not uploaded."
Else
' Loop through the uploaded files
For Each File In Uploader.Files.Items
encType = File.ContentType
If (Len(encType) >= 50) Then encType = "application/octet-stream"
File.ADOSaveToDisk "F:\\CSDATA\\My_attachment",File.FileName
endTime = Time
totalTime = (endTime - startTime) * 10000000
uploadTime = (markTime1 - startTime) * 10000000
' Output the file details to the browser
Response.Write "File Uploaded: " & File.FileName & "
"
Response.Write "Size: " & File.FileSize & " bytes
"

Next
END if
%>
</body>
</html>

How can I send the uploaded file as the attachment?

1

1 Answers

0
votes

This is the trick I used for uploading file on server in a WYSIWYG:

  1. consider an input field named attachment in Email form.
  2. put a fake button beside this input say Browse.
  3. By clicking on the fake button, simulate a click on other hidden FORM in your document. This second form sends the file toward the uploader and receive the filename from uploader. To prevent the submission of second form and refreshing the page, use Jquery.form.js to upload the files and onSuccess pass the uploaded FileName to your real input named attachment.

Real Email Form:

<form action="sendEmail.asp" method="post">
    <input name="attachment"><a onclick="$('#hiddenFileField').click()">Browse</a>
    <!--other fields of email form-->
</form>

Hidden form which uploads the file on server:

<form id="hiddenForm" action="File-Uploader.asp" enctype="multipart/form-data" method="post" accept-charset="utf-8">
    <input name="file1" id="hiddenFileField" type="file">
</form>

and this is the scripts to handle the process:

$(document).ready(function(){
     $('#hiddenForm').ajaxForm({

       beforeSubmit: function() {
       $("#stat").html('uploading...');
       },

       success: function(data) {
           $("#attachment").val(data);
           $("#stat").html("");
       }
    });

    // The code to submit hidden form as soon as file selected to upload:
    $("#hiddenFileFild").change(function(){$("#hiddenForm").submit();})

});

You may modify the code to prevent submission of main from before completing the upload using #status.

Finally you can delete the temp file after sending email:

<%
dim fs
dim file
file=server.mappath(request.form("attachment")))
Set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists(file) then
  fs.DeleteFile(file)
end if
%>