Asp Formmail Output formating

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
%>

DilysAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
markhoyConnect With a Mentor Commented:
<%@ 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
 
prohacxCommented:
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
 
DilysAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
markhoyCommented:
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
 
apolloisCommented:
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
 
DilysAuthor Commented:
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
 
apolloisCommented:
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
 
GaryCommented:
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
All Courses

From novice to tech pro — start learning today.