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

email via server

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I've created several data access pages that run client side. However, I need to email users. I can't email client side because my users are using didn't version of Windows (95,98,XP,2000) and different versions of Office. Consequently, some have CDONTS installed and other CDO and some neither. Therefore, I need to send the email via the server. I used Remote Scripting to do this (which worked great). But now our IT dept is installing the runtime version of Sun's Java. This causes a proble with remote scripting. So I discovered XMLHttp. The following code works, but the asp page strips out all of the spaces in the data sent to it.

How do I code it to so that the asp page reads the data as sent (spaces intact)?

Client Side Code
Code:
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title> Email Test</title>

<script language="VBScript" >

'*************************
'*  Setup email to send  *
'*************************

Function SetupEmail()

	MMC_Startup "Live",""
	msgbox "here i am"
	strFrom = "MyEmailAddress"
	strTo = "MyEmailAddress"
	strCC = vbnullstring
	strBC = vbnullstring
	strSubject = "Subject"
	strBody = "This is a test"
	strAttachFile = vbnullstring
	strBodyFormat = vbnullstring
	strMailFormat = vbnullstring

	strEmailValue = "From=" & strFrom & "&To=" & strTo & "&Subject=" & strSubject & "&Body=" & strBody
	msgbox PostEmail( strEmailValue)

End Function

'*************************************
'*  Post email via send to asp page  *
'*************************************

Function PostEmail(ByVal strEmailValues)

    Dim sLink, xmlHttp
 
    sLink = "[URL unfurl="true"]http://mypath/SendEmail.asp"[/URL]
    set xmlHttp = CreateObject("Msxml2.XMLHTTP.4.0")

    xmlHttp.open "POST", sLink, false
    xmlHttp.setRequestHeader "Content-Type", "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL]
msgbox stremailvalues
    xmlHttp.send strEmailValues

    PostEmail = xmlHttp.responseText
    
End Function

</script>
</head>
<body>
<h1>Email Test</h1>

<p><input type="button" value="Click here to send email" onclick="SetupEmail()"/></p>
</body>
</html>

Server Side
Code:
<%@language=VBScript%>
<%
'********************************
'*  Declaration Specifications  *
'********************************

	Dim lngBodyFormat			'Body Format (0 or 1, default 1)
	Dim lngMailFormat			'Mail Format (0 or 1, default 1)
	Dim lngImportance			'Importance (0=low; 1=medium; 2=highest)

	Dim strError				'String Returned to caller (Success or Error and error message)
	Dim strFrom					'Indicates From whom the message was sent
	Dim strTo					'Indicates To whom the message is to be sent
	Dim strCc					'Courtesy copy
	Dim strBcc					'Blind Courtesy copy
	Dim strSubject				'Subject of email
	Dim strBody					'Body of Email
	Dim strAttachFile			'Source, Filename, Encoding Method
	Dim strContentBase			'Content Base
	Dim strCurrentLocation		'Current Location
	Dim strValue				'Value
	Dim strAttachURL			'Attached URL
	Dim strSetLocaleIDs			'Local Ids

	Dim objEMail				'Email Object


	strError = vbNullString

'************************************************
'*  Extract the value for each argument passed  *
'************************************************

	strFrom = Request("From")
	if (len(strFrom) = 0) then strError = "Missing FROM property"

	strTo = Request("To")
	if (len(strTo) = 0) then strError = strError & vbCrLf & "Missing TO property"

	strCc = Request("Cc")
	strBcc = Request("Bcc")

	lngBodyFormat = Request("BodyFormat")
	if (len(lngBodyFormat) > 0) then
		if (lngBodyFormat <> 0) and (lngBodyFormat <> 1) then lngBodyFormat = 1
	else
		lngBodyformat = 1
	end if
	
	lngMailFormat = Request("MailFormat")
	if (len(lngMailFormat) > 0) then
		if (lngMailFormat <> 0) and (lngMailFormat <> 1) then lngMailFormat = 1
	else
		lngMailFormat = 1
	end if
	
	lngImportance = Request("Importance")
	if (len(lngImportance) > 0) then
		if (lngImportance < 0) or (lngImportance > 2) then lngImportance = 1
	else
		lngImportance = 1
	end if
	
	strSubject = Request("Subject")
	

	
	strBody = Request("Body")
	strContentBase = Request("ContentBase")
	strCurrentLocation = Request("CurrentLocation")
	strValue = Request("Value")	
	strAttachFile = Request("AttachFile")
	strAttachURL = Request("AttachURL")
	strSetLocaleIDs = Request("SetLocaleIDs")
	
response.write Server.URLEncode(strBody)

'*******************************************************************************************
'*  If From and To properties contain values, then set the properties of the email object  *
'*  and send the email.                                                                    *
'*******************************************************************************************

	if (len(strFrom) > 0) and (len(strTo) > 0) then
	
		on error resume next
		
		Set objEMail = Server.CreateObject("CDONTS.NewMail")
		
		objEmail.From = strFrom
		objEMail.To = strTo
		
		if (len(strCc) > 0) then objEMail.Cc = strCc
		if (len(strBcc) > 0) then objEMail.Bcc = strBcc
		if (len(strSubject) > 0) then objEMail.Subject = strSubject
		if (len(strBody) > 0) then objEMail.Body = strBody
		if (len(strContentBase) > 0) then objEmail.ContentBase = strContentBase
		if (len(strCurrentLocation) > 0) then objEmail.CurrentLocation = strCurrentLocation
		if (len(strValue) > 0) then objEmail.Value = strValue

response.write strBody
	
		objEMail.BodyFormat = lngBodyFormat
		objEMail.MailFormat = lngMailFormat
		objEMail.Importance = lngImportance
		
		if (len(strAttachFile) > 0) then objEMail.Attachfile strAttachFile
		if (len(strAttachURL) > 0) then objEmail.AttachURL strAttachURL
		if (len(strSetLocaleIDs) > 0) then objEmail.SetLocaleIDs strSetLocaleIDs
	
		objEMail.Send 
		response.write err.number & err.description
		
		Set objEMail = Nothing
		
		Response.Write "Success"
	else
		Response.Write	"Error" & vbcrlf & strError
	end if
%>
 
If the e-mail being sent is HTML encoded then you could do a replace on all spaces client side to "&nbsp;" and all character returns to "<BR>" tags.
 
Thanks Leeus for responding. The problem is that the asp page removes all of the escape codes (as written above). However, I discovered how to read the data (via the asp page) as sent.

So, below is a html page (client) that sends info to an asp routine, who, in turn, sends the email, and returns to the client without ever leaving the client page.

Client Side
Code:
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title> Email Test</title>

<script language="VBScript" >

'*************************
'*  Setup email to send  *
'*************************

Function SetupEmail()

	strFrom = "FromMe@FromMe.com"
	strTo = "FromMe@FromMe.com"
	strCC = vbnullstring
	strBC = vbnullstring
	strSubject = "Subject"
	strBody = "<HTML><HEAD></HEAD><BODY><P>A request has been made<P></P>To view the request, click  <A href=[URL unfurl="true"]http://MyWebSite/MyPage.htm?serverfilter=%22dtmAdded%3D'1/10/2005%202%3A03%3A38%20PM'%22%26Document=Report>here</A></P><P></P>(NOTE:[/URL] This is a test.)</BODY></HTML>"
	strAttachFile = vbnullstring
	strBodyFormat = vbnullstring
	strMailFormat = vbnullstring

	strEmailValue = "~From=" & strFrom & "~To=" & strTo & "~Subject=" & strSubject & "~Body=" & strBody

	msgbox PostEmail( strEmailValue)


End Function

'*************************************
'*  Post email via send to asp page  *
'*************************************

Function PostEmail(ByVal strEmailValues)

    Dim sLink, xmlHttp
 
    sLink = "//MyWebSite/SendEmail.asp"
    set xmlHttp = CreateObject("Msxml2.XMLHTTP.4.0")

    xmlHttp.open "POST", sLink, false
    xmlHttp.setRequestHeader "Content-Type", "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL] 
    xmlHttp.send strEmailValues

    PostEmail = xmlHttp.responseText
    
End Function

</script>
</head>
<body>
<h1>Email Test</h1>
<p><input type="button" value="Click here to send email" onclick="SetupEmail()"/></p>
</body>
</html>

Server Side
Code:
<%@language=VBScript%>
<%
'+***********************************************************************************************************
'*
'*  File:       SendEmail.asp
'* 
'*  Author:     FancyPrairie
'*
'*  Date:       January, 2005
'*
'*  Purpose:    This routine uses CDONTS to send an email via the server.  This routine is
'*              generally called via a call to the function PostEmail.
'*              Note that the function PostEmail uses XmlHTTP to open this file.
'*
'*              The routine expects the HTTP data sent to it to be in the format
'*              ;TagName1=Value1;TagName2=Value2;TagName3=Value3...;TagNamen=Valuen  The arguments
'*              are sent to this routine as a string.  And all arguments are optional except for FROM and To.
'*
'*              NOTE: The first character in the string represents the character that is used to
'*                    separate the Tags.  (In the example above, the semicolon (;) was used.)
'*
'*				You only need to include the arguments applicable to your app.  This example specifies
'*              FROM, TO, SUBJECT, and BODY.
'*
'*              Example  ";From=Smith.JustinOther@mhsil.com;To=Jones.Alias@mhsil.com;Subject=Subject;Body=Body"
'*
'*              The properties and methods for the NewMail object can be found at [URL unfurl="true"]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_denali_newmail_object_cdonts_library_.asp[/URL]
'*
'*              Valid Arguments for the NewMail Properties are:
'*
'*                  From= .............. Specifies the sender of the message
'*                  To= ................ Identifies the recipients of the message. At least one of the To, Cc or Bcc properties (Cc and Bcc properties discussed subsequently) must be nonempty. Each recipient must be addressed in full, with multiple recipients separated by semicolons.
'*                  Cc= ................ (Optional) The Cc property (for indicating recipients of copies of the message) works exactly like the To property
'*                  Bcc ................ (Optional) The Bcc property (for indicating recipients of blind copies of the message) works exactly like the To property
'*                  Subject= ........... (Optional) Subject of the email
'*                  Body= .............. (Optional) Consists of plain text only or HTML
'*                  BodyFormat= ........ (Optional) The BodyFormat property sets the text format of the NewMail object.
'*                                       It is of type Long, and has two possible values:
'*                                         0, which indicates that the body of the message includes Hypertext Markup Language (HTML); or 
'*                                         1 (default), which indicates that the body of the message is exclusively plain text.
'*                  MailFormat= ........ (Optional) The MailFormat property sets the encoding for the NewMail object.
'*                                       It is of type Long, and has two possible values:
'*                                         0, which indicates that the object is to be in MIME (Multipurpose Internet Mail Extension) format; or
'*                                         1 (default), which indicates that the object is to be in uninterrupted plain text.
'*                                       This property determines the default value for the EncodingMethod parameter in the AttachFile method
'*                  Importance= ........ (Optional)  The Importance property allows us to set the importance associated with the NewMail object.
'*                                       Its type is Long, and the possible values are:
'*                                         0, indicating low importance;
'*                                         1 (default), indicating normal importance; and 
'*                                         2, indicating high importance
'*                  ContentBase= ....... (Optional) Sets a base for all URLs relating to the NewMail object's message body.
'*                  CurrentLocation= ... (Optional) Sets an absolute or relative path for all URLs relating to the NewMail object's message body
'*                  Value= ............. (Optional) Sets the value and contents of an additional header for the NewMail object
'*
'*              Valid Arguments for the NewMail Methods are:
'*
'*                  AttachFile= ........ (Optional)  This method has three parameters.
'*                                       The first parameter is Source, of type String or Istream object.
'*                                           This parameter is required, and must contain the full path and file name
'*                                           of the attachment. Only C/C++ and Java programs can use an Istream object.
'*                                       The second (optional) parameter is FileName, of type String.
'*                                           This provides a file name to appear in the attachment's placeholder in
'*                                           the message. If not specified, the file name from the Source
'*                                           parameter is used.
'*                                       The third (optional) parameter is EncodingMethod, of type Long, which indicates
'*                                           the encoding of the attachment. There are two possible values:
'*                                           0, meaning the attachment is in UUEncode format; and
'*                                           1, indicating the attachment is in Base64 format.
'*                                              (Base64 is the encoding scheme defined by MIME;
'*                                              UUEncode is an older format that you should use if you suspect
'*                                              your recipient(s) may not have a MIME-compliant system.)
'*                                           The default value of this parameter depends upon the MailFormat property.
'*                                              If the MailFormat property is set to 1, the default value of EncodingMethod is 0.
'*                                              If the MailFormat property is set to 0, the default value of EncodingMethod is 1.

'*                  AttachURL= ........... (Optional) Adds an attachment to the message and associates a Uniform Resource Locator (URL) with the attachment.
'*                  SetLocaleIDs= ........ (Optional) Sets identifiers that define a messaging user's locale.
'*
'-************************************************************************************************************

'********************************
'*  Declaration Specifications  *
'********************************

	Dim lngBodyFormat			'Body Format (0 or 1, default 1)
	Dim lngMailFormat			'Mail Format (0 or 1, default 1)
	Dim lngImportance			'Importance (0=low; 1=medium; 2=highest)

	Dim strAttachFile			'Source, Filename, Encoding Method
	Dim strAttachURL			'Attached URL
	Dim strBcc					'Blind Courtesy copy
	Dim strBody					'Body of Email
	Dim strCc					'Courtesy copy
	Dim strContentBase			'Content Base
	Dim strCurrentLocation		'Current Location
	Dim strError				'String Returned to caller (Success or Error and error message)
	Dim strFrom					'Indicates From whom the message was sent
	Dim strPostBuffer			'Post Buffer sent to this asp page
	Dim strSeparator            'Tag delimiter
	Dim strSetLocaleIDs			'Local Ids
	Dim strSubject				'Subject of email
	Dim strTo					'Indicates To whom the message is to be sent
	Dim strValue				'Value

	Dim objEMail				'Email Object

'********************************************************************************************************
'*  Get Post Buffer.  The 1st character of the Post Buffer is the character used for deliminating Tags  *
'********************************************************************************************************

	strPostBuffer = Request.Form()
	strSeparator = Mid(strPostBuffer,1,1)
	strPostBuffer = Mid(strPostBuffer,2)

	strError = vbNullString

'************************************************
'*  Extract the value for each argument passed  *
'************************************************

	strFrom = GetTagValue("From",strPostBuffer,strSeparator)							'FROM
	if (len(strFrom) = 0) then strError = "Missing FROM property"

	strTo = GetTagValue("To",strPostBuffer,strSeparator)								'TO
	if (len(strTo) = 0) then strError = strError & vbCrLf & "Missing TO property"

	strCc = GetTagValue("Cc",strPostBuffer,strSeparator)								'Cc
	strBcc = GetTagValue("Bcc",strPostBuffer,strSeparator)								'Bcc

	lngBodyFormat = GetTagValue("BodyFormat",strPostBuffer,strSeparator)				'BodyFormat
	if (len(lngBodyFormat) > 0) then
		if (lngBodyFormat <> 0) and (lngBodyFormat <> 1) then lngBodyFormat = 1
	else
		lngBodyformat = 1
	end if
	
	lngMailFormat = GetTagValue("MailFormat",strPostBuffer,strSeparator)				'MailFormat
	if (len(lngMailFormat) > 0) then
		if (lngMailFormat <> 0) and (lngMailFormat <> 1) then lngMailFormat = 1
	else
		lngMailFormat = 1
	end if
	
	lngImportance = GetTagValue("Importance",strPostBuffer,strSeparator)				'Importance
	if (len(lngImportance) > 0) then
		if (lngImportance < 0) or (lngImportance > 2) then lngImportance = 1
	else
		lngImportance = 1
	end if
	
	strSubject = GetTagValue("Subject",strPostBuffer,strSeparator)						'Subject
	
	strBody = GetTagValue("Body",strPostBuffer,strSeparator)							'Body

	strContentBase = GetTagValue("ContentBase",strPostBuffer,strSeparator)				'ContentBase
	strCurrentLocation = GetTagValue("CurrentLocation",strPostBuffer,strSeparator)		'CurrentLocatin
	strValue = GetTagValue("Value",strPostBuffer,strSeparator)							'Value
	strAttachFile = GetTagValue("AttachFile",strPostBuffer,strSeparator)				'AttachFile
	strAttachURL = GetTagValue("AttachURL",strPostBuffer,strSeparator)					'AttachURL
	strSetLocaleIDs = GetTagValue("SetLocaleIDs",strPostBuffer,strSeparator)			'SetLocaleIDs
	
'*******************************************************************************************
'*  If From and To properties contain values, then set the properties of the email object  *
'*  and send the email.                                                                    *
'*******************************************************************************************

	if (len(strFrom) > 0) and (len(strTo) > 0) then
	
		on error resume next
		
		Set objEMail = Server.CreateObject("CDONTS.NewMail")
		
		objEmail.From = strFrom
		objEMail.To = strTo
		
		if (len(strCc) > 0) then objEMail.Cc = strCc
		if (len(strBcc) > 0) then objEMail.Bcc = strBcc
		if (len(strSubject) > 0) then objEMail.Subject = strSubject
		if (len(strBody) > 0) then objEMail.Body = strBody
		if (len(strContentBase) > 0) then objEmail.ContentBase = strContentBase
		if (len(strCurrentLocation) > 0) then objEmail.CurrentLocation = strCurrentLocation
		if (len(strValue) > 0) then objEmail.Value = strValue

		objEMail.BodyFormat = lngBodyFormat
		objEMail.MailFormat = lngMailFormat
		objEMail.Importance = lngImportance
		
		if (len(strAttachFile) > 0) then objEMail.Attachfile strAttachFile
		if (len(strAttachURL) > 0) then objEmail.AttachURL strAttachURL
		if (len(strSetLocaleIDs) > 0) then objEmail.SetLocaleIDs strSetLocaleIDs
	
		objEMail.Send 
		
		Set objEMail = Nothing
		
		Response.Write "Success"
	else
		Response.Write	"Error" & vbcrlf & strError
	end if

'+******************************************************************************************
'*  Function:	GetTagValue
'*
'*  Author:		FancyPrairie
'*
'*	Date:		2/20/2004
'*
'*  Purpose:	This routine will return the value assigned to a tag.  The format of the
'*                tags are assumed to be in the following format (which each set of tags
'*                separated by semicolons.
'*                TagItem1=TagValue1;TagItem2=TagValue2;....TagItemn=TagValuen;
'*
'*  Arguments:  	strItem (string)
'*              	----------------
'*              	This is the name of the item for which you want the value returned.
'*
'*              	strTag (string)
'*              	---------------
'*              	This is the string that contains the list of tags.
'*
'*                  strSeparator
'*                  ------------
'*                  This represents the character the separates the Tags
'*
'*			Example:
'*                    strTag = "FirstName=Bill;LastName=Schimm;
'*
'*                    strFirstName = GetTagValue("FirstName",strTag,";")   'Returns "Bill"
'*
'-******************************************************************************************

Function GetTagValue (strItem, strTag, strSeparator)

'********************************
'*  Declaration Specifications  *
'********************************

	Dim astrTag				'Array of Tags
	
	Dim i					'Working Variable
	Dim k					'Working Variable

'********************************************
'*  Convert the string to an array of tags  *
'********************************************

	astrTag = split(strTag,strSeparator)

'************************************************************
'*  Loop thru the list of tags and look for the equal sign  *
'*  When found, determine the name of the tag item.  If it  *
'*  matches the one the caller is looking for, return the   *
'*  value to the caller.                                    *
'************************************************************
	
	for i = 0 to ubound(astrTag)										'Loop thru list
		k = instr(1,astrTag(i),"=")										'Find = sign
		if (k > 0) then													'IFT, = sign found
			if (trim(strItem) = trim(mid(astrTag(i),1,k-1))) then		'   Trim up to (not including the = sign) and see if it matches what the caller is looking for
				GetTagValue = trim(mid(astrTag(i),k+1))					'   Match...Return value to caller
				exit function
			end if
		end if
	next 
	
	GetTagValue  = vbNullString							'No Match
	
end function

%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top