Google Answers Logo
View Question
 
Q: How to combine scripting with C# to allow users to customize email templates ( No Answer,   3 Comments )
Question  
Subject: How to combine scripting with C# to allow users to customize email templates
Category: Computers > Programming
Asked by: wombatfish-ga
List Price: $100.00
Posted: 16 Apr 2003 15:59 PDT
Expires: 16 May 2003 15:59 PDT
Question ID: 191448
I need to develop a C# program that reads an ASCII file that consists
of HTML code and send it out as an email to a mailing list.
The tough part for me is that the ASCII file must also support some
form of procedural logic that lets a user control the resulting
content.
The procedural code should be simple for a "super-user" to set up
(like VBScript or VB.NET).
In addition the user can select and embed pre-defined tags (wrapped
in, say, { and } characters) that my program will replace with values
from a database:
(e.g. {Country}, {Surname}, {Title}, etc.)

For example, using an ASP.NET metaphor, a user could set up a template
that looks like this:
<!---- start of template -->
<HTML>
<BODY>
Dear {Title} {Surname},<br><br>
<% if "{Country}" = "USA" then %>
   <H1>Special offer to our USA subscribers only"</H1>
   If you renew your subscription for only $100 , etc., etc.
<% else %>
   <H1>Get fast international renewal deliveries today</H1>
   If you renew your subscription for only $200 , etc., etc.
<% end if %>
</BODY>
</HTML>
<!---- end of template -->

The C# program (running as a Windows service) reads a list of names
and addresses from a database and sends an HTML email out to each
subscriber based on the template design. In this example, for each
name in the database, the {Country}, {Surname} and {Title} tags would
first be replaced with the person's live data by the C# program; the
"revised" template would be passed to the scripting engine which would
"interpret" it, "generate" the HTML content, and pass the "generated"
content back to the C# program, which would then send that content as
the body of an email; continuing the same procedure with the next
record, etc.

I don't want to rely on COM technology like Windows Scripting for
performance reasons.
1) Is there a .NET version of Windows Scripting ?
2) How is it invoked from C# (and passed the template) ?
3) How do I pass the generated HTML content that was created by the
scripting engine back to the C# program ?

Request for Question Clarification by mathtalk-ga on 16 Apr 2003 18:45 PDT
Hi, wombatfish-ga:

If I understand your request, you want to write a Windows service in
C# that performs a "mail-merge" operation on an HTML template,
producing individualized HTML documents that you would then send as
email items.

Besides simple substitution on tags, you also want the ability to
evaluate "if...then" conditional logic using these tags.  Such
capabilities are provided (for formatted text output) by Crystal
Reports and MS Word.  You want to create a stand-alone executable to
do this with HTML output, which can in turn run as a service.

Have I missed anything?  Is there something intrinsic about using the
scripting engine, other than the evaluation of the if...then
conditionals?

regards, mathtalk-ga

Clarification of Question by wombatfish-ga on 17 Apr 2003 01:20 PDT
A couple of years ago, I wrote a similar program in Delphi using the
Windows scripting engine. I found a freeware Delphi component on the
net that called the engine and retrieved the generated code. I was
hoping something similar was possible in C#...

The advantage of using the VBScript scripting engine was that you
could define variables and leverage functions in the template.

A good example was a template in which a "power-user" dynamically
built a URL from the data he was given.

e.g.
<%
Function CheckDigit(foobar1, foobar2, ED)
' using the three parameters, calculate and return a checkdigit to
prevent tampering with expiry date or other parameters..
End Function

Dim ED ' expiry date could change over time, so it's best kept in the
template
ED = "20030109"
%>
<HTML>
<BODY>
' foobar1 and foobar2 are price specific data

<a href=http:///www.foobar.com/orderform.asp?f1={foobar1}&f2={foobar2}&ED=<%=ED%>&checkdigit=<%=CheckDigit("{foobar1}",
"{foobar2}", ED)%>>Click here to order</a>

etc.

orderform.asp compared the checkdigit with the 3 params and blocked
tampered access...

I don't think that XML/XSLT is flexible enough, or quite so intuitive
for a user to grasp.
Answer  
There is no answer at this time.

Comments  
Subject: Re: How to combine scripting with C# to allow users to customize email templates
From: mathtalk-ga on 16 Apr 2003 18:48 PDT
 
If this were something I needed to do, I think I'd consider creating
the "templates" as XSLT transforms rather than HTML with embedded
scripting.  I'd put the source data from the "mail merge" database
into XML files.  The XSLT transform can than "act" on the XML source
data to produce HTML (or XHTML) outputs.

regards, mathtalk-ga
Subject: Re: How to combine scripting with C# to allow users to customize email templates
From: j_philipp-ga on 16 Apr 2003 20:54 PDT
 
Can you evaluate the expressions via a command?
E.g. in ASP VBScript/ JScript one could use

   userInput = "1 + 1 + 2 - 3"
   result = eval(userInput)

I don't know if this is still available in C#, or if this is what you
said you want to exclude for performance reasons. If you do use XML,
you could work it out like this:

<template:check condition="{Country} = 'USA'">
  <template:is-true>
    <p>Hello USA</p>
  </template:is-true>
  <template:is-false>
    <p>Hello World</p>
  </template:is-false>
</template:check>

As you can see, a different name-space for your template-language
would be needed to differentiate the two languages. However, this
would still require for the super-user to write appropriate XML syntax
for the HTML. So you can see this is _not_ easy to write for the
reasons of XML. One the other hand, it would be very easy to parse for
you, since you have the XML DOM and also the Doctype-Validation.

If I'd do it I probably would just parse the thing myself but exclude
the boolean expressions and pass them to the scripting engine's
eval-method. Your example is quite intuitive to write as it resembles
common ASP syntax:

<% if "{Country}" = "USA" then %> 
   <H1>Special offer to our USA subscribers only"</H1> 
   If you renew your subscription for only $100 , etc., etc. 
<% else %> 
   <H1>Get fast international renewal deliveries today</H1> 
   If you renew your subscription for only $200 , etc., etc. 
<% end if %> 

Do you intend to hand-code the parser for that syntax?
Which features besides if-else conditions do you want to include (e.g.
loops, select-case, and so on)?
Subject: Re: How to combine scripting with C# to allow users to customize email templates
From: spikecura-ga on 23 Apr 2003 16:47 PDT
 
1) Is there a .NET version of Windows Scripting ? 

Yes, it's called VSA.

2) How is it invoked from C# (and passed the template) ? 

See this: http://www.csharphelp.com/archives/archive102.html

You will have to create a class to hold the template, and pass a
created instance of the object to the engine.

3) How do I pass the generated HTML content that was created by the
scripting engine back to the C# program ?

Your class can have a variable to hold the HTML content, and your
script should use this variable to return the HTML content.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy