Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts

    Converting Code from VBScript to Javascript

    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

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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...

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Quote Originally Posted by DanInMa View Post
    Vbscript/asp = Server side language

    javascript = Client side language

    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.
    Code:
    <%@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

    Code:
    <%@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...

    Code:
    <%@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 )
    Last edited by blaze4218; 09-23-2011 at 07:10 PM.

  • #5
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    that is not javascript code. just becuase you put <%@LANGUAGE="JavaScript"%> at the top doesnt magically make it javascript.

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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.

  • #7
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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...

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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:
    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!
    
    }
    %>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • The Following 2 Users Say Thank You to Old Pedant For This Useful Post:

    blaze4218 (09-23-2011), jason_kelly (09-24-2011)

  • #10
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    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".

  • #11
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I'm totally going to use that, thanx!

    BTW @Old Pedant I was kinda hoping you might weigh in on my issue also ... if then esle optimization

    @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...
    Last edited by blaze4218; 09-23-2011 at 08:31 PM.

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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...)

  • #13
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    blaze4218, I apologize. I hadn't refreshed the page & I didn't see your posts. Sorry!

  • #14
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    truce

  • #15
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •