Executing PHP Script from VB6 *RESOLVED*
Hello,
I am trying to execute a PHP Script that sends an email populated with variable from a vb6 form. I am trying to use the post method, but not sure this is even the best way to accomplish my goal here is what I have:
VB Code:
Private Sub Command1_Click()
'text4 is just a url address
Winsock1.Connect Text4, 80
End Sub
Private Sub Winsock1_Connect()
Dim strHttpRequest As String
Dim strPHP As String
strPHP = Text4 & "/vbMail.php"
strHttpRequest = "POST " & strPHP & " HTTP/1.0\r\n"
strHttpRequest = strHttpRequest _
& " Content-Length:65\r\n" & vbCrLf _
& " Content-Encoding: application/x-www-from-urlencoded\r\n\r\n" & vbCrLf _
& " Var1=Jesse&Var2=21&var3=Fort Collins" _
& vbCrLf
Winsock1.SendData strHttpRequest
'var1 var2 and var3 are the variables I am trying to send
End Sub
my PHP Script is:
PHP Code:
function ReturnValue($Field) {
return $Field;
}
$msg = "Hello";
$msg .= ReturnValue($_POST[Var1]);
$msg .= ReturnValue($_POST[Var2]);
$msg .= ReturnValue($_POST[Var3]);
$recipient = "[email protected]";
$subject = "VB6";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Reply-To: [email][email protected][/email]";
mail($recipient,$subject,$msg,$headers);
?>
This does not work and I am new to PHP and very new to vb6 integrating with PHP. Thanks in advance for any help
Jesse
Re: Executing PHP Script from VB6
Which part of the application doesn't work and what errors, if any, are you getting?
Re: Executing PHP Script from VB6
It Appears to go off with out a hitch It runs through all the connection code with no errors I just never get an email. I am pretty sure it is not the PHP portion however since if I access it though a browser window I do get an email.
Re: Executing PHP Script from VB6
The problem is in your HTTP request. I recognise it from the reply I gave Electroman earlier. Let me explain it a little more.
- \r\n is the PHP escape sequence for the carridge return and new line characters. Each header in the HTTP request must be seaparted by a CRLF line ending. As VB does not understand \r\n you need to use the vbCrLF constant. To signify the end of the headers and the start of the content we use a blank line.
- The Content-Length header must be the exact number of characters in your message body. In this case you've used 65, which is incroeect.
- The third problem, is you have told the server you will be using URL encoding in the message body but the data is not encoded properly. Namely the space hasn't been converted to %20 or a + chracter.
This corrected version should work:
VB Code:
Private Sub Winsock1_Connect()
Dim strHttpRequest As String
Dim strPHP As String
Dim strData As String
Dim intContentLength As Integer
strData = "Var1=Jesse&Var2=21&Var3=Fort+Collins"
intContentLength = Len(strData)
strPHP = Text4 & "/vbMail.php"
strHttpRequest = "POST " & strPHP & " HTTP/1.0" & vbCrLf & _
" Content-Length:" & intContentLength & vbCrLf & _
" Content-Type: application/x-www-from-urlencoded" & vbCrLf & _
vbCrLf & strData
Winsock1.SendData strHttpRequest
End Sub
Re: Executing PHP Script from VB6
I got it to access the php script with this code
VB Code:
strData = "Var1=Jesse&Var2=21&Var3=Fort Collins"
CreateSendString = "POST /vbMail.php HTTP/1.1" & vbCrLf & _
"Host: www.hostaddress.com" & vbCrLf & _
"Content-Length: " & Len(strData) & vbCrLf & _
vbCrLf & strData & _
However it is not passing the variables listed in strData do you know the correct syntax for this.
Re: Executing PHP Script from VB6
Did you read my post above?
Re: Executing PHP Script from VB6
Yes and it also works to execute the php file but it still will not pass the 3 variables in strData.
By the way thank you for all your help on this I really appreciate it.
Re: Executing PHP Script from VB6
What does the Tex4 variable in your code contain. You PHP script should be executed.
1 Attachment(s)
Re: Executing PHP Script from VB6
Yes My php script is executed but the the variables are not passed. I am posting a copy of my form if you enter a valid email address you should get a email contaning Hello [first name] [last name].
ps. You can go to http://www.phototix.com/temp.html to run the php script via a web browser.
Re: Executing PHP Script from VB6
My mistake, you should be sending the Content-Type header, not the Content-Encoding header. Also, make sure the HTTP request looks like this and uses the HTTP 1.0 otherwise the server may send back a chunked response, which needs to be re-assembled.
VB Code:
strHttpRequest = "POST " & Text4 & " HTTP/1.0" & vbCrLf & _
"Host: www.phototix.com" & vbCrLf & _
"Content-Length: " & Len(strData) & vbCrLf & _
"Content-Type: application/x-www-form-urlencoded" & vbCrLf & _
vbCrLf & strData
P.s: don't send cache control headers as they are response headers and my apologies for giving you incorrect code ;)
Re: Executing PHP Script from VB6
Re: Executing PHP Script from VB6 *RESOLVED*
I have since taken down the php script from my server. But for the benifit of this thread here is what it contained
PHP Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?php
function ReturnValue($Field) {
return $Field;
}
?>
</head>
<body>
<?php
$msg = "Hello ";
$msg .= ReturnValue($_POST[Var1]);
$msg .= ReturnValue($_POST[Var2]);
$msg .= ReturnValue($_POST[Var3]);
$recipient1 = ReturnValue($_POST[Var3]);
$subject = "VB6 PHP Post";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Reply-To: ";
$headers .= ReturnValue($_POST[Var3]);
mail($recipient1,$subject,$msg,$headers);
?>
</body>
</html>
Re: Executing PHP Script from VB6 *RESOLVED*
What's the point of the ReturnValue function? :confused: