Can you declare a variable in ASP classic like this? - asp-classic

I am trying to see if this can be done in classic ASP:
Dim myVar
myVar = <<< END
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>test</title>
</head>
<BODY>
END
I can do this in PHP, but I am not positivte if it can be done in ASP.
Problem is I need to handle the HTML output via a variable, and I don't want to go through the HTML and convert " to ""
EDIT:
I've found that this is called HEREDOC syntax in PHP.
Since its been asked, what I am trying to do is store HTML type tags (which may contain ', " characters which would otherwise break
myVar = "<stuff color="red">here</stuff>"
so I would need to fix it by replacing color="red" with color=""red""
PART OF THE PROBLEM:
I don't want to have to replace " with "" for the content as I assign it, I guess a HEREDOC syntax is not available for ASP classic.
OK FINE... :P
Since everyone is asking me WHY I am going about it this way, here is why, I have to support this old ASP code, I don't want to, but suddenly the scope of this old app changes that they want the contents (which used to be an HTML page) to be emailed, SO... I wanted to HEREDOC the HTML output, pass it to the mail function and have it email. Having said that, I know its sloppy, and I know it works better the other way, however this is what the job called for, I didn't want to re-write it, I just wanted to augment the output from HTML to HTML-EMAIL...
Hope that makes more sense ;)

The easiest thing to do would be to keep the HTML in a separate file, and then open that file using a TextStream object, reading the string into a variable.

No. One language's syntax isn't going to work in a different language. You can, however, assign a string literal to the variable:
Dim myVar
myVar = _
"<html xmlns=""http://www.w3.org/1999/xhtml"" lang=""en"" xml:lang=""en"">" & vbCrLf & _
"<head>" & vbCrLf & _
" <title>test</title>" & vbCrLf & _
"</head>" & vbCrLf & _
"" & vbCrLf & _
"<BODY>" & vbCrLf

I'm pretty sure you can't do exactly what you're asking here.
It might help, however, if you explained what you're trying to do. There may be some other way.
update:
You could consider putting your HTML fragments into seperate files. This would allow you to define them without having to do any reformatting of the HTML. In a similar manner you could put them into a database or even into resource files.
I'm still not 100% clear why you want these HTML fragments defined as variables, but these approaches would work.

there is no herodoc for asp :) gogo escape - its the only useable way

Related

ASP Classic CDOSYS Email via HTMLBody format

I have an issue sending an email using CDOSYS messaging system in ASP Classic using HTMLBody format. It seems to have a character limit, and when the email message is being sent, it cuts the message off around the bottom of the email. At first, I thought the message was being sent before the entire email could be written, but then after some troubleshooting and research, there is some sort of CDOSys message character limit when using HTMLBody. My question is, is there any way to override the character limit or get around the restriction? This is only for HTMLBODY.
EDIT: The "duplicate" flag would not have helped me. This is not a truncation issue, but an issue with HTMLBody limitations that prevent lengthy messages from fully being sent in the message. I checked that article and it was not what I was looking at an answer to. The responses on this thread were sufficient in answering my question. Thank you all.
Here is my code:
Set myMail=CreateObject("CDO.Message")
HTML = HTML & "<html>"
HTML = HTML & "<body>"
HTML = HTML & "<font face='calibri'>"
HTML = HTML & "<img src='http://" & SupportTagURLWebPath & "/images/SkypeEmailHeader.png'>"
HTML = HTML & "<br><Br>"
HTML = HTML & "<font face='calibri'>"
HTML = HTML & "<b>To " & Request.Form("SkypeTemplateName") & ":</b>"
HTML = HTML & "<br><br>"
HTML = HTML & "Thank you for contacting the Bank of America Service Desk. We're committed to providing seamless support in the moments that matter."
HTML = HTML & "<br><br>"
HTML = HTML & "We heard your concerns with Skype for Business audio/video, and recommend using approved Skype for Business devices to resolve the issue."
HTML = HTML & "<br><br>"
HTML = HTML & "<h4><font color='red'>What do I need to do?</font></h4>"
HTML = HTML & "<div style='background-color: #FFF8DC;'>"
HTML = HTML & "1. Visit the <a href='http://u.go/pchk'>Skype for Business Peripheral Checker</a> & complete the form.<br>"
HTML = HTML & "<img src='http://" &SupportTagURLWebPath & "/images/SkypeEmailbody.png'><br>"
HTML = HTML & "4. Once approved, your new device(s) will be shipped to you. To get started, visit the <a href='http://u.go/tIxvB5'>Skype for Business page</a> and select <i>Setup your equipment</i> tab."
HTML = HTML & "</div>"
HTML = HTML & "<br><br>"
HTML = HTML & "<br>"
HTML = HTML & "If you still encounter Skype for Business audio/visual issues with your new device(s), please <a href='http://u.go/7I76vm'>submit a web ticket</a> and one of our expert Bank of America Service Desk employees will reach out to you."
HTML = HTML & "Thank you,"
HTML = HTML & "<br>"
HTML = HTML & "Premium Service Desk"
HTML = HTML & "<br><Br>"
HTML = HTML & "<img src='http://" & SupportTagURLWebPath & "/images/SkypeEmailFooter.png'>"
HTML = HTML & "</font>"
HTML = HTML & "</body>"
HTML = HTML & "</html>"
myMail.Subject= "Skype for Business audio/visual experience"
myMail.From=EMAILADDRESS
myMail.To=Request.Form("SkypeTemplateEmail")
'mymail.CC= Request.Form("displayemail")
myMail.BCC="psd.wmwhc#bao.com"
myMail.ReplyTo="Do Not Reply"
'myMail.TextBody="This is a message."
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
'Name or IP of remote SMTP server
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver")="amta3dns.bo.com"
'Server port
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
myMail.Configuration.Fields.Update
'myMail.TextBody= Request.Form("genfeedback")
myMail.HTMLBody=HTML
myMail.Send
set myMail=nothing
It is a line length issue. Without the crlf you blow out the max length at 998 characters. Best thing to do would be to write a function that splits the content in mid-string, avoiding the html tags. HTML text, e.g. in a para, ignores line breaks.
The RFC says
There are two limits that this standard places on the number of
characters in a line. Each line of characters MUST be no more than
998 characters, and SHOULD be no more than 78 characters, excluding
the CRLF.
The 998 character limit is due to limitations in many implementations
which send, receive, or store Internet Message Format messages that
simply cannot handle more than 998 characters on a line. Receiving
implementations would do well to handle an arbitrarily large number
of characters in a line for robustness sake. However, there are so
many implementations which (in compliance with the transport
requirements of [RFC2821]) do not accept messages containing more
than 1000 character including the CR and LF per line, it is important
for implementations not to create such messages.
The more conservative 78 character recommendation is to accommodate
the many implementations of user interfaces that display these
messages which may truncate, or disastrously wrap, the display of
more than 78 characters per line, in spite of the fact that such
implementations are non-conformant to the intent of this
specification (and that of [RFC2821] if they actually cause
information to be lost). Again, even though this limitation is put on
messages, it is encumbant upon implementations which display messages
Thanks go to the blog at emailonacid for the pointer.
I was able to figure it out via another thread doing a google search. If you place the below line for every 3-5 lines of the email message, it seems to "reset" or loop the number limit back to the beginning without changing the format of the email. I am not sure if this is the correct way of handling it, but it seems to work for a number of messages I created today which have pretty lengthy messages that would otherwise be cut off when the message is sent. I hope this helps anyone else with this issue.
HTML = HTML & vbCrLf
Instead of inserting the message body in the script and using a lot of HTML = HTML & "text goes here", I created an actual HTML file and read each line in to the HTMLBody:
Set myFSO=Server.CreateObject("Scripting.FileSystemObject")
' Read Email Template File
Set myFile=myFSO.OpenTextFile(Server.MapPath("..\msg\"+Request.Form("Template")+".html"), 1)
EmailBodyTemplate=""
Do While myFile.AtEndOfStream=False
EmailBodyTemplate=EmailBodyTemplate&myFile.ReadLine
Loop
myFile.Close
Set myFile=Nothing
Set myFSO=Nothing
myMail.HTMLBody=EmailBodyTemplate
Not only will this automatically place a vbCrLf at the end of each line read in from the file but it also makes the function generic so that you can send any email you wish by changing the template name.

Quote become " on PHPExcel

I'm using PHPExcel to export data into excel report. But the below characters are being altered.
" become "
& become &
Example:
"The Company" --> become "The Company"
Me & My Company --> become Me & My Company
Can anyone help me on how can I fix it. Thanks!!
Double-check the strings that you are passing to PHPExcel to make sure that they don't already have the HTML elements in them. Note that if you are debugging by dumping output to your browser then the string in your PHP code may actually already be
This & That
but the browser will display it as
This & That.

Write a file containing ASP delimiters

We have lots of files containing image / static file references to resources on our site. As we are moving these files to S3 hosting, I am writing a script that reads each of our ASP files, replaces each reference so that each one points at a globally defined variable, and rewrites those changes to the file, so that we don't have to update all these files manually!
This works alright with references inside actual code.
eg.
Response.Write "<img src=""http://www.site.com/images/image.gif"">"
becomes
Response.Write "<img src=""" & s3BucketName & "/images/image.gif"">"
But when it comes to replacing code like this :
<script src="/javascript/script.js">
It's more difficult - as we have to place the ASP delimiters <%= %>
around the global var around it.
eg.
<script src="<%=s3BucketName%>/javascript/script.js">
Because the script delimiters actually stop the page where they are, and generate an error, that's what I'm finding difficult. URLencoding the <%=s3BucketName%> string, and writing, just writes the encoded text (unusable by ASP), doesn't transform into what I want.
Any ideas gratefully received.
Thanks, Adam
Ok, I've come up with a solution, seems to work. If you find a better one, please comment.
textToReplace = "src="""& chr(60) & chr(37) & chr(61) & "s3BucketName" & chr(37) & chr(62) & "/javascript/script.js"
Basically each symbol of <%= and %> is converted into chr() values.
You could use the basetag in the head section of your pages then you can leave the relative urls like your script example alone, you can combine this technique with the one peedeeaay suggests.
<base href="http://www.s3bucketdomain.com/directory/" />

Server side call to webservice in classic ASP

I've .NET webservice, which takes a encoded html-string as a parameter, decodes the string and creates a PDF from the html. I want to make a synchronous server side call to the webservice from a classic asp webpage. It works fine if use a plain text string (with no html tags), but when I send a encoded html string the webservice it seems that the string is empty when it reaches the webservice.
The webservice is working fine when I call it from client side, with both plain text string and an encoded html string.
My code looks like this:
Private Sub SaveBookHtmlToPdf(pHtml, pShopId)
Set oXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")
Dim strEnvelope
strEnvelope = "pShopId=" & pShopId & "&pEncodedHtml=" & Server.HTMLEncode(pHtml)
Call oXMLHTTP.Open("POST", "https://mydomain.dk:4430/PdfWebservice.asmx/SaveBookToPdf", false)
Call oXMLHTTP.SetRequestHeader("Content-Type","application/x-www-form-urlencoded")
Call oXMLHTTP.Send(strEnvelope)
Set oXMLHTTP = Nothing
End Sub
It smells like some kind of security issue on the server. It's working when posting a asynchronous call from the client side, but not when it comes from server side - it seems that the encoded html string is somehow not allowed in a server side call to the webservice.
Anyone who know how to solve this tricky problem?
This looks all wrong to me:
Server.HTMLEncode(pHtml)
Its quite common for developers to get confused between HTML encoding and URL encoding even though they are quite different. You are posting data that needs to be URL encoded. Hence your code should use URLEncode instead:
strEnvelope = "pShopId=" & pShopId & "&pEncodedHtml=" & Server.URLEncode(pHtml)
Edit:
One thing that URLEncode does that may not be compatible with a URLEncoded post is it converts space to "+" instead of "%20". Hence a more robust approach might be:
strEnvelope = "pShopId=" & pShopId & "&pEncodedHtml=" & Replace(Server.URLEncode(pHtml), "+", "%20")
Another issue to watch out for is that the current value of Response.CodePage will influence how the URLEncode encodes non-ASCII characters. Typically .NET does things by default in UTF-8. Hence you will also want to make sure that your Response.CodePage is set to 65001.
Response.CodePage = 65001
strEnvelope = "pShopId=" & pShopId & "&pEncodedHtml=" & Replace(Server.URLEncode(pHtml), "+", "%20")
This may or may not help but I use a handy SOAP Class for Classic ASP which solved a few problems I was having doing it manually. Your code would be something like this:
Set cSOAP = new SOAP
cSOAP.SOAP_StartRequest "https://mydomain.dk:4430/PdfWebservice.asmx", "", "SaveBookToPdf"
cSOAP.SOAP_AddParameter "pShopId", pShopId
cSOAP.SOAP_AddParameter "pEncodedHtml", Server.HTMLEncode(pHtml)
cSOAP.SOAP_SendRequest
' result = cSOAP.SOAP_GetResult("result")
You will probably need to set your namespace for it to work ("" currently), and uncomment the 'on error resume next' lines from the class to show errors.
AnthonyWJones made the point about URL encoding and HTML encoding, and the original problem being experienced is likely a combine of the two, a race condition if you will. While is was considered answered, it partially wasn't, and hopefully this answers the cause of the effect.
So, as the message get HTMLEncoded, the html entities for the tags become such '<' = '<'.
And as you may know, in URLEncoding, &'s delimit parameters; thus the first part of this data strEnvelope = "pShopId=" & pShopId & "&pEncodedHtml=" & Server.HTMLEncode(pHtml) upto the "&pEncodedHtml" bit, is fine. But then "<HTML>..." is added as the message, with unencoded &'s...and the receiving server likely is delimiting on them and basically truncating "&pEncodedHtml=" as a null assign: "&pEncodedHtml=<HTML>... ." The delimiting would be done on all &'s found in the URL.
So, as far as the server is concerned, the data for parameter &pEncodedHtml was null, and following it were now several other parameters that were considered cruft, that it likely ignored, which just happened to actually be your message.
Hope this provides additional info on issues of its like, and how to correct.

Conditional includes in Classic ASP - where the file may not exist on the server

I am currently in a situation where I have to make some additions to an application written in classic ASP using server-side JScript on IIS.
The additions that I need to make involve adding a series of includes to the server-side code to extend the application's capabilities. However, the inc files may not exist on the server in all cases, so I need the application to fall back to the existing behavior (ignore the includes) if the files do not exist, rather than generating an error.
I know that this can't be accomplished using if statements in the JScript code because of the way that SSI works, and have not come across any ways of dynamically including the code on the server side, where the files may not exist.
Does anyone know of a way to accomplish this in classic ASP? Any help would be much appreciated.
Here's a script to dynamically include asp files:
<%
' **** Dynamic ASP include v.2
function fixInclude(content)
out=""
if instr(content,"#include ")>0 then
response.write "Error: include directive not permitted!"
response.end
end if
content=replace(content,"<"&"%=","<"&"%response.write ")
pos1=instr(content,"<%")
pos2=instr(content,"%"& ">")
if pos1>0 then
before= mid(content,1,pos1-1)
before=replace(before,"""","""""")
before=replace(before,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
before=vbcrlf & "response.write """ & before & """" &vbcrlf
middle= mid(content,pos1+2,(pos2-pos1-2))
after=mid(content,pos2+2,len(content))
out=before & middle & fixInclude(after)
else
content=replace(content,"""","""""")
content=replace(content,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
out=vbcrlf & "response.write """ & content &""""
end if
fixInclude=out
end function
Function getMappedFileAsString(byVal strFilename)
Dim fso,td
Set fso = Server.CreateObject("Scripting.FilesystemObject")
Set ts = fso.OpenTextFile(Server.MapPath(strFilename), 1)
getMappedFileAsString = ts.ReadAll
ts.close
Set ts = nothing
Set fso = Nothing
End Function
execute (fixInclude(getMappedFileAsString("included.asp")))
%>
The last line (the one starting with "execute") is equivalent to an "include" directive, with the difference that it can be included inside an "if" statement (dynamic include).
Bye
If you are really brave, you can read the contents of the file and then Eval() it.
But you will have not real indication of line numbers if anything goes wrong in the included code.
As a potentially better alternative: Can you not create some sanity check code in global.asa to create the include files as blanks if they do not exist?
Put simply, no. Why would the files not exist? Can you not at least have empty files present?
What you could do is something like this:
Use Scripting.FileSystemObject to detect the presence of the files
Use Server.Exeecute to "include" the files, or at least execute the code.
The only problem is that the files cannot share normal program scope variables.
The solution to this turned out to be to use thomask's suggestion to include the files and to set a session variable with a reference to "me" as per http://www.aspmessageboard.com/showthread.php?t=229532 to allow me to have access to the regular program scope variables.
(I've registered because of this, but can't seem to associate my registered account with my unregistered account)

Resources