sending email not working lazarus / 21-General ---------------------------------------------------------------------- Awesome Programmer · 2024-06-12 18:21 my Lazarus compiler 2.0.0. Yes, Yes... I know it is sooooo old that I shouldn't use this version at all... lol... but it is working and compiling my code with NO ISSUES whatsoever. If I were to upgrade, it will break over 50% of all my components and LIBRARIES that means many of my codes/program will seize to compile forever. Just thought I'd let you all know, before you all jump on me for this... lol...  Anyways, I am trying to use the msmtp terminal command to send email messages. I have searched online for this command and found a lot of good information and samples. So, I was able to come up with a  command line that works flawlessly. It sends email and I can even send SMS text message to smartphone. ONLY through TERMINAL or CONSOLE. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i echo "Hello world" | msmtp --from=James@James.com 1234567899@tmomail.net However, this same commandline code WILL NOT execute from within Lazarus. No message box pops up or gives any hints or errors. I don't get SMS or EMAIL message either. The function is below. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function SendMail:Boolean;var sp:TProcess; L:Tstringlist;begin  l:=tstringlist.create;    sp:= Tprocess.Create(nil);    try      sp.options:=[poUsePipes];      sp.executable := 'echo';      sp.Parameters.Add('"Hello World"');      sp.Parameters.Add('|');      sp.Parameters.Add('msmpt --from=james@james.com 1234567899@tmomail.net');      sp.execute;      l.SaveToStream(sp.Input);      l.clear;      l.LoadfromStream(sp.output);      writeln(l.text);    finally      sp.free;      l.free;    end;    result:=true;end; Can someone tell me what is wrong with the above PASCAL CODE? ---------------------------------------------------------------------- TRon · 2024-06-12 18:24 Quote from: Awesome Programmer on June 12, 2024, 08:21:04 pmCan someone tell me what is wrong with the above PASCAL CODE? TProcess is not a shell replacement. ---------------------------------------------------------------------- Fibonacci · 2024-06-12 18:30 I guess you should run "msmtp", not "echo", and then send there your text with pipes ---------------------------------------------------------------------- MarkMLl · 2024-06-12 18:58 Quote from: Awesome Programmer on June 12, 2024, 08:21:04 pmCan someone tell me what is wrong with the above PASCAL CODE? Yes, RTFM: QuoteTProcess ... does not support complex pipelines as in Unix. If this behaviour is desired, the shell can be executed with the pipeline as the command it should execute. https://www.freepascal.org/docs-html/current/fcl/process/index.html Oh, and it's Pascal, not PASCAL. MarkMLl ---------------------------------------------------------------------- Awesome Programmer · 2024-06-12 20:08 Yes, RTFM: LOL... for your information, I always do my best to read and re-read TFM. Sometimes, things don't always work like they say in the BOOK... I learned this after coding for over 20 years. I came up with a workaround and it works; created a shell script file and then execute the script file from my Lazarus program. ---------------------------------------------------------------------- Remy Lebeau · 2024-06-12 20:10 Quote from: Awesome Programmer on June 12, 2024, 08:21:04 pmCan someone tell me what is wrong with the above PASCAL CODE? echo is not an executable that you can run directly.  It is a built-in command of the terminal itself.  You are using the terminal's pipe operator to send the output of echo to the STDIN stream of the msmpt executable.  So you need to do the same thing with TProcess, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function SendMail:Boolean;var  sp: TProcess;  L: TStringList;begin  L := TStringList.Create;  sp := TProcess.Create(nil);  try    sp.Options := [poUsePipes];    sp.Executable := 'msmtp';    sp.Parameters.Add('-- from=james@james.com');    sp.Parameters.Add('1234567899@tmomail.net');    sp.Execute;    L.Text := 'Hello World';    L.SaveToStream(sp.Input);    L.Clear;    L.LoadFromStream(sp.Output);    WriteLn(L.Text);  finally    sp.free;    L.free;  end;  Result := true;end; ---------------------------------------------------------------------- MarkMLl · 2024-06-12 20:28 Quote from: Awesome Programmer on June 12, 2024, 10:08:27 pmYes, RTFM: LOL... for your information, I always do my best to read and re-read TFM. Sometimes, things don't always work like they say in the BOOK... I learned this after coding for over 20 years. I came up with a workaround and it works; created a shell script file and then execute the script file from my Lazarus program.   Sorry :-) A shell script will work, but I've also used pipelines etc. to good effect by specifying the shell (sh, bash or WHY) explicitly. MarkMLl ---------------------------------------------------------------------- rvk · 2024-06-12 21:41 Quote from: Remy Lebeau on June 12, 2024, 10:10:58 pmQuote from: Awesome Programmer on June 12, 2024, 08:21:04 pmCan someone tell me what is wrong with the above PASCAL CODE? echo is not an executable that you can run directly.  It is a built-in command of the terminal itself.  Not an executable??? Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i pi@space01:~ $ whereis echoecho: /usr/bin/echo /usr/share/man/man1/echo.1.gzpi@space01:~ $ ls /usr/bin/echo -ltr- rwxr-xr-x 1 root root 35632 Sep 22  2020 /usr/bin/echo*pi@space01:~ $ file /usr/bin/echo/usr/bin/echo: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld- linux-aarch64.so.1, BuildID[sha1]=bbd46f97ea84f6e4971a0cff6a54ffa85435f1a4, for GNU/Linux 3.7.0, stripped  msmtp is a Linux command so I assume this is Linux and not Windows. Quote from: Remy Lebeau on June 12, 2024, 10:10:58 pmYou are using the terminal's pipe operator to send the output of echo to the STDIN stream of the msmpt executable. Yes, the problem in the original post is that the pipe and second command were given as parameter to the echo command. That doesn't work. You need a shell (bash, sh) to be able to use pipe like that. Or handle the input/output streams yourself. Running this complete line as parameter itself with bash as command might be easier. ---------------------------------------------------------------------- Remy Lebeau · 2024-06-12 22:01 Quote from: rvk on June 12, 2024, 11:41:08 pmQuote from: Remy Lebeau on June 12, 2024, 10:10:58 pmecho is not an executable that you can run directly.  It is a built-in command of the terminal itself.  Not an executable??? Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i pi@space01:~ $ whereis echoecho: /usr/bin/echo /usr/share/man/man1/echo.1.gzpi@space01:~ $ ls /usr/bin/echo -ltr- rwxr-xr-x 1 root root 35632 Sep 22  2020 /usr/bin/echo*pi@space01:~ $ file /usr/bin/echo/usr/bin/echo: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld- linux-aarch64.so.1, BuildID[sha1]=bbd46f97ea84f6e4971a0cff6a54ffa85435f1a4, for GNU/Linux 3.7.0, stripped  OK. It is an executable on some platforms, but not on others.  Doesn't change the rest of my earlier reply.  The code should not be executing echo but rather executing msmtp instead. ---------------------------------------------------------------------- MarkMLl · 2024-06-13 07:06 Quote from: Remy Lebeau on June 13, 2024, 12:01:56 amOK. It is an executable on some platforms, but not on others.  Doesn't change the rest of my earlier reply.  The code should not be executing echo but rather executing msmtp instead. Like time, echo exists as both a shell internal command and a binary. So you're both right, stop snarling at each other :-) MarkMLl ---------------------------------------------------------------------- Awesome Programmer · 2024-06-13 15:16 Quote from: Remy Lebeau on June 12, 2024, 10:10:58 pmQuote from: Awesome Programmer on June 12, 2024, 08:21:04 pmCan someone tell me what is wrong with the above PASCAL CODE? echo is not an executable that you can run directly.  It is a built-in command of the terminal itself.  You are using the terminal's pipe operator to send the output of echo to the STDIN stream of the msmpt executable.  So you need to do the same thing with TProcess, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function SendMail:Boolean;var  sp: TProcess;  L: TStringList;begin  L := TStringList.Create;  sp := TProcess.Create(nil);  try    sp.Options := [poUsePipes];    sp.Executable := 'msmpt';    sp.Parameters.Add('-- from=james@james.com');    sp.Parameters.Add('1234567899@tmomail.net');    sp.Execute;    L.Text := 'Hello World';    L.SaveToStream(sp.Input);    L.Clear;    L.LoadFromStream(sp.Output);    WriteLn(L.Text);  finally    sp.free;    L.free;  end;  Result := true;end; Thanks Remy for your reply. When I run your code, it hangs up on L.LoadFromStream(sp.Output);. The program doesn't freeze or crash. It is just sitting at that line for an output from the command, but there is NO output from the command. Command executes and exits without no output. So, I not sure why this is. ---------------------------------------------------------------------- Remy Lebeau · 2024-06-13 16:52 Quote from: Awesome Programmer on June 13, 2024, 05:16:11 pmWhen I run your code, it hangs up on L.LoadFromStream(sp.Output);. The program doesn't freeze or crash. It is just sitting at that line for an output from the command, but there is NO output from the command. Command executes and exits without no output. So, I not sure why this is. Does the original command line output anything to begin with? Try adding the poStdErrToOutPut flag in the Options. Maybe the send is failing and outputting an error that you are not receiving yet. ---------------------------------------------------------------------- rvk · 2024-06-13 17:20 I'm not familiar with TProcess.Option poUsePipes but shouldn't you first write the TProcess.Input to the stream before you do TProcess.Execute? (I don't think msmpt will wait for inout but just ends if the input is empty.) ---------------------------------------------------------------------- MarkMLl · 2024-06-13 17:33 I did this a few days ago, and have put a few Gb of text lines through the resulting streams: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     processObject := TProcess.Create(nil);    processObject.Executable := binPath + binName;    processObject.Options := [poUsePipes];    processObject.Execute;    Sleep(100);    if processObject.Running then begin      StatusBar := 'Starting...';      idleTimer1.Enabled := true    end else      FreeAndNil(processObject);        ...      FilterStreamToLines(ProcessObject.StdErr, lineStdErr, @stdErrLineCallback, true        , 0              );      FilterStreamToLines(ProcessObject.Output, lineOutput, @outputLineCallback, false        , 0              )  and Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure FilterStreamToLines(const inputStream: TInputPipeStream; var line: ansistring;                        callback: PerLineCallback; timestamp: boolean= false; device: integer= -1);var  ch: ansichar; begin{ TODO : Read more than one character per loop. }   while inputStream.NumBytesAvailable > 0 do begin    inputStream.Read(ch, 1);    case ch of...  So it does work reliably. MarkMLl ---------------------------------------------------------------------- rvk · 2024-06-13 17:49 But isn't that for output streams. Here the "hello world" is the inputstream filled AFTER Execute. And if the command sees there is no bytes in the inputstream it could quit directly. So my guess was that you first need to begin filling the inputstream before you do Execute. Unless the command really waits before any input (which isn't certain). ---------------------------------------------------------------------- MarkMLl · 2024-06-13 17:58 Yes, that is for output and works with a program that runs until it gets a sigint or sigterm. If a program is terminating immediately it sees no input and doesn't have an option to run until it sees an explicit EOF (i.e. pipe closure etc.) then it's obviously a problem. OTOH my experience with things like sendmail was that they ran until they got an explicit EOM, e.g.  .  as the first character of a line. MarkMLl ---------------------------------------------------------------------- Remy Lebeau · 2024-06-14 04:39 Quote from: rvk on June 13, 2024, 07:49:55 pmAnd if the command sees there is no bytes in the inputstream it could quit directly. So my guess was that you first need to begin filling the inputstream before you do Execute. Unless the command really waits before any input (which isn't certain). According to https://marlam.de/msmtp/msmtp.html: QuoteIn its default mode of operation, it reads a mail from standard input... So, it's safe to assume that it waits for the caller to enter input so it knows what to send. ---------------------------------------------------------------------- TRon · 2024-06-14 04:54 I am also not familiar with the details of such piping as shown by your example Remy but could it perhaps be that msmtp sits and (ultimately) waits for a terminating character such as CR/LF or EOF ? (I am unfamiliar with what the piped stream actually sends to the command) ---------------------------------------------------------------------- MarkMLl · 2024-06-14 06:35 Quote from: TRon on June 14, 2024, 06:54:47 amI am also not familiar with the details of such piping as shown by your example Remy but could it perhaps be that msmtp sits and (ultimately) waits for a terminating character such as CR/LF or EOF ? (I am unfamiliar with what the piped stream actually sends to the command) Or a dot at the start of a line, possibly by itself. That was "the UNIX way" in days of yore. MarkMLl ---------------------------------------------------------------------- rvk · 2024-06-14 08:57 Quote from: Awesome Programmer on June 13, 2024, 05:16:11 pmQuote from: Remy Lebeau on June 12, 2024, 10:10:58 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sp.Executable := 'msmpt'; When I run your code, it hangs up on L.LoadFromStream(sp.Output);. The program doesn't freeze or crash. It is just sitting at that line for an output from the command, but there is NO output from the command. Command executes and exits without no output. So, I not sure why this is. You did change the msmpt to msmtp, didn't you?  I did a quick test. There is no output so if you comment out the reading of the non-existing output, it will go through. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program mailtest;{$MODE OBJFPC}{$H+}uses classes, sysutils, process; const  frommail = 'me@meme.com';  tomail = 'me@meme.com'; function foo(): Boolean;var  sp: TProcess;  L: TStringList;begin  L := TStringList.Create;  sp := TProcess.Create(nil);  try    sp.Options := [poUsePipes];    sp.Executable := 'msmtp';    sp.Parameters.Add('-- from=' + frommail);    sp.Parameters.Add(tomail);    sp.Execute;    L.Text := 'Hello World';    L.SaveToStream(sp.Input);     {    L.Clear;    L.LoadFromStream(sp.Output);    WriteLn(L.Text);    }     writeln('Exit status: ', sp.exitstatus);    writeln('Exit code: ', sp.exitcode);   finally    sp.free;    L.free;  end;  Result := true;end;  begin  writeln('Start');  writeln('Date = ', {$i %DATE%});  writeln('FPC target = ', {$i %FPCTARGET%});  writeln('FPC target CPU = ', {$i %FPCTARGETCPU%});  writeln('FPC target OS = ', {$i %FPCTARGETOS%});  writeln('FPC version = ', {$i %FPCVERSION%});   foo(); end. QuoteStart Date = 2024/06/14 FPC target = aarch64 FPC target CPU = aarch64 FPC target OS = Linux FPC version = 3.3.1 Exit status: -1 Exit code: 0 ---------------------------------------------------------------------- Remy Lebeau · 2024-06-14 15:57 Quote from: rvk on June 14, 2024, 10:57:08 amQuote from: Remy Lebeau on June 12, 2024, 10:10:58 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sp.Executable := 'msmpt';You did change the msmpt to msmtp, didn't you?    thanks, I fixed it in my earlier example. ---------------------------------------------------------------------- Awesome Programmer · 2024-06-18 12:51 Sorry, I couldn't get back right away. That's what I end up doing. Commented out the output part of the code and it went straight through. What doesn't make sense is that after reading your comments about not receiving input characters or ending the inputstream with dots or CR/LF, well by the time the program reached the outputstream pause the email has already be sent and received. just thought i should let them no... Thank you for your answers and suggestions. ---------------------------------------------------------------------- MarkMLl · 2024-06-18 13:14 Quote from: Awesome Programmer on June 18, 2024, 02:51:43 pmWhat doesn't make sense is that after reading your comments about not receiving input characters or ending the inputstream with dots or CR/LF, well by the time the program reached the outputstream pause the email has already be sent and received. All that indicates is that the mailer is not actually waiting for a . terminator or for further input. I wouldn't generally expect something in that position to be concerned about the lifetime of the program that's called it, which is what your final fix changed. MarkMLl ---------------------------------------------------------------------- Remy Lebeau · 2024-06-18 18:20 Quote from: Awesome Programmer on June 18, 2024, 02:51:43 pmWhat doesn't make sense is that after reading your comments about not receiving input characters or ending the inputstream with dots or CR/LF, well by the time the program reached the outputstream pause the email has already be sent and received. It does make sense, if you take into account that when using poUsePipes, TProcess.Execute() closes its end of the pipes it creates before exiting back to the caller.  So, when msmtp has read in all of the data from the stdin pipe, and the pipe has been closed, it knows there is no more data to read in and can send what data it did read.