Advanced CDONTS Techniques
November 13, 2000
Sending Attachments with CDONTS
Let's say you have your registration web page completed and want to send
the user a confirmation email with some standard attachments about rules,
procedures and policies associated with registration at your site. If you
understood CDONTS from the
first article, this task should
not be too challenging. However, we never really discussed sending an
attachment, like a MS Word document, so here we go.
<%
'pretend we've got all sorts of ASP code
'up here to handle the form data
'begin CDONTS code
dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.To = email '(variable we stored the user's email in)
objCDO.From = "someaddy@email.com"
objCDO.Subject = "Confirmation Email"
objCDO.Body = "Thank you for registering."
objCDO.AttachFile ("c:\reg_confirm.doc")
objCDO.Send
'rest of the web page and ASP below.
%>
That is pretty easy, and does not require much explanation. You simply say
"AttachFile" and in parenthesis show where the file is on your web
site's server. Attaching a file from the client's hard drive is much
more complex and a bit out of the scope of this article. You need to deal
with A) Putting a field on the form for the file, B) Getting the file from
the user's hard drive to your web server, and C) Getting the file from the
web server to your inbox. ASP does not support file uploading in forms by
default, so you will need a third party component to get the job done (if
you want to try this yourself, I recommend
EZsite Upload -
if you can figure that out, you'll be able to figure the rest out for
yourself).
Sending HTML with CDONTS
Some readers were not clear about how to send HTML in an email using
CDONTS, perhaps to send a more visually provocative opt-in advertisement or
something like that. Fortunately, we can accomplish this task with ease.
Let's revisit the code above and discuss how.
<%
'pretend we've got all sorts of ASP code
'up here to handle the form data
'begin CDONTS code
dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.To = email '(variable we stored the user's email in)
objCDO.From = "someaddy@email.com"
objCDO.Subject = "Confirmation Email"
objCDO.BodyFormat = 0
objCDO.MailFormat = 0
bodyHTML = "<html><head></head><body>" & _
"<font face=""3"" color=""red"">" & _
"Hello, I'm an a message sent via HTML</font><br><br>" & _
"<img src=""http://www.enfused.com/images/net216.jpg"">" & _
"</body></html>"
objCDO.Body = bodyHTML
objCDO.Send
'rest of the web page and ASP below.
%>
Look carefully at the code above, the first thing you should notice is that
we added objCDO.BodyFormat = 0 and objCDO.MailFormat = 0
. Instead of setting these equal to 1, like we did in the first
article, this time they have been set equal to 0. Those attributes will tell
CDONTS to send the mail as HTML. As for the body of the HTML message, it
is literally an HTML page that you can create any way you like. But there
are a few caveats of which you must be aware. First, assigning HTML code
to a variable requires creative use of quotation marks. Note that all the
HTML code is inside double quotes already, so if you have an attribute that
uses a double quote, how can one enter it without terminating the original
quote? Simply use two quotes like I did above. That will indicate to the
server that you want a quote actually embedded within the variable, not that
you are terminating the string. If you forget to do this for attributes
with quotes, you will get errors, so be careful.
Second, take a look at how we stored all the HTML in one variable. You
will recall seeing this technique before when we discussed SQL, at the end
of the line if you add & _ you can then continue the
string on the next line without worrying about writing "bodyHTML =
bodyHTML +" every line. Third, note that the image embedded in this email
is pointing to an HTTP location. If you think about what we are doing
carefully, the reasoning behind that should make sense. The user is not
going to have a copy of the image on their hard drive, so when they get the
email it will have to have a source for the image. So, point them out to
your public web server. There is another way to do this, which is less
taxing on your web server - and that is to attach the image to the email.
Here is how you do that:
'these two lines of code will replace
'what was done above with the image.
'attach the image to the email and
'give it a name for use later on
objCDO.AttachURL "c:\images\net216.gif", "net216.gif"
'...
'inside the bodyHTML variable
bodyHTML = "<html>etc, etc..." & _
"<img src=""net216.gif""><br><br>"
'notice how the name you assigned above
'is referenced as the image source?
Once you have all your HTML code squared away, set objCDO.body equal to your
variable (bodyHTML) and send away! Move on to the next page to read about
using the data we stored in the registration database last month.
Forms: Saving State Revisited
Using ASP for Form Handling: Part 4 - Filling the Gaps
Using the Registration Database
|