sort mailbody

This is a discussion on "sort mailbody" within the Classic ASP section. This forum, and the thread "sort mailbody are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Classic ASP

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 17th, 2003, 14:44
New Member
Join Date: Dec 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sort mailbody

Hi

i have a form which is send via a formhandler. unfortunately the field in the mail aren't sortetd at all...

i saw a post in this forum where the solution to sort a mail is to put the fields in an array and then sort it. but there you have to name all the fields. As our formhandler serves for different forms, i can't name the fields...

can anybody tell me how i could put the fields in an array and sort them when i don't know how many fields i have and also don't know what the fieldnames are?

our script is as follows:



...



<%

Function fctSendMail(strFrom, strTo, strSubject, strBody, strError)

strOS = Request.ServerVariables("SERVER_SOFTWARE")
strOSVersion = Mid(strOS, InStrRev(strOS,"/")+1, 1)

If strOSVersion >= 5 Then
' on Windows 2000 and higher we can use CDOSYS

Set objMessage = CreateObject("CDO.Message")
Set objConfig = CreateObject("CDO.Configuration")

If Application("parMailServer") = "localhost" Then
objConfig.Fields(cdoSendUsingMethod) = cdoSendUsingPickup
Else
objConfig.Fields(cdoSendUsingMethod) = cdoSendUsingPort
objConfig.Fields(cdoSMTPServer) = Application("parMailServer")
objConfig.Fields(cdoSMTPServerPort) = 25
objConfig.Fields(cdoSMTPAuthenticate) = cdoAnonymous
End If

objConfig.Fields.Update

' Bind configuration to message
Set objMessage.Configuration = objConfig

objMessage.From = strFrom
objMessage.Sender = strFrom
objMessage.To = strTo
objMessage.Subject = strSubject
objMessage.TextBody = strBody

On Error Resume Next

objMessage.Send

If Err.Number = 0 Then
fctSendMail = True
Else
fctSendMail = False
strError = Err.Description
Err.Number = 0
End If
On Error Goto 0

Else

' on NT4 we have to use CDONTS. No error handling is provided
Set objMail = CreateObject("CDONTS.NewMail")
With objMail
.From = strFrom
.To = strTo
.Subject = strSubject
.Body = strBody
.Send
End With
Set objMail = Nothing

End If

End Function

strFormErrorMail = ""
strFormErrorScreen = ""
strBodyMail = ""
strBodyScreen = ""
strMailError = ""
strSubject = Request.Form("Subject")

' First make sure all parameters needed for mail sending have been passed

If Request.Form("To") & " " = " " Then
strFormErrorMail = strFormErrorMail & "- " & Application("txtFormErrorNoRecipient" & conCurLang) & vbCrLf
strFormErrorScreen = strFormErrorScreen & "- " & Application("txtFormErrorNoRecipient" & conCurLang) & "
" & vbCrLf
End If

If Request.Form("From") & " " = " " Then
strFormErrorMail = strFormErrorMail & "- " & Application("txtFormErrorNoSender" & conCurLang) & vbCrLf
strFormErrorScreen = strFormErrorScreen & "- " & Application("txtFormErrorNoSender" & conCurLang) & "
" & vbCrLf
End If

If strSubject & " " = " " Then
strSubject = Application("txtFormDefaultSubject" & conCurLang) & " " & Request.ServerVariables("HTTP_REFERER")
End If

If strFormErrorMail = "" Then ' We have all necessary information to try sending the form contents by e-mail

For Each Field in Request.Form
strBodyScreen = strBodyScreen & Field & ": " & Request.Form(Field) & "
" & vbCrLf
strBodyMail = strBodyMail & Field & ": " & Request.Form(Field) & vbCrLf
Next

If fctSendMail(Request.Form("From"), Request.Form("To"), strSubject, strBodyMail, strMailError) Then
Response.Write "

" & Application("txtFormSuccessMessage" & conCurLang) & " " & Request.Form("To") & ":</p>"
Response.Write "

" & strBodyScreen & "</p>"
Else
Response.Write "

" & Application("txtFormErrorSending" & conCurLang) & ":</p>"
Response.Write "

" & strMailError & "</p>"
' Send notification to head webmaster
strSubject = Application("txtFormErrorMessage" & conCurLang) & " " & Request.ServerVariables("HTTP_REFERER")
strBodyMail = Application("txtFormErrorSending" & conCurLang) & ": " & vbCrLf & strMailError
If fctSendMail(Application("parWebmasterEmail"), Application("parWebmasterEmail"), strSubject, strBodyMail, strMailError) Then
Response.Write "

" & Application("txtFormErrorSentToWebmaster" & conCurLang) & "</p>"
End If
End If

Else ' Some parameters were missing, notify the user on-screen and the Webmaster by email

Response.Write "

" & Application("txtFormErrorMessage" & conCurLang) & " " & Request.ServerVariables("HTTP_REFERER") & ":

"
Response.Write strFormErrorScreen

' Send notification to head webmaster
strSubject = Application("txtFormErrorMessage" & conCurLang) & " " & Request.ServerVariables("HTTP_REFERER")
strBodyMail = strFormErrorMail & vbCrLf & Trim(Application("txtContentmasterText" & conCurLang)) & ": " & Request.Form("Contentmaster")
If fctSendMail(Application("parWebmasterEmail"), Application("parWebmasterEmail"), strSubject, strBodyMail, strMailError) Then
Response.Write "

" & Application("txtFormErrorSentToWebmaster" & conCurLang) & "</p>"
End If

End If

%>

thanx in advance!

  #2 (permalink)  
Old Dec 17th, 2003, 14:55
New Member
Join Date: Dec 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
... i guess i would have to put this in an array..

For Each Field in Request.Form
strBodyScreen = strBodyScreen & Field & ": " & Request.Form(Field) & "
" & vbCrLf
strBodyMail = strBodyMail & Field & ": " & Request.Form(Field) & vbCrLf
Next
  #3 (permalink)  
Old Dec 17th, 2003, 16:24
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
It looks like you're pretty familiar with ASP so I'll limit my reply to the concept, but if you need help with the code just ask.

You'll want to add a number to the beginning of each field name that indicates what order the items should show up in the results, make sure you use a leading zero if there's more than 10, for example:
01FirstName
02LastName
04State
03City
...
17Zipcode

Then in your form handler you'd use one of the many alphabetizing/sorting routines that you can find on the web along with the For Each loop to sort the form data. This will put it in the order you specify with your numbering.

Finally, you'll just need to strip off the beginning numbers of the fieldname when you output them using something like Fieldname=right(Field,len(Field)-2)

The tricky bit is the alphabetizing as you'd need to move the form data into arrays before you can order them. I'll put some code together for this and post it shortly just in case you or someone else needs it.
  #4 (permalink)  
Old Dec 17th, 2003, 16:51
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
OK, here's the code. Of course you'll want to modify it to build the body string for your email rather than write out the values to the page. I included some HTML to demonstrate the code.
Code: Select all
<html>
<head>
	<title></title>
</head>
<body>
<form action="" method="post">
<input type="text" name="01test" value="1">

<input type="text" name="03test" value="3">

<input type="text" name="06test" value="6">

<input type="text" name="04test" value="4">

<input type="text" name="02test" value="2">

<input type="text" name="05test" value="5">

<input type="submit">
</form>
<% 
if request.form.count>0 then

	formcount = request.form.count
	redim aFieldName(formcount)
	redim aFieldValue(formcount)
	
	' load the form data into arrays
	i=0
	for each item in request.form
		aFieldName(i) = item
		aFieldValue(i) = request.form(item)
		i = i + 1
	next
	
	' sort the arrays
	For iTemp = 0 To formcount-1
		For jTemp = 0 To iTemp  
		
		If strComp(aFieldName(jTemp), aFieldName(iTemp)) > 0 Then
			strTemp = aFieldName(jTemp) 
			aFieldName(jTemp) = aFieldName(iTemp) 
			aFieldName(iTemp) = strTemp
			strTemp = aFieldValue(jTemp) 
			aFieldValue(jTemp) = aFieldValue(iTemp) 
			aFieldValue(iTemp) = strTemp 
		End If 
		
		Next 
	Next 

	' display the data
	for i = 0 to formcount-1
		response.write(right(aFieldName(i),len(aFieldName(i))-2) & " = " & aFieldValue(i) & "
")
	next
	
end if
 %>

</body>
</html>
  #5 (permalink)  
Old Dec 18th, 2003, 07:34
New Member
Join Date: Dec 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thanx a lot!! it worked!
..actually, I hade to name my fields '_01...', 'cause without the '_' i got an error (maybe because I validate some of my fields?)
but nevertheless the fields are now sortet in my Mail!!

ehm.. I got another question... I have the hidden fields 'From', 'To', 'Subject' and 'Title' and the Submit-Button which also appear in the Mailbody.. like:

From: xy@mail.com

Senden: Senden

Subject: Mitarbeiterumfrage 2003

Title: Besten Dank

To: xy@mail.com

_01Anstellungsbedingungen: 1

_02Infrastruktur: 2

_03Arbeitsabläufe: 3

_04Arbeitsklima: 4

_05InfoPersonalpolitik: 56

_06Sonstiges: 7

_07Geschlecht: weiblich

_08Alter: bis 19

_09Führungsposition: ja

_10Arbeitsbereich: Innendienst

_11Arbeitsplatz: RD

_12Name:

_13Geschäftsstelle: .


is there a way to eliminate them from the mailbody?? (i can't eliminate from!)

thanx again in advance for any advice!!
  #6 (permalink)  
Old Dec 18th, 2003, 10:06
New Member
Join Date: Dec 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
ok... i got a workaround...

as i know, that I only want the fields beginning with a _ in my mail, i just do the following:
' display the data
for i = 0 to formcount-1
if left(aFieldName(i),1) = "_" then
strBodyScreen = strBodyScreen & right(aFieldName(i),len(aFieldName(i))-3)& ": " & aFieldValue(i) & "
" & vbCrLf
strBodyMail = strBodyMail & right(aFieldName(i),len(aFieldName(i))-3)& ": " & aFieldValue(i) & vbCrLf
end if
next
  #7 (permalink)  
Old Dec 18th, 2003, 15:43
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
Yep, that would have been my suggestion.
  #8 (permalink)  
Old Jan 6th, 2004, 12:26
New Member
Join Date: Dec 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
my form now works fine...but some users asked me, if I could give a structure to the mailbody...
like now, the body looks like:
NAME: Sabine
MAIL: xy@mail.com
SAMPLETEXT: this is a test... a.s.o.

..but the users want something that looks more like a table:
NAME: sabine
MAIL: xy@mail.com
SAMPLETEXT: this is a test... a.s.o.

any advice??
  #9 (permalink)  
Old Jan 6th, 2004, 15:19
Highly Reputable Member
Join Date: Jul 2003
Location: Ipswich, UK
Posts: 690
Thanks: 0
Thanked 0 Times in 0 Posts
send the email in html format instead, then you can use tables for formatting.
Closed Thread

Tags
sort, mailbody

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort a three dimensional array?? nate2099 PHP Forum 3 Aug 1st, 2007 16:21
Some sort of database in flash? Throntel Flash & Multimedia Forum 3 Jan 6th, 2007 05:54
trying to sort out a disjointed rollover problem seanollett JavaScript Forum 0 Apr 23rd, 2006 08:25
filter xml and sort xml alcove Other Programming Languages 1 Apr 17th, 2006 03:01
New Member needs help to sort out website! nico7_uk Introduce Yourself 1 Jun 29th, 2005 09:17


All times are GMT. The time now is 07:51.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43