Forum > Networking and Web Programming

Post data from fpc like submitting a web form using Indy10?

(1/2) > >>

BosseB:
I need to automate a logging function from an FPC/Lazarus program on Windows.
It currently runs on Windows Server 16.

Current situation:
My application was written back in 2018 in order to send emails on activity events.
It was built with the then current versions of Fpc and Lazarus including Indy10.
And it uses Indy to send the the emails using an account on my ISP.

The purpose of emailing is as a log transfer/sharing function but it has been difficult maintaining the email connections when services become more and more locked down on the Internet...

New transfer method
So I want to switch away from emailing and instead send a POST packet to a php script on my web-server just like if I had filled in a form online with a number of text fields as parameters including a multi-line log text in html format.

I currently have 7 text fields for parameters (they include spaces and special chars like / etc).
Additionally there is one log text which is multi-line and is in HTML format.

My existing program builds the log message from incoming data and sends it as an email message with the html text as the body and I would like to keep the extraction functions in place and just fork out when the emailing is invoked such that I can call another function for this new messaging path.

I can easily build a web page with a form where the submit action is a POST to the php script and it will receive the data and can act on it.
However, this is only a development option for the php script since the logging is from an automated unattended service.

Questions:
1) How can I send a call to the php script just like if it came from a submitted form?

2) How do I send multi-line html text compared to single line text strings?

3) Can I use Indy10 since that is already integrated into the remote system application?

Grateful for any insights/suggestions/weblinks on this issue!

delphius:

--- Quote from: BosseB on December 20, 2023, 06:28:44 pm ---The purpose of emailing is as a log transfer/sharing function but it has been difficult maintaining the email connections when services become more and more locked down on the Internet...

--- End quote ---

If something works well, then just don't touch it.

What exactly is the problem with sending logs via email right now?

Why was it decided to reinvent the wheel, and not just finish sending by mail?

BosseB:

--- Quote from: delphius on December 20, 2023, 07:59:35 pm ---
--- Quote from: BosseB on December 20, 2023, 06:28:44 pm ---The purpose of emailing is as a log transfer/sharing function but it has been difficult maintaining the email connections when services become more and more locked down on the Internet...

--- End quote ---

If something works well, then just don't touch it.

--- End quote ---
Well the whole point is that it is NOT working well. Lately several disruption events...

--- Quote ---What exactly is the problem with sending logs via email right now?

--- End quote ---
The emails don't reach their destinations...

--- Quote ---Why was it decided to reinvent the wheel, and not just finish sending by mail?

--- End quote ---
We have experienced several sudden stops in email delivery caused by mail servers like GMail.
Those previous times the emails were server side stuffed into a spam folder for unknown reasons and every user was having to reconfigure GMail to allow these emails to pass through.
Now, after the last stop it is no longer the case that they go into spam, they just disappear...

Being fed up by this I am trying to use another route by letting the mail server on the web-server ISP do the sending via a local php script....

And before I set out to create that script I want to check that I can actually feed it the data, which is extracted by the existing application.
Hence the question on how to POST stuff like this using Indy10 like a web browser would when the user hits Submit.

Also I have stated POST rather than GET in order to protect the actual command from spoofing.
I have other scripts on the web server which sends report emails but these use GET and the info is minimal and can be sent using a simple URL with the added data.
I rather want to use POST but don't know how.

Leledumbo:

--- Quote from: BosseB on December 20, 2023, 06:28:44 pm ---1) How can I send a call to the php script just like if it came from a submitted form?

--- End quote ---
Just send a HTTP POST of enctype application/x-www-form-urlencoded.

--- Quote from: BosseB on December 20, 2023, 06:28:44 pm ---2) How do I send multi-line html text compared to single line text strings?

--- End quote ---
To HTTP, both are the same, just encode line endings properly.

--- Quote from: BosseB on December 20, 2023, 06:28:44 pm ---3) Can I use Indy10 since that is already integrated into the remote system application?

--- End quote ---
Now since this thing can be considered ancient, it depends. AFAIK it's capable of sending HTTP POST, but lacking in the secure version (SSL / TLS). So, if your PHP side is protected, the request might not come through.

For a reference, this is how you would do it with fphttpclient:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$H+} uses  Classes,  SysUtils,  sslsockets,  fphttpclient; var  LReqBody: TStrings;  LResBody: TStringStream;begin  with TFPHTTPClient.Create(nil) do    try      LReqBody := TStringList.Create;      with LReqBody do begin        // both fieldN and valueN will be urlencode-d automatically since we call FormPost with TStrings as the request body        Values['field1'] := 'value1';        Values['field2'] := 'value2';        Values['field3'] := 'value3';      end;      LResBody := TStringStream.Create('');      FormPost('https://whatever.com/asyouwish',LReqBody,LResBody);      WriteLn('Response: ' + LResBody.DataString);    finally      LResBody.Free;      LReqBody.Free;      Free;    end;end. 

Remy Lebeau:

--- Quote from: BosseB on December 20, 2023, 06:28:44 pm ---1) How can I send a call to the php script just like if it came from a submitted form?

--- End quote ---

You can use the TIdHTTP.Post() method with a TStringList holding name=value pairs. That will send an HTTP request in the standard application/x-www-form-urlencoded format, which PHP can read using $_POST, eg:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  PostData: TStringList;begin  PostData := TStringList.Create;  try    PostData.Add('To=' + ...);    PostData.Add('From=' + ...);    PostData.Add('Message=' + ...);    IdHTTP1.Post(url, PostData);  finally    PostData.Free;  end;end; 

--- Quote from: BosseB on December 20, 2023, 06:28:44 pm ---2) How do I send multi-line html text compared to single line text strings?

--- End quote ---

The exact same way as above.  Simply put the entire multi-line text into the TStringList as a single string, including any embedded line breaks.  They will be encoded accordingly.


--- Quote from: BosseB on December 20, 2023, 06:28:44 pm ---3) Can I use Indy10 since that is already integrated into the remote system application?

--- End quote ---

Yes.

Navigation

[0] Message Index

[#] Next page

Go to full version