Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve the output of an asp page in a string for e-mail body 3

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
CA
Hello,
I have a website that is written with ASP 3. When a member registers on the site, I want to send them an e-mail containing data that pertains to that specific member. So I want to create a template e-mail body.

In *EVERY* example that I can find on sending e-mail with CDO for Win 2000, they concatenate a string for the HTMLBody property.

i.e.
Code:
Dim strHTMLBody

strHTMLBody = &quot;<HTML>&quot;
strHTMLBody = strHTMLBody & &quot;<HTML>&quot;
strHTMLBody = strHTMLBody & &quot;<HEAD>&quot;
strHTMLBody = strHTMLBody & &quot;</HEAD>&quot;
strHTMLBody = strHTMLBody & &quot;<BODY>&quot;
strHTMLBody = strHTMLBody & &quot;</BODY>&quot;
strHTMLBody = strHTMLBody & &quot;</HTML>&quot;

objCDO.HTMLBody = strHTMLBody

This is hard to maintain because it is hard-coded. I can't just whip open FrontPage and make a quick change. Also, the code is not reusable between multiple pages. Anyway...

I would prefer to store the contents of the e-mail in another ASP page and then use ASP's wonderful ability to dynamically generate the output and attach it to the e-mail body.

i.e. a new page called test.asp:
Code:
<HTML>
<HEAD>
</HEAD>
<BODY>
 Welcome to my website, <%=Request.QueryString(&quot;YourName&quot;)%>.
 This is my e-mail body!
</BODY>
</HTML>

This is great for maintenance... i.e. it's easier to modify and better fits the code-reusability rule. CDO supports the
Code:
CreateMHTMLBody
function which loads an external page into the body. So I'm set... right?

Nope! CreateMHTMLBody works in Visual Basic, but when I use this function in ASP, the page hangs and eventually times out. Also, when tested in VB (because it's the only place where the code didn't hang) the query string data was ignored.

i.e.
Code:
Dim m 
set m = new CDO.Message
m.CreateMHTMLBody &quot;[URL unfurl="true"]http://localhost/test.asp?YourName=bob&quot;[/URL]
debug.print m.HTMLBody
Set m = Nothing

would print:

Code:
Welcome to my website, .
This is my e-mail body!

So how can I store the e-mail body in another page, while still keeping it a template, and not concatenating strings, and using CDO for Win 2000.... as my XP Pro box doesn't seem to have the CDONTS library?
Thanks for any input,
The Insider
 
You could use the XMLHTTP object to query your server for the other page. This will return the contents of the page into the XMLHTTP object and can be retrieved using it's .ResponseText method.

Code:
<%
Response.Buffer = True
Dim objXMLHTTP, URL

' Create an xmlhttp object:
Set objXMLHTTP = Server.CreateObject(&quot;Microsoft.XMLHTTP&quot;)

URL = &quot;[URL unfurl="true"]http://www.yoursite.com/pagename.asp&quot;[/URL]

' Opens the connection to the remote server.
objXMLHTTP.Open &quot;GET&quot;, URL, False

' Actually Sends the request and returns the data:
objXMLHTTP.Send

Response.Write objXMLHTTP.ResponseText
%>

You could then expand this basic piece of code by putting the fields that need to be generated inside the QueryString of the address. So if you wanted to use the persons name and physical address in the body of the page:
Code:
<%
Response.Buffer = True
Dim objXMLHTTP, URL

' Create an xmlhttp object:
Set objXMLHTTP = Server.CreateObject(&quot;Microsoft.XMLHTTP&quot;)

URL = &quot;[URL unfurl="true"]http://www.yoursite.com/pagename.asp?name=&quot;&nameVariable&&quot;&street=&quot;&streetVar&&quot;&city=&quot;&cityVar&&quot;&state=&quot;&stateVar&&quot;zipcode=&quot;&zipcode[/URL]

' Opens the connection to the remote server.
objXMLHTTP.Open &quot;GET&quot;, URL, False

' Actually Sends the request and returns the data:
objXMLHTTP.Send

Mail.body = objXMLHTTP.ResponseText
%>

In your file holding the email you could pull the values out of the querystring (Request.QueryString) in order to fill them in at the appropriate places in your email body page. You then assign the pafge that is returned to your xmlhttp object to be the body of your email, and voila! :)

Hope this helps,
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Brilliant! I'll give it a try!
Thanks,
The Insider
 
Glad to be of help, Let us know how it works out for you :)
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top