?
Solved

Asp Formmail Output formating

Posted on 2003-03-24
8
Medium Priority
?
304 Views
Last Modified: 2008-02-01
I have an html web form when data is entered into this form by a user the information retrieved from the form is sent to me via email using an ASP form mail.
Now my question is i want the content of the email to be formated so that is can be easier to read how do i go about this.
Below is the form mailer

What i am looking to achive is put in some bold and colors.


<%@ Language=VBScript %>
<% Option Explicit %>
<%
    'The header/footer for the email.
    Const strHeader = "Here are the results of the form:"
    Const strFooter = "Custumer Request Form "

    'email will be going to
    Const strTo = "dilys@hercompany.com"
     
     
    'This information is optional
    Dim strFrom, strSubject, strRedirectURL, strFromPath

    'strFrom = Request.Form("txtSendToEmailAddress")
     strFrom = " Request for new acct "
    if Len(strFrom) = 0 then strFrom = strTo

    'strSubject = Request.Form("txtEmailSubject")
     strSubject = " A new request has been made "
    if Len(strSubject) = 0 then strSubject = ""

    strRedirectURL = "http://www.dilys.com/Sales/htm/thanks.htm"
    if Len(strRedirectURL) = 0 then strRedirectURL = "/"

    'strFromPath = Request.Form("urlFromPath")
    'if Len(strFromPath) = 0 then strFromPath = "UNKNOWN"

    Dim strBody
    strBody = strHeader & ( vbCrLf & vbCrLf )
    strBody = strBody & ( "Customer request form information: " & strFromPath & vbCrLf ) & _
          ( "FORM submitted at " & Now() & vbCrLf & vbCrLf )

    dim ix, formElementName, formElementValue, prefix, fldName
    For ix = 1 to Request.Form.Count
        formElementName = Request.Form.Key(ix)
        formElementValue = Request.Form.Item(ix)

        ' what type of field was that on the form?
        prefix = Left(formElementName,3)
       
        ' and throw away prefix to get actual field name
        fldName = Mid(formElementName,4)
       
        ' but change periods to spaces for readability
        fldName = Replace(fldName, "."," ")

        Select Case prefix
            ' if the prefix indicates this is a form field of interest...
            Case "txt","sel","rad","cbo","lst","chk":
                ' if user didn't answer this question, say so...
                if Len(formElementValue) = 0 then formElementValue = "unansewered"
               
                ' then tack on the name of the field and the answer
                strBody = strBody & (fldName & ": " & formElementValue & vbCrLf)
        End Select
    Next
         
    strBody = strBody & ( vbCrLf & vbCrlf & strFooter )

'Time to send the email
     Dim objNewMail
     Set objNewMail = Server.CreateObject("CDONTS.NewMail")
      objNewMail.From = "CustReq@dilys.com.com"
     objNewMail.To = strTo
     objNewMail.cc = "CSCONTACT@dilyscomp.com"
     objNewMail.Subject = strSubject
      objNewMail.Body = strBody
      objNewMail.Send
      Set objNewMail = Nothing
     
     'redirect to thank you page then home page
    Response.Redirect strRedirectURL
%>

0
Comment
Question by:Dilys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 5

Expert Comment

by:prohacx
ID: 8196183
Hi there Dilys!

What do you mean by "formatted"?

If you mean look nicer, you can send yourself a HTML mail, so you can format your stuff using tables and styles like this:

<%@ Language=VBScript %>
<%option explicit%>
<%response.Expires=-1%>
<%

dim myMail, strHTML

set myMail = server.CreateObject("CDONTS.NewMail")

myMail.BodyFormat = 0
myMail.MailFormat = 0

myMail.From = "sender@senderdomain.com"
myMail.To = "dilys@hercompany.com"
myMail.Subject = "HTML mail"

strHTML = "<html><head></head><body>"
strHTML = strHTML & "And yes, this is a <b>HTML</b>-mail!<hr><br>"
strHTML = strHTML & "<img src=""a.jpg""><hr>"
strHTML = strHTML & "<h3>Even <u>with</u> a picture!</h3>"
strHTML = strHTML & "</body></html>"

myMail.Body = strHTML
myMail.AttachURL "c:\temp\a.jpg", "a.jpg"

myMail.Send

set myMail = nothing

Response.Write "Mail was sent!"

%>

You can even use images like I did above.

Hope this helps you any further!
0
 

Author Comment

by:Dilys
ID: 8196508
Thanks but this doesn’t help because the values are all dynamically generated so I cannot apply your code. However if the content of the email was static then it’s easy to give the form a better look. All I am trying to do is make the form field in bold and leave the values

The line below is where I need to massage but have no idea how to go about it

strBody = strBody & (fldName & ": " & formElementValue & vbCrLf)
0
 
LVL 7

Expert Comment

by:markhoy
ID: 8196959
You can easily use HTML formatting on dynamic values (see www.aspfaqs.com > Email > HTML for more info).

On your example:


Select Case prefix
           ' if the prefix indicates this is a form field of interest...
           Case "txt","sel","rad","cbo","lst","chk":
               ' if user didn't answer this question, say so...
               if Len(formElementValue) = 0 then formElementValue = "unansewered"
               
               ' then tack on the name of the field and the answer
               strBody = strBody & "<B>"(fldName & "</b>: " & formElementValue & vbCrLf)
       End Select


to make the NAME bold. Now make the HTML email:

dim myMail, strHTML

set myMail = server.CreateObject("CDONTS.NewMail")

myMail.BodyFormat = 0
myMail.MailFormat = 0

myMail.From = "sender@senderdomain.com"
myMail.To = "dilys@hercompany.com"
myMail.Subject = "HTML mail"

strHTML = "<html><head></head><body>"
strHTML = strHTML & strBody

strHTML = strHTML & "</body></html>"

myMail.Body = strHTML

myMail.Send

set myMail = nothing

Is that what you need? HTH

Mark
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:apollois
ID: 8197510
Hi Dilys,

You can create an HTML file with the exact formatting you want.  This will be your email body template  Wherever you have data values, simiply use something like "@CompanyName@"

Then, in your ASP where you process the form, read the HTML template using FSO, and do a replace of all variables:

strHTML = ReadFile("YourTemplateName.htm")     'Must be full path to file

strHTML = Replace(strHTML, "@CompanyName@", Request.form("CompanyName"))

You could even automate this:

FOR Each strFieldName in Request.form
     strHTML = Replace(strHTML, "@" & strFieldName & "@", Request.form(strFieldName))
NEXT


======= PUT THIS FUNCTION AT BOTTOM OF  PAGE AFTER </HTML> TAG ===
<%
FUNCTION ReadFile(pstrFileName)

     CONST ForReading = 1
     
     Dim objFso           'As FileSystemObject  
     Dim objFile      'As File
     Dim objTs           'As TextStream
     Dim strText
     
     Set objFso      = CreateObject("Scripting.FileSystemObject")
     Set objFile = objFso.GetFile(pstrFileName)
     Set objTs      = objFile.OpenAsTextStream(ForReading)
     
     strText = objTs.ReadAll

     Set objFso = Nothing
     Set objFile = Nothing
     Set objTs = Nothing

     ReadFile = strText

END FUNCTION
%>

Best Regards,
>apollois<
0
 

Author Comment

by:Dilys
ID: 8206373
The html formating wraps the message and i do not want that
i want to have something like this

Name:   Paul Smith
Address:  89 Help Street

all i want is name and address to be in bold i do not want the information wrapped . In addition to this  my data is not static the reason i am having this problem can someone please help.

The FSO I do not understand it.
0
 
LVL 7

Accepted Solution

by:
markhoy earned 1000 total points
ID: 8206411
<%@ Language=VBScript %>
<% Option Explicit %>
<%
   'The header/footer for the email.
   Const strHeader = "Here are the results of the form:"
   Const strFooter = "Custumer Request Form "

   'email will be going to
   Const strTo = "dilys@hercompany.com"
   
   
   'This information is optional
   Dim strFrom, strSubject, strRedirectURL, strFromPath

   'strFrom = Request.Form("txtSendToEmailAddress")
    strFrom = " Request for new acct "
   if Len(strFrom) = 0 then strFrom = strTo

   'strSubject = Request.Form("txtEmailSubject")
    strSubject = " A new request has been made "
   if Len(strSubject) = 0 then strSubject = ""

   strRedirectURL = "http://www.dilys.com/Sales/htm/thanks.htm"
   if Len(strRedirectURL) = 0 then strRedirectURL = "/"

   'strFromPath = Request.Form("urlFromPath")
   'if Len(strFromPath) = 0 then strFromPath = "UNKNOWN"

   Dim strBody
   strBody = strHeader & ( vbCrLf & vbCrLf )
   strBody = strBody & ( "Customer request form information: " & strFromPath & vbCrLf ) & _
         ( "FORM submitted at " & Now() & vbCrLf & vbCrLf )

   dim ix, formElementName, formElementValue, prefix, fldName
   For ix = 1 to Request.Form.Count
       formElementName = Request.Form.Key(ix)
       formElementValue = Request.Form.Item(ix)

       ' what type of field was that on the form?
       prefix = Left(formElementName,3)
       
       ' and throw away prefix to get actual field name
       fldName = Mid(formElementName,4)
       
       ' but change periods to spaces for readability
       fldName = Replace(fldName, "."," ")

       Select Case prefix
           ' if the prefix indicates this is a form field of interest...
           Case "txt","sel","rad","cbo","lst","chk":
               ' if user didn't answer this question, say so...
               if Len(formElementValue) = 0 then formElementValue = "unansewered"
               
               ' then tack on the name of the field and the answer
               strBody = strBody & "<B>" & (fldName & "</b>: " & formElementValue & vbCrLf)
       End Select
   Next
       
   strBody = strBody & ( vbCrLf & vbCrlf & strFooter )

strHTML = "<html><head></head><body>"
strHTML = strHTML & strBody

strHTML = strHTML & "</body></html>"
strBody = strHTML


'Time to send the email
    Dim objNewMail
    Set objNewMail = Server.CreateObject("CDONTS.NewMail")
myMail.BodyFormat = 0
myMail.MailFormat = 0


     objNewMail.From = "CustReq@dilys.com.com"
    objNewMail.To = strTo
    objNewMail.cc = "CSCONTACT@dilyscomp.com"
    objNewMail.Subject = strSubject



     objNewMail.Body = strBody
     objNewMail.Send
     Set objNewMail = Nothing
   
    'redirect to thank you page then home page
   Response.Redirect strRedirectURL
%>

0
 
LVL 10

Expert Comment

by:apollois
ID: 8206517
Dilys,

>>>The html formating wraps the message and i do not want that

Whatever you want to NOT word-wrap, put in <PRE> tags like this.

<PRE>
This line will NOT word wrap
</PRE>

>>> all i want is name and address to be in bold i do not want the information wrapped <<<

<PRE>
Name:   <b>Paul Smith</b><BR>
Address:  <b>89 Help Street</b><BR>
</PRE>

>>> The FSO I do not understand it. <<<

What do you not understand?

References and Tutorials for FileSystemObject (FSO)

FileSystemObject Ref Library
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/FSOoriFileSystemObject.asp

FSO TextStream Object Ref
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsobjtextstream.asp?frame=true

FileSystemObject: Reading a file
http://www.asptutorial.info/sscript/Readingfile.asp

ASP FileSystemObject Object
http://www.w3schools.com/asp/asp_ref_filesystem.asp

What is the FileSystemObject The FileSystemObject, or FSO
http://www.4guysfromrolla.com/webtech/faq/FileSystemObject/faq1.shtml


Iterating through the FileSystemObject Collections The FileSystemObject contains several collections
http://www.4guysfromrolla.com/webtech/faq/FileSystemObject/faq5.shtml

Using the FileSystemObject to Write to Text Files
http://www.4guysfromrolla.com/webtech/faq/FileSystemObject/faq3.shtml

Using the FileSystemObject to Read Text Files
http://www.4guysfromrolla.com/webtech/faq/FileSystemObject/faq2.shtml

Copying, Moving, and Deleting Files
http://www.4guysfromrolla.com/webtech/faq/FileSystemObject/faq4.shtml

Feel free to post related follow-up questions.

Best Regards,
>apollois<
0
 
LVL 58

Expert Comment

by:Gary
ID: 9313048
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer by markhoy

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question