?
Solved

Script conversion from php to asp

Posted on 2007-10-16
31
Medium Priority
?
340 Views
Last Modified: 2013-12-13
I have an email script written in php, and I need it converted to asp. Please help. Thanks.

<?php


// get posted data into local variables
$EmailFrom = "removed";
$EmailTo = "removed";
$Subject = "removed";
$Name = Trim(stripslashes($_POST['Name']));
$Phone = Trim(stripslashes($_POST['Phone']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=sent.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
0
Comment
Question by:isaacr25
  • 12
  • 10
  • 8
  • +1
31 Comments
 
LVL 8

Accepted Solution

by:
Wikkard earned 690 total points
ID: 20090058

<%@language=vbscript%>
<%
function stripslashes(thedata)
      tmp = replace(thedata,"/","")
      tmp = replace(tmp, "\","")
      stripslashes= tmp 'return the result
end function

'trim is built in vbs function

on error resume next 'might want to comment this line when testing

emailfrom = "removed"
emailto = "removed"
subject = trim(stripslashes(request.form("name")))
phone = trim(stripslashes(request.form("phone")))
email =trim(stripslashes(request.form("email")))
message = trim(stripslashes(request.form("message")))

validationok = true

'validate the above variable
'validationok = false if any errors

if NOT validationOK then
      body = body & "Name: " & name " & vbcrlf
      body = body & "Phone: " & phone & vbcrlf
      etc etc
end if


'sending email depends on the server and asp components you have available
'easiest way is to use CDONTS
Set myCDONTSMail = CreateObject("CDONTS.NewMail")
myCDONTSMail.Send emailFrom,emailTo,subject,Body

         Set myCDONTSMail  = Nothing


'err is build in error object
if err then
      reponse.redirect("error.htm")
else
      resonse.write "Email Sent"
end if
%>
0
 

Author Comment

by:isaacr25
ID: 20090119
I saved the code as contact.asp, and changed the form action to contact.asp. When I click the submit button, I get a HTTP 500 Internal Server Error, with the domain name plus contact.asp in the address bar. Here is the form code:

<form action="contact.asp" id="form" style="margin:0; padding:0" method="post">
                                                  <div align="center">
                                                    <input name="Name" type="text"  class="w" id="Name" style="width:210px; height:25px;  " value=" &nbsp;your name"  >
                                                    <br>
                                                    <input name="Email" type="text"  class="w" id="Email" style="width:210px; height:25px; " value=" &nbsp;e-mail" >
                                                    <br>
                                                    <input name="Phone" type="text"  class="w" id="Phone" style="width:210px; height:25px; " value=" &nbsp;phone" >
                                                    <br>
                                                    <textarea name="Message"  cols="35" rows="35" class="w" id="Message" style="width:210px; height:90px; overflow:hidden "> &nbsp;message</textarea>
                                                  </div>
                                                  <div style="margin:12 15 0 50px;"><input name="clear" type="image" id="clear" style=" border-style:none  " src="images/6_k1.gif" onClick="this.form.reset(); return false;"><input name="submit"  type="image" id="submit" style="border-style:none; margin-left:16px "  src="images/6_k2.gif" >
                                                  </div>
</form>
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 690 total points
ID: 20090164
ok put this at the top of the page

after <%
response.write now
response.flush

this should then show you the actual error, also take out the "etc etc" line and replace with proper code
:-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:isaacr25
ID: 20090197
When I click the Submit button, I get the same results... a blank page with an HTTP 500 Internal Server Error. No error messages are displayed.

ASP CODE:
after <%
response.write now
response.flush


<%@language=vbscript%>
<%
function stripslashes(thedata)
      tmp = replace(thedata,"/","")
      tmp = replace(tmp, "\","")
      stripslashes= tmp 'return the result
end function

'trim is built in vbs function

on error resume next 'might want to comment this line when testing

emailfrom = "removed"
emailto = "removed"
subject = trim(stripslashes(request.form("name")))
phone = trim(stripslashes(request.form("phone")))
email =trim(stripslashes(request.form("email")))
message = trim(stripslashes(request.form("message")))

validationok = true

'validate the above variable
'validationok = false if any errors

if NOT validationOK then
      body = body & "Name: " & name " & vbcrlf
      body = body & "Phone: " & phone & vbcrlf
      body = body & "Email: " & email & vbcrlf
      body = body & "Message: " & message & vbcrlf
end if


'sending email depends on the server and asp components you have available
'easiest way is to use CDONTS
Set myCDONTSMail = CreateObject("CDONTS.NewMail")
myCDONTSMail.Send emailFrom,emailTo,subject,Body

         Set myCDONTSMail  = Nothing


'err is build in error object
if err then
      reponse.redirect("error.htm")
else
      resonse.write "Email Sent"
end if
%>
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090223
If using IE make sure you have Friendly HTTP error message unchecked.  The settings is in the Advanced area (i.e. tab) of Internet Options.

CDONTS is a nice and even default email component on some servers but those are older ones.  CDONTS was deprecated a few versions ago.  You might need to use CDOSYS or another component.  Is this your web server?  If so, which OS and version?  If not, check with your web host provider to find out what email component to use.  They might even be able to provide you with basic setup, config and use steps.  

Let me know if you have a question or need more info.

bol
0
 

Author Comment

by:isaacr25
ID: 20090233
Friendly HTTP errors is already checked. I don't have my own server... I'm using a host provider. I know that the compatibility is with ASP 2.0.
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090285
>> Friendly HTTP errors is already checked <<

Remove the check.  My post meant to suggest that you uncheck it.  As a developer you need better error messages than the "friendly" one.  It is pretty useless.

Compatibility with ASP 2.0 isn't enough info.  If you have a provider then most likely you are on a current and up to date OS.  You won't be able to use CDONTS.  Please contact them to find out what email component to use.  You should also ask for any required config steps, etc.  There is no way that you or us can guess those things so you should ask.  If you can't get any good help (I would recommend a new web host provider) but you could try using default steps for CDO.  I'll look for some sample scripts for CDO in case you need them.  If that is the component they use and there are no config issues then the default script should work well for the email part.

bol
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090292
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 690 total points
ID: 20090303
There was an extra " in the following line following if validationOK then ....should read as follows:
body = body & "Name: " & name  & vbcrlf

Definitely CDONTS is deprecated however i thought that this was the easiest way to send email without going into server os versions or into asp components.

0
 

Author Comment

by:isaacr25
ID: 20090338
Wikkard,
     I did turn off HTTP Friendly errors and tried again, after removing the extra ". This is what was returned:

Microsoft VBScript compilation error '800a0400'

Expected statement

/contact.asp, line 6

<%@language=vbscript
^
0
 

Assisted Solution

by:Shadi-Elwan
Shadi-Elwan earned 81 total points
ID: 20090341
From b0lsc0tt Links:
Try this:

Include this metatype library code on the page you use this emailing with code because there are some things in it this method needs. You can probably get rid of these two lines if you figure out what it references but I didn't take the time to look.

<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows Library" -->
<!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library" -->

<%
Dim ObjSendMail
Dim iConf
Dim Flds
   
Set ObjSendMail = Server.CreateObject("CDO.Message")
Set iConf = Server.CreateObject("CDO.Configuration")
   
Set Flds = iConf.Fields
With Flds
.Item(cdoSendUsingMethod) = 2
.Item(cdoSMTPServer) = "mail-fwd"
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPconnectiontimeout) = 10
.Update
End With
   
Set ObjSendMail.Configuration = iConf
   
Set ObjSendMail.Configuration = iConf
ObjSendMail.To = "someone@someone.net"
ObjSendMail.Subject = "this is the subject"
ObjSendMail.From = "someone@someone.net"
   
' we are sending a text email.. simply switch the comments around to send an html email instead
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = "this is the body"
   
ObjSendMail.Send
   
Set ObjSendMail = Nothing
Set iConf = Nothing
Set Flds = Nothing
%>
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090424
If the top part of your script is like the section below then you might have missed the closing or had an extra ASP tag.

<%
response.write now
response.flush
%>  ' missing tag

<%@language=vbscript%>
<%
function stripslashes(thedata)
      tmp = replace(thedata,"/","")
      tmp = replace(tmp, "\","")
      stripslashes= tmp 'return the result
end function

Also comment out or delete the on error resume next line.  It is good to have in production script when you have error handling but it can make work harder at this step.

bol
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090439
By the way, deprecated may not have been the right word to use, or been ambiguous.  The CDONTS component is should not even be available if CDO is used.  There may be exceptions to this but by default I don't believe it is available.  That is why I asked. :)

bol
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 690 total points
ID: 20090442
Sorry I should have given the full code again instead of just the changes.
0
 

Author Comment

by:isaacr25
ID: 20090453
Wikkard,
    No code is posted. Thanks.
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 690 total points
ID: 20090462
<%@language=vbscript%>
<html><head></head>
<body>
<%

function stripslashes(thedata)
      tmp = replace(thedata,"/","")
      tmp = replace(tmp, "\","")
      stripslashes= tmp 'return the result
end function

'trim is built in vbs function

'on error resume next 'might want to comment this line when testing

emailfrom = "removed"
emailto = "removed"
subject = trim(stripslashes(request.form("name")))
phone = trim(stripslashes(request.form("phone")))
email =trim(stripslashes(request.form("email")))
message = trim(stripslashes(request.form("message")))

Validationok = true

'validate the above variable
'validationok = false if any errors

if ValidationOK then
      body = body & "Name: " & name  & vbcrlf
      body = body & "Phone: " & phone & vbcrlf
      body = body & "Email: " & email & vbcrlf
      body = body & "Message: " & message & vbcrlf


     'sending email depends on the server and asp components you have available
     'easiest way is to use CDONTS
     Set myCDONTSMail = CreateObject("CDOSYS.NewMail")
     myCDONTSMail.Send emailFrom,emailTo,subject,Body

     set myCDONTSMail  = Nothing

end if
'err is build in error object
if err then
      reponse.redirect("error.htm")
else
      resonse.write "Email Sent"
end if
%>

<form action="test.asp" id="form" style="margin:0; padding:0" method="post">
                                                  <div align="center">
                                                    <input name="Name" type="text"  class="w" id="Name" style="width:210px; height:25px;  " value=" &nbsp;your name"  >
                                                    <br>
                                                    <input name="Email" type="text"  class="w" id="Email" style="width:210px; height:25px; " value=" &nbsp;e-mail" >
                                                    <br>
                                                    <input name="Phone" type="text"  class="w" id="Phone" style="width:210px; height:25px; " value=" &nbsp;phone" >
                                                    <br>
                                                    <textarea name="Message"  cols="35" rows="35" class="w" id="Message" style="width:210px; height:90px; overflow:hidden "> &nbsp;message</textarea>
                                                  </div>
                                                  <div style="margin:12 15 0 50px;"><input name="clear" type="image" id="clear" style=" border-style:none  " src="images/6_k1.gif" onClick="this.form.reset(); return false;"><input name="submit"  type="image" id="submit" style="border-style:none; margin-left:16px "  src="images/6_k2.gif" >
                                                  </div>
</form>

</body>
</html>
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 690 total points
ID: 20090473
Here is is again ...this time with the correct CDO email sending code.



<%@language=vbscript%>
<html><head></head>
<body>
<%

function stripslashes(thedata)
      tmp = replace(thedata,"/","")
      tmp = replace(tmp, "\","")
      stripslashes= tmp 'return the result
end function

'trim is built in vbs function

'on error resume next 'might want to comment this line when testing

emailfrom = "removed"
emailto = "removed"
subject = trim(stripslashes(request.form("name")))
phone = trim(stripslashes(request.form("phone")))
email =trim(stripslashes(request.form("email")))
message = trim(stripslashes(request.form("message")))

Validationok = true

'validate the above variable
'validationok = false if any errors

if ValidationOK then
      body = body & "Name: " & name  & vbcrlf
      body = body & "Phone: " & phone & vbcrlf
      body = body & "Email: " & email & vbcrlf
      body = body & "Message: " & message & vbcrlf


     'sending email depends on the server and asp components you have available
     'easiest way is to use CDO rather than CDONTS
Dim ObjSendMail
Dim iConf
Dim Flds
   
Set ObjSendMail = Server.CreateObject("CDO.Message")
Set iConf = Server.CreateObject("CDO.Configuration")
   
Set Flds = iConf.Fields
With Flds
.Item(cdoSendUsingMethod) = 2
.Item(cdoSMTPServer) = "mail-fwd"
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPconnectiontimeout) = 10
.Update
End With
   
Set ObjSendMail.Configuration = iConf
   
Set ObjSendMail.Configuration = iConf
ObjSendMail.To = "someone@someone.net"
ObjSendMail.Subject = "this is the subject"
ObjSendMail.From = "someone@someone.net"
   
' we are sending a text email.. simply switch the comments around to send an html email instead
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = "this is the body"
   
ObjSendMail.Send
   
Set ObjSendMail = Nothing
Set iConf = Nothing
Set Flds = Nothing



end if
'err is build in error object
if err then
      reponse.redirect("error.htm")
else
      resonse.write "Email Sent"
end if
%>

<form action="test.asp" id="form" style="margin:0; padding:0" method="post">
                                                  <div align="center">
                                                    <input name="Name" type="text"  class="w" id="Name" style="width:210px; height:25px;  " value=" &nbsp;your name"  >
                                                    <br>
                                                    <input name="Email" type="text"  class="w" id="Email" style="width:210px; height:25px; " value=" &nbsp;e-mail" >
                                                    <br>
                                                    <input name="Phone" type="text"  class="w" id="Phone" style="width:210px; height:25px; " value=" &nbsp;phone" >
                                                    <br>
                                                    <textarea name="Message"  cols="35" rows="35" class="w" id="Message" style="width:210px; height:90px; overflow:hidden "> &nbsp;message</textarea>
                                                  </div>
                                                  <div style="margin:12 15 0 50px;"><input name="clear" type="image" id="clear" style=" border-style:none  " src="images/6_k1.gif" onClick="this.form.reset(); return false;"><input name="submit"  type="image" id="submit" style="border-style:none; margin-left:16px "  src="images/6_k2.gif" >
                                                  </div>
</form>

</body>
</html>
0
 

Author Comment

by:isaacr25
ID: 20090474
Ok Wikkard,
    Now here is the error I'm receiving.

Microsoft VBScript runtime error '800a01ad'
ActiveX component can't create object: 'CDOSYS.NewMail'
/contact.asp, line 37
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 690 total points
ID: 20090482
try again ...i just posted a version with the correct mail stuff in it.......cut from Shadi-Elwan's post
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090503
If the retry also fails then you need to contact your web host.  If that default name and component didn't work then they most likely use a different mailer instead.  There are a number of other really good ones and your host is probably using one of them.  My personal favorite is ASPMail and it is very popular.

If the web host is hard to contact then look on their site for info or you might let us know who they are.  There is the small chance that an expert could help just knowing the host. :)

bol
0
 

Author Comment

by:isaacr25
ID: 20090507
Another error:

ADODB.Fields error '800a0bb9'

Unknown runtime error

/contact.asp, line 46
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090526
It would help to see the line when you post the message.  I am pretty sure it is the line below but please confirm.

.Item(cdoSMTPServer) = "mail-fwd"

That needs to have your SMTP server name in the place of mail-fwd.  Most of the time that is something like ...

.Item(cdoSMTPServer) = "smtp.yourmailhost.com"

You could look in your email program for the correct server name (it would be in the outgoing servers setting).  That setting might not work for the ASP script but it is a good thing to try first.

bol
0
 

Author Comment

by:isaacr25
ID: 20090533
Web host is crystaltech.com
0
 

Author Comment

by:isaacr25
ID: 20090541
Opened in DreamWeaver, line 46 is:

.Item(cdoSendUsingMethod) = 2
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090579
Take a look at http://forums.crystaltech.com/index.php/topic,31408.0.html.  There is suggested code using CDO that seemed to work for others with your host.  Notice the port is not the default and will need to be changed.  Also, according to another post, the From email has to be from a valid account with that host.  Let me know how that script works.

bol
0
 

Author Comment

by:isaacr25
ID: 20090596
I have no idea how to add the field variables/values etc to this script. Is there no easy way to get this done, such as here?

http://www.asptutorial.info/script/formtomail/
0
 

Author Comment

by:isaacr25
ID: 20090676
Ok,
     I found and used this script, and it works fine: http://www.codefixer.com/tutorials/sample_form_email_script.asp.

My only questions is how do I redirect to a different page upon successful transmission?
0
 
LVL 8

Assisted Solution

by:Wikkard
Wikkard earned 690 total points
ID: 20090729
response.redirect("someotherpage.html")
0
 

Author Comment

by:isaacr25
ID: 20090748
What if I need the page to show in a container named "cont"?

When I try
response.redirect("sent.html", "cont")  I get this error:

Microsoft VBScript compilation error '800a0414'

Cannot use parentheses when calling a Sub

/contact.asp, line 37

response.redirect("sent.html", "cont")
--------------------------------------^
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 729 total points
ID: 20090797
You can't specify a container.  The page goes into the same browser window.  Since this is server script there is no way to prevent this.  You can write Javascript with the ASP script and have that page send something to cont.  However you can't go direct to cont if that isn't your window.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20631768
I'm glad we could help.  However I noticed the B grade.  Did you still need help with this?  If so let us know.

With all of the answers provided and the complete way in which they were provided I don't really understand how you award the B grade.  If it was a mistake or if you have a question about grading then let me know.  I can help you fix it or answer your questions.

It is nice to have a chance to earn the A or at least know why it wasn't.  In this case the answers seem complete enough that I believe it was.

Thanks for the fun question.  Let us know about the grade.

b0lsc0tt
EE Zone Advisor
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question