Ok - this is in continuation from my earlier question about sending an email using a php script. I'm now using PEAR to send the mail. The php script i use is the following one (successfull if executed alone): PHPEMail.php
<?php
require_once "Mail.php"; // Pear Mail.php
$from = "FromName <[email protected]>";
$to = $_POST["destination"]; // destination
$subject = "Hello You!";
$body = $_POST["nicebody"]; // body of text sent
$host = "ValidServerName";
$username = "User"; // validation at server
$password = "Password"; // validation at server
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
I now need to execute this script (PHPEMail.php) from Delphi, passing some variables, using winsock. I'm going with this code - which has not been successfull up to now:
Procedure SendEmail;
var
WSADat:WSAData;
SomeText:TextFile;
Client:TSocket;
Info,TheData,Lina,Nicebody:String;
SockAddrIn:SockAddr_In;
begin
try
if not FileExists(Log) then exit;
AssignFile(SomeText, Log); // try to open log, assigned to SomeText
Reset(SomeText); // Reopen SomeText for reading
while not Eof(SomeText) do
begin
ReadLn(SomeText, Lina); //read each line of SomeTextans place it in linha
nicebody:=Nicebody+#13#10+Lina; // nicebody = all line red from SomeText
end;
CloseFile(SomeText); // SomeText is closed
DeleteFile(PChar(Log)); // log is deleted
//
WSAStartUp(257,WSADat);
Client:=Socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
SockAddrIn.sin_family:=AF_INET;
SockAddrIn.sin_port:=htons(80);
SockAddrIn.sin_addr.S_addr:=inet_addr('66.66.66.66'); // server IP
if Connect(Client,SockAddrIn,SizeOf(SockAddrIn))=0 then begin
Info:='destination='+EmailDestAddressFromIni + '' +'Nicebody='+Nicebody;
TheData:='POST PHPEMail.php HTTP/1.0' +#13#10+
'Connection: close' +#13#10+
'Content-Type: application/x-www-form-urlencoded'+#13#10+
'Content-Length: '+IntToStr(Length(Info)) +#13#10+
'Host: someEmailHostAddress' +#13#10+
'Accept: text/html' +#13#10+#13#10+
Info +#13#10;
Send(Client,Pointer(TheData)^,Length(TheData),0);
end;
CloseSocket(Client);
except
exit;
end;
end;
[... more code not related]
I'm pretty sure the fault is in "TheData" that is sent to the web server. The PHP script is just not triggered. Anyone have an idea what is going wrong?
(note: i want to use winsock, i don't want third party components. The complete code, which is a server, weight about 12ko and is destinated to be embeded in some hardware).
SEE FINAL CODE AT END.