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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

database contents in email body 1

Status
Not open for further replies.

dcarbone

Programmer
Aug 22, 2003
29
CA
I'm using a pretty common CDONTS script to send email messages with embedded HTML. How would I be able to insert the contents of an access table into the body of an e-mail
 
One way is to create a connection/recordset to your database

***********************************************************
<%
Dim cnDB ' As ADODB.Connection
Dim rsData ' As ADODB.Recordset
Set cnDB = Server.CreateObject(&quot;ADODB.Connection&quot;)
cnDB.ConnectionString = &quot;Provider=SQLOLEDB;&quot; _
& &quot;Data Source=(local);&quot; _
& &quot;Initial Catalog=Northwind;&quot; _
& &quot;User ID=sa;&quot; _
& &quot;Password=;&quot;
cnDB.Open
Set rsData = cnDB.Execute(&quot;SELECT * FROM Customers&quot;)
%>
***********************************************************


then simply populate the fields in the body of the HTML formatted email with the recordset fields you wish





logo.gif width='85' height='30'

bath.gif
 
will this work for multiple records, if i use a loop?
 
then simply populate the fields in the body of the HTML formatted email with the recordset fields you wish

How is this done?
I am attempting to insert some db information into the body of an email, but can only insert it into the Subject line.



James Goodman MCSE, MCDBA
 
jgoodman00,

Here's an example of how it is done:

Code:
Set Message = CreateObject(&quot;CDONTS.NewMail&quot;)

BodyToSend = &quot;<HTML><HEAD>&quot; & _
   &quot;<style>body {font-family:courier;font-size:10pt;}</style>&quot; & _
   &quot;</HEAD><BODY>Dear &quot; & Trim(rsObj(&quot;USERNAME&quot;)) & &quot;,<br><br>&quot; & _
   &quot;Your password is <b>&quot; & Trim(rsObj(&quot;PASSWORD&quot;)) &  &quot;</b>.&quot; & _
   &quot;</BODY></HTML>&quot;

Message.From = &quot;Web Messenger <mywebemail@mydomain.com>&quot;
Message.To = rsObj(&quot;EMAIL&quot;)
Message.Subject = &quot;Your Password&quot;
Message.Body = BodyToSend
Message.Importance = 2  'cdoHigh
Message.BodyFormat = 0  'cdoBodyFormatHTML 
Message.MailFormat = 0  'cdoMailFormatMime 

On Error Resume Next
Message.Send
Set Message = nothing

In this example, a recordset named rsObj is assumed to have USERNAME, PASSWORD and EMAIL fields.
 
dcarbone,

You may have to use array variables to hold those record contents if you want to get information from multiple records. But you can still use an ordinary variable if you just want to concatenate those information.
 
So I have to do all of that just to add something to the body?

Is it not possible to do this in HTML, because I want the client to be able to click on a hyperlink which opens a message?

At the moment I have the following asp which creates the hyperlink I want. The only thing missing is the ability to add data to the body of the message:
<%
objRS.Open &quot;SELECT * FROM tblLUPStaffNames WHERE StaffID = &quot; & Request.Form(&quot;cboFor&quot;)
Response.Write &quot;<BR><a href='mailto:&quot;
Response.Write objRS(&quot;StaffEmail&quot;)
Response.Write &quot;?subject=Automatic Message: &quot; & Now & &quot;'>&quot;
Response.Write &quot;Email Reminder&quot;
Response.Write &quot;</a>&quot;
objRS.Close
%>


I want the email to come from the client user, not from the server.

James Goodman MCSE, MCDBA
 
James,

If you want a message to come from the client user with text in the body, add the &quot;body&quot; parameter to the link:
Code:
<%
objRS.Open &quot;SELECT * FROM tblLUPStaffNames WHERE StaffID = &quot; & Request.Form(&quot;cboFor&quot;)    
Response.Write &quot;<BR><a href='mailto:&quot; 
Response.Write objRS(&quot;StaffEmail&quot;) 
Response.Write &quot;?subject=Automatic Message: &quot; & Now
Code:
Response.Write &quot;&body=&quot; & objRS(&quot;CannedMessage&quot;) & &quot;'>&quot;
Code:
Response.Write &quot;Email Reminder&quot;
Response.Write &quot;</a>&quot;
objRS.Close
%>

That is assuming that the database stores a &quot;CannedMessage&quot; field. Or you can assemble your message in a variable:

Code:
<%
objRS.Open &quot;SELECT * FROM tblLUPStaffNames WHERE StaffID = &quot; & Request.Form(&quot;cboFor&quot;)
Code:
strMessage = &quot;Dear &quot; & objRS(&quot;StaffName&quot;) & vbcrlf & vbcrlf
strMessage = strMessage & &quot;Some kind of text... and you can use the 'vbcrlf' constant to make multiple lines in the message.  You may need to be careful because I think you are limited to max 255 characters, starting from 'mailto' to the end of your message&quot;
Code:
Response.Write &quot;<BR><a href='mailto:&quot; 
Response.Write objRS(&quot;StaffEmail&quot;) 
Response.Write &quot;?subject=Automatic Message: &quot; & Now
Code:
Response.Write &quot;&body=&quot; & strMessage & &quot;'>&quot;
Code:
Response.Write &quot;Email Reminder&quot;
Response.Write &quot;</a>&quot;
objRS.Close
%>

Earnie Eng
 
you're welcome! Thanks for the star :)

Earnie Eng
 
I Thought I would try using a multiple line string in order to include an automatic hyperlink to the relevent item. So, I added the following code:
strMessage = Request.Form(&quot;txtDescription&quot;) & vbCrLf
strMessage = strMessage & &quot;You can view the item here: &quot; & vbCrLf
strMessage = strMessage & &quot; & strItemID


However, this does not place the message text on multiple lines. It is instead lumped together on a single line.

Any ideas?


James Goodman MCSE, MCDBA
 
hm...

I did a google search, and found this:
Code:
%0D%0A
That is the equivalent of the vbCRLF (carraige return / line feed)

I found the hint here -
simply replace all your vbcrlf with %0D%0A and that should do the trick!

I tested it on my own server, and it works fine. Good luck!

Earnie Eng
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top