[thelist] Re: Sending mail w/o Cdonts

jai tanwar jaytanwar at hotmail.com
Mon May 14 20:51:02 CDT 2001


Hi all

This is my First email to this List
Attached is a Asp script taken from this List(Written by Scott)  and Revised 
Further by me , What this Asp script do is as Follows
Its Reads an Html file From a Location setup in the Script and Writes the 
Contents of that File  to the
PICKMAIL directory of the Smtp Directory .
I need the Help in th following areas ,
a) How to read the Gif files which are part of the html file and are not the 
Url addresses
How to read and attach such files in the Html mail body using the 
FilesystemObject.
b)How to introduce some sort of Batch processing that is It Writes to the 
PICKMAIL directory
For first 5000 email Reocrds then after say 1-2 Hours to the Next 5000 email 
records.
c)How to read/Etract info from the Messages Which are in the "BadMail" or 
"dROP" Directory
of the Smtp server and Do something with them such as resending to them or 
extracting those
emails addresses  by parsing through those messages.

Hope some one can add more Value to the Following .

Thanks
Jai



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- #include file="../include/DataStore.inc"  -->
<!-- #include file="../include/adovbs.inc"  -->

<html>
<head>
<title>Untitled</title>
</head>

<body>
<%
'Sending the Email Without Using the Cdonts Object
'Used the Server Variable to Get the Path Info and Printed It On Browser.
'd:\Inetpub\wwwroot\einfoasia-web\marketing\withoutcdonts.asp,
'This is the Better Way of sending the Bulk emails Faster and in
'a Easier manner . 'Its Using the IIS - SMTP server For sending the emails 
to the Users.Which On our server
'Is installed a C:\Inetpub\Mailroot\Pickup and another TempMail Directory  
is Created
'to Place all the

'#Function for Randomizing the message name which is Generated.
Response.write Request.ServerVariables("Path_Translated")
Function GenMIMEName()
   Randomize   ' Initialize random-number generator.
   RandomNbr = Int((1000000 * Rnd) + 0)
   RandomNbr = Right("0000000" + RandomNbr, 6)
   GenMIMEName = Request.ServerVariables("REMOTE_ADDR") & "_" & RandomNbr & 
".email"
End Function
%>
<%
'Enumerate through file
'We need to have some specific headers and such, here's what a MIME email 
looks like before its sent out:

'Return-Path:<support at ti3.com>
'Date: Mon, Aug 28 2000 17:41 -0600
'To: <sgd at ti3.com>
'From: <support at ti3.com>
'Subject: [Friday Freebie] Look, ma no CDONTS!
'MIME-Version: 1.0
'Content-Type: text/plain; charset="US-ASCII"
'Content-transfer-encoding: 7bit
'ContentType is Used to setup Wheather the Content send is of plain text 
type
'or the content send is of the htm/html type.This Variable can be Changed to 
make
'The Content to be send as of  Plain or Html type.
'This is the body of the email.

Function GenMIMEDate(byval mydate, byval gggg)
' mydate is already a date type, no error checking here!
' gggg is expected to already be in offset from GMT format, i.e. "-0600"
' we need to return ddd, mmm dd yyyy hh:nn gggg
ddd = weekdayname(weekday(mydate),True)
mmm = monthname(month(mydate),True)
dd = day(mydate)
yyyy = year(mydate)
hh = hour(mydate)
nn = minute(mydate)
GenMIMEDate = ddd &", "& mmm &" "& dd &" "& yyyy &" "& hh &":"& nn &" "& 
gggg
end function
%>

<%
'File handling: WriteEmail Function is Writing the Files to the TEMPMAIL 
Directory
'of the Smtp Server.
'Now we gotta write it to a file, in a temporary spot.
'This is because if we create the file in IIS's SMTP Server Pickup 
directory,
'it will try to slurp it up before we're done, and that's Bad.
'directory? —\InetPub\MailRoot\Pickup (using the default install name for 
the Inet root).
'Create a directory for the temp spot; we use 
D:\InetPub\MailRoot\Pickup\TempMail.
'Let's write it to file, shall we?


Sub WriteEmail(byval email, byval filename)
Dim ForAppending,fs,a,logstr
' drop the email to a file
ForAppending = 1
filename= "c:\Inetpub\Mailroot\Pickup\tempmail\" & filename
'filename = "D:\InetPub\Mailroot\Pickup\tempmail\" & filename

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(filename, ForAppending, True)
a.Write(email)
a.Close
Set a = Nothing
Set fs = Nothing
End Sub

'Move the temp file into the Pickup directory 'Well, so far we gots the 
email being generated with all the MIME we need (for now),
'and we're writing it to the file system with no probs.
'Now we gotta move it to the Pickup directory to actually get it sent.
'I'll reopen the WriteEmail subroutine and tweak a few things to get it 
done:


Sub WriteEmail(byval email, byval filename)
Dim ForAppending,fs,a,logstr
' drop the email to a file
tempdir ="tempmail\"
pickupdir = "c:\Inetpub\Mailroot\pickup\"
'pickupdir = "D:\InetPub\Mailroot\Pickup\"
ForAppending = 8
tempfilename = pickupdir & tempdir & filename
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(tempfilename, ForAppending, True)
a.Write(email)
a.Close
Set a = Nothing
'fs.MoveFile tempfilename, pickupdir & filename
Set fs = Nothing
End Sub



'Now, let's see the function calls in action:

' use _ to use multiple lines in VBScript
' email = genMIMEEmail("jai at einfoasia.com", _
  '                   "jai at einfoasia.com,_

    '                 "[Friday Freebie] Look, ma no CDONTS!", _
     '                "This is the body of the email. ")
' WriteEmail email,GenMIMEName'

'This is the 2nd Part of the Script From the 2nd Article Where the 
Attachments and
'Reading From a File these Issues are Dealt With.'we have to
'change the MIME type so the email program on the receiving end knows to
'parse it out and separate the attachment from the email body. The MIME type
'is named "multipart/mixed" --a generic term that says, "Hey I've got
'multiple pieces and each piece may have its own MIME type.

'Const MIMEBOUNDARY = "XXXX MESSAGEBOUNDARY XXXX"
'The Mimeheader For the Emails that We are Generally Sending are of type 
text/html
'If there is Mixed Type then Content-type:multipart/mixed Should be Used to 
send the Emails
'Message.Mime type is important Because it will Decide the Way Message Looks 
to the End user."
mimeheader = "Content-type:text/html"
'mimeheader = "Content-Type: multipart/mixed; boundary=""" & MIMEBOUNDARY & 
""""

'For the body of the email (from what I understand the body is always the
'first part in a multipart message).

'bodyMIME = "Content-Type: htm; charset=""US-ASCII""" & vbNewLine
'bodyMIME = bodyMIME & "Content-transfer-encoding: 7bit" & vbNewLine & 
vbNewLine

'For the attachment, there are options. We can attach anything, from a jpeg
'file to an Access database. Since we're limiting this to attaching text
'files (for demonstration), let's worry about just the minimum requirements:

'attachMIME = "Content-Type: html; name=""reghelp.htm""" & vbNewLine
'attachMIME = attachMIME & "Content-Transfer-Encoding: quoted-printable" & 
vbNewLine
'attachMIME = attachMIME & "Content-disposition: attachment; 
filename=""reghelp.htm"""
'attachMIME = attachMIME & vbNewLine & vbNewLine


'Let's slurp in a file from the file system, returning
'Empty is it doesn't exist or encounter any errors (like the path not being
'found):


Function ReadAttachment(byval filename)
On Error Resume Next
Dim ForAppending,fs,a,logstr
' slurp in an attachment
ForReading = 1
'For 1 For Reading only,  Reading and Writing = 2 for opening the File for 
reading as well as Writing
'8 = For appending the File
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(filename, ForReading)
' first determine if it exists
if a.AtEndOfStream then
slurped = Empty
else
slurped = a.ReadAll
end if
a.Close
Set a = Nothing
Set fs = Nothing
ReadAttachment = slurped
End Function

'The caveat here is that you have to know the FULL path to the file,
'otherwise you'll get an empty attachment. The other side of that coin is we
'don't want the full path name in with the file name. I don't want the
'recipient to know my directory structure. So to that end:


Function StripPath(ByVal strFilename)
' grab the filename from the full path, basically everything after the last 
\
patharray = Split(strFilename, "\")
tmpname = patharray(UBound(patharray))
If InStr(tmpname, """") <> 0 Then ' take out double quotes
    tmpname = Replace(tmpname, """", "")
End If
StripPath = tmpname
End Function


'Now that we know what to change, and we've got what we need to attach a
'file,  Let's makeour boundary string a constant and tweak functions 
GenMIMEHeaders() and
'GenMIMEEmail():


'Const MIMEBOUNDARY = "XXXXMESSAGEBOUNDARYXXXX"

'********* GenMIMEHeaders ************
function GenMIMEHeaders(byval replyto, byval from, byval mto, byval subject)
replyto = "<"& replyto &">"
from = "<"& from &">"
mto = "<" & mto   &">"
sendto = replace(mto,"'","")
'sendto = split(mto,",")
'for each addr in sendto
'tolist = "<"& addr &">," & tolist
'Next
'tolist = Left(tolist,len(tolist)-1) ' take off the last comma
headers = "Return-Path:"&replyto & vbNewLine
headers = headers & "Date: " & GenMIMEDate(Now,"-0600") & vbNewLine
headers = headers & "To:"& sendto & vbNewLine
headers = headers & "From:"& from & vbNewLine
headers = headers & "Subject: "& subject & vbNewLine
headers = headers & "MIME-Version: 1.0" & vbNewLine
headers = headers & "Content-Type: text/html; boundary="""&MIMEBOUNDARY&""""
GenMIMEHeaders = headers & vbNewLine & vbNewLine
response.write ("hello")
response.write mto
end function
'#Againg the Content Type is the Plain text/Html Which Should be Used.
'********* GenMIMEEmail ************
function GenMIMEEmail(byval from, byval mto, byval subject, byval body, 
byval fileattach)
'bodyMIME = "Content-Type: htm;"" charset=""US-ASCII""" & vbNewLine
'bodyMIME = bodyMIME & "Content-transfer-encoding: 7bit" & vbNewLine & 
vbNewLine
fullmail = GenMIMEHeaders(from,from,mto,subject) & "" & MIMEBOUNDARY & 
vbNewLine
'response.write ("hello")
'Reading the File To be Send
dim objfilesys
Dim thetextStream
dim readalltextfile
dim var1
dim var2
set objfilesys = Server.Createobject("Scripting.FileSystemObject")
set thetextStream = 
objFilesys.openTextFile("d:\Inetpub\wwwroot\einfoasia-dotcom\Newsletter_Issue10.htm",1)
readalltextfile =  thetextStream.ReadAll
body = ""
set thetextStream = 
objFilesys.openTextFile("d:\Inetpub\wwwroot\einfoasia-dotcom\Newsletter_Issue10.htm",1)
do until thetextstream.atendofstream
'response.write thetextstream.readline

var1 = thetextstream.Readline & vbNewLine
'response.write var1
'var2 =        Replace(var1,">",">")
'response.write "HI"
Body =  Body + var1
loop
'response.write Body
'response.write Readalltextfile
'thetextStream.readall
thetextstream.close
' --add the body--
'body =  "HI"
fullmail = fullmail & bodyMIME & body & vbNewLine
fullmail = fullmail & "" & MIMEBOUNDARY

' Do we need to attach a file?
'if isEmpty(fileattach) or fileattach="" then ' Nope, no file, close the 
separator
GenMIMEEMail = fullmail & "" & vbNewLine
'else
' theres an attachment
'attach = StripPath(fileattach)
'attachMIME = "Content-Type: html; name="""& attach &"""" & vbNewLine
'attachMIME = attachMIME & "Content-Transfer-Encoding: quoted-printable" & 
vbNewLine
'attachMIME = attachMIME & "Content-disposition: attachment; 
filename="""&attach&""""
'attachMIME = attachMIME & vbNewLine & vbNewLine

'fullmail = fullmail & vbNewLine & "--" & MIMEBOUNDARY & vbNewLine & 
attachMIME
'fullmail = fullmail & ReadAttachment(fileattach) & vbNewLine
'GenMIMEEMail = fullmail & "--" & MIMEBOUNDARY & "--" & vbNewLine
'end if
end function


'So the finished function calls to get the job done look like this:

' in VB and VBScript, we can use the _ to extend to the next line
'email = GenMIMEEmail("jai at einfoasia.com", _
   , _
   '"New Log", _
   '"  Body  ",_
   '"")
'If you need to do Some attachments then Simply Give the Full Path of the 
Location
'Of the File Which needs to be added.
'"d:\Inetpub\wwwroot\einfoasia-web\marketing\aboutus.htm "
' without an attachment:


'The Marketing Profile Table to be used.
'This Part We are Opening the Recordset to send the Emails to the Users
'These Email Records are readfROM the Database that we will be using .
'Retrieve the Email Records to be Used For Sending the Emails
'
'Set EmailRec = Server.CreateObject ("ADODB.Recordset")
     'EmailSQL = "Select

'response.write emailstring
IF Counter  > 4840  then
email = GenMIMEEmail("info at dotcomdirectory.com.sg", _
   "'" & emailstring & "' ", _
   "News Letter Issue10 ", _
   "Body", _
   "")  ' Note we've allowed Empty and "" to signify no file to attach

writeEmail email,GenMimeName
end if
response.write Counter & ("<BR>")
counter = Counter + 1
response.write Counter & ("<BR>")
emailrec.movenext
loop
'end if
'" & emailstring & "'
%>




<%

'To note, the reason I use constructs like

'headers = headers & "To:"& tolist & vbNewLine
'headers = headers & "From:"& from & vbNewLine
'headers = headers & "Subject: "& subject & vbNewLine


%>

</body>
</html>
<!-- #include file="../include/DBClose.inc"  -->
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.





More information about the thelist mailing list