...

View Full Version : Converting Code from VBScript to Javascript



jason_kelly
09-23-2011, 06:24 PM
Hello,

I am new javascript and I need your help converting the following code from VBScript to Javascript please.


<script language="vbscript">

function CDO_Mail_Small_Text_2()

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemailadress@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Update
End With

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

With iMsg
Set .Configuration = iConf
.To = "jason.kelly@tc.gc.ca"
.CC = ""
.BCC = ""

'.ReplyTo = "Reply@something.nl"
.From = """ARealName"" <ARealName@gmail.com>"
.Subject = "Important message"
.TextBody = strbody
.Send
End With

end function

</script>



Much thanks and appreciation for all your help and support.

Cheers,

J

blaze4218
09-23-2011, 06:27 PM
I assume you would like to use javascript to access this script and think that you need to convert this to javascript to make that happen. Well I use a similar email VBscript provided by my hosting company and I can tell you right now that I was able to access it through my javascript code...

DanInMa
09-23-2011, 06:32 PM
Vbscript/asp = Server side language

javascript = Client side language

You cannot convert this code directly into javascript as javascript cannot accomplish this functionality.

You can, however, use an Ajax call to submit the desired data to an asp page in the background with your current code.

blaze4218
09-23-2011, 07:03 PM
Vbscript/asp = Server side language

javascript = Client side language
:rolleyes:
Sorry but... WRONG,WRONG,WRONG
I use javascript in my asp pages exclusively unless incorporating VBscripts like above. It's a little different because it can access server technologies, but if transitioning from dynamic HTML to serverside Apps (like I did), it's the best way because you don't have to learn a new language for your logic operations.

<%@LANGUAGE="JavaScript"%>
<%Respons.Write('Try this one on for size')%>
Although I do realize the error of my post. I assumed jason_kelly was using serverside js, but maybe he wants to call this function from a clientside js enhanced page... in which case your right DanInMa, AJAX would be a good solution. @jason_kelly if you are just sending an email (as the script suggests) you just need to send the variables to the vbscript via url variables. You can use AJAX, or any linking method to send them <a href="www.yoursite.com/mailer.asp?name=johnsmith"></a>
and then add this handy dandy javascript to the mailer to recieve them



<%@LANGUAGE="JavaScript"%>
<%
name=Request.Form("name");
%>
//...VBscript after that

add a few Regular Expressions for security and your good to go.
@DanInMa I just said in my post that I already do that...



<%@LANGUAGE="JavaScript"%>
<%
Response.Expires = -1
%>

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

Function CheckEmail(strTemp)
Dim regEx2
Set regEx2 = New RegExp
regEx2.Pattern = "/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i"
regEx2.Global = True
CheckEmail = regEx2.Replace(strTemp, "")
End Function

Function RemoveBadCharacters(strTemp)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = "[^A-Za-z0-9_ ]"
regEx.Global = True
RemoveBadCharacters = regEx.Replace(strTemp, "")
End Function

Function VBSendMail(subject,name,male,phone,comment)

body="NAME:"+name+"<br /><br />E-MAIL:"+male+"<br /><br />PHONE NUMBER:"+phone+"<br /><br />COMMENT:"+comment

sendUrl="http://schemas.microsoft.com/cdo/configuration/sendusing"
smtpUrl="http://schemas.microsoft.com/cdo/configuration/smtpserver"

Set objConfig=CreateObject("CDO.Configuration")
objConfig.Fields.Item(sendUrl)=2 ' cdoSendUsingPort
objConfig.Fields.Item(smtpUrl)="relay-hosting.secureserver.net"
objConfig.Fields.Update

Set objMail=CreateObject("CDO.Message")
Set objMail.Configuration=objConfig
objMail.From="form@mysite.com"
objMail.To="me@mysite.com"
objMail.Subject=subject
objMail.HtmlBody=body
objMail.Send
End Function
</SCRIPT>
<%
name= RemoveBadCharacters(Request.Form("name"));
male= CheckEmail(Request.Form("male"));
subject= RemoveBadCharacters(Request.Form("subject"));
phone= RemoveBadCharacters(Request.Form("phone"));
comment= RemoveBadCharacters(Request.Form("comment"));
VBSendMail(subject,name,male,phone,comment);
%>



EDIT:
Mind you in this example my javiscript was sparse, but I was feeling lazy and didn't want to learn the VBscript syntax for that... (Still haven't :o )

DanInMa
09-23-2011, 07:47 PM
that is not javascript code. just becuase you put <%@LANGUAGE="JavaScript"%> at the top doesnt magically make it javascript.

blaze4218
09-23-2011, 07:50 PM
but it is serverside javascript syntax. Look it up, I've been using it for years, built several apps, it's a real thing, and not the least bit VBScript. The language is defined by the syntax. I learned how to do it on MSDN.

DanInMa
09-23-2011, 07:54 PM
http://static.tvtropes.org/pmwiki/pub/images/picard-facepalm.jpg

blaze4218
09-23-2011, 08:04 PM
You did get the part where I said I was wrong, and where we helped the poster with the relevant information didn't you? Sorry if I ruined your day with my ignorance...

Old Pedant
09-23-2011, 08:23 PM
I strongly suspect that Jason Kelly thinks that he will be able to use JavaScript to send email from a browser, instead of using ASP. He will be sadly disappointed.

However, here is his ASP VBScript code converted to ASP JScript code:


<%@ language="javascript" %>
<%
function CDO_Mail_Small_Text_2()
{
iMsg = CreateObject("CDO.Message");
iConf = CreateObject("CDO.Configuration");

iConf.Load( -1 ) // CDO Source Defaults...I think this is unneeded
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true;
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1;
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemailadress@gmail.com";
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword";
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com";

iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465;
iConf.Fields.Update(); // notice that this MUST be a function call!

strbody = "Hi there\n\n"
+ "This is line 1\n\n"
+ "This is line 2\n\n"
+ "This is line 3\n\n"
+ "This is line 4";

iMsg.Configuration = iConf;
iMsg.To = "jason.kelly@tc.gc.ca";
iMsg.CC = "";
iMsg.BCC = "";

// iMsg.ReplyTo = "Reply@something.nl";
iMsg.From = '"ARealName" <ARealName@gmail.com>';
iMsg.Subject = "Important message";
iMsg.TextBody = strbody;
iMsg.Send(); // notice that this MUST be a function call!

}
%>

Raphael
09-23-2011, 08:25 PM
DanInMa is right - even though it says "JAVASCRIPT", it's actually microsoft's implementation of javascript called jscript which is different from client-side javascript. Javascript was originally designed as a client-side language. MS ripped it off, put in a bunch of proprietary stuff & got sued so they had to rename it "jscript".

blaze4218
09-23-2011, 08:29 PM
I'm totally going to use that, thanx!

BTW @Old Pedant I was kinda hoping you might weigh in on my issue also :o ... http://www.codingforums.com/showthread.php?t=238933

@Raphael I already admitted the err of my ways in my second post and my last post... do we really need to waste anymore time on it? It seems more of a tomato, tamahto kind of issue anyway...

blaze4218
09-23-2011, 08:34 PM
Just for the record, jason_kelly could have made the same distinction that I did and assumed that he needed to convert the vbscript to jscript, in which case my original post might have actually made more sense than DanInMa's (probably not, but I can dream...)

Raphael
09-23-2011, 08:37 PM
blaze4218, I apologize. I hadn't refreshed the page & I didn't see your posts. Sorry! :)

blaze4218
09-23-2011, 08:41 PM
truce :)

jason_kelly
09-24-2011, 03:08 PM
A Great Big Thanks to everyone for everyones help.

Such great help and support on a great forum is always hard to find.

I am glad I found the experts ;)

Cheers and have a great weekend all.

J



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum