Creating and sending an e-Mail - on the fly lazarus / 62-General ---------------------------------------------------------------------- J-G · 2022-03-19 17:02 I suspect that this may be possible but with the plethora of e-mail clients available it may be rather more than simple! What I can do of course is create a 'string' which can be used as the body of an e-mail message  -   I also know there is a way to invoke the users default e-mail client with something like [ @mailto ] (though I'm not sure of the precice detail   -   what I would like to do is to pass the 'string' to the Client so that the user just needs to [Send]  - though automating the [send] would be even better  Anyone know  :  1   -   if any of this IS possible    2  -  which  dialogue or control  I need to use. ---------------------------------------------------------------------- Sieben · 2022-03-19 18:17 Sth along these lines? Nowadays Synapse is part of OPM afaik. ---------------------------------------------------------------------- mas steindorff · 2022-03-19 18:46 Quote from: Sieben on March 19, 2022, 07:17:40 pmSth along these lines? that document is 10 years old, is it valid? ---------------------------------------------------------------------- J-G · 2022-03-19 19:16 Quote from: Sieben on March 19, 2022, 07:17:40 pmSth along these lines? Nowadays Synapse is part of OPM afaik. Thanks for the response Sieben  -  I'd already found that Synapes document and considered it far too complex for the simple option that I want   -   even though I think I already have Synapse installed, well I see a tab entitled [SynEdit] in my list of controls I think I used that when I needed to send/receive files to/from my website. ---------------------------------------------------------------------- alpine · 2022-03-19 19:43 Quote from: J-G on March 19, 2022, 08:16:57 pmQuote from: Sieben on March 19, 2022, 07:17:40 pmSth along these lines? Nowadays Synapse is part of OPM afaik. Thanks for the response Sieben  -  I'd already found that Synapes document and considered it far too complex for the simple option that I want   -   even though I think I already have Synapse installed, well I see a tab entitled [SynEdit] in my list of controls I think I used that when I needed to send/receive files to/from my website. Not so complicated. Here is my small helper unit for Synapse: 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 unit mailsendu; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, smtpsend; type   { TMySMTPSend }   TMySMTPSend = class(TSMTPSend)  private    FSendSize: Integer;  protected    function SendToRaw(const AFrom, ATo: String; const AMailData: TStrings)      : Boolean;  public    function SendMessage(AFrom, ATo, ASubject: String; AContent,      AAttachments: TStrings): Boolean;     property SendSize: Integer read FSendSize write FSendSize;   end; implementation uses  ssl_openssl, mimemess, mimepart, synautil, synachar; { TMySMTPSend } function TMySMTPSend.SendToRaw(const AFrom, ATo: String; const AMailData  : TStrings): Boolean;var  S, T: String;begin  Result := False;  if Self.Login then  begin    FSendSize := Length(AMailData.Text);    if Self.MailFrom(GetEmailAddr(AFrom), FSendSize) then    begin      S := ATo;      repeat        T := GetEmailAddr(Trim(FetchEx(S, ',', '"')));        if T <> '' then        Result := Self.MailTo(T);        if not Result then          Break;      until S = '';      if Result then        Result := Self.MailData(AMailData);    end;    Self.Logout;  end;end; function TMySMTPSend.SendMessage(AFrom, ATo, ASubject: String; AContent,  AAttachments: TStrings): Boolean;var  Mime: TMimeMess;  P: TMimePart;  I: Integer;begin  Mime := TMimeMess.Create;  try    // Set some headers    Mime.Header.CharsetCode := UTF_8;    Mime.Header.ToList.Text := ATo;    Mime.Header.Subject := ASubject;  Mime.Header.From := AFrom;     // Create a MultiPart part    P := Mime.AddPartMultipart('mixed', Nil);     // Add as first part the mail text    Mime.AddPartTextEx(AContent, P, UTF_8, True, ME_8BIT);     // Add all attachments:    if Assigned(AAttachments) then      for I := 0 to Pred(AAttachments.Count) do        Mime.AddPartBinaryFromFile(AAttachments[I], P);     // Compose message    Mime.EncodeMessage;     // Send using SendToRaw    Result := Self.SendToRaw(AFrom, ATo, Mime.Lines);   finally    Mime.Free;  end;end; end.  And then (with SSL and attachments): 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 mailsend_test; uses  Classes, SysUtils, mailsendu, blcksock; type   { TSink }   TSink = class(TObject)    procedure Progress(Sender: TObject; Reason: THookSocketReason;const Value: String);  end; var  Content, Attach: TStringList;  SMTP: TMySMTPSend;  Sink: TSink;  Written: Integer; { TSink } procedure TSink.Progress(Sender: TObject; Reason: THookSocketReason;  const Value: String);begin  case Reason  of    {:Socket connected to IP and Port. Connected IP and Port is in parameter in     format like: 'localhost.somewhere.com:25'.}    HR_Connect: Written := 0;    {:report count of bytes writed to socket. Number is in parameter string. If     you need is in integer, you must use StrToInt function!}    HR_WriteCount:      begin        Written := Written + StrToInt(Value);        WriteLn('Written ', Written, ' of ', SMTP.SendSize, ' bytes');      end;    {:report situation where communication error occured. When raiseexcept is     @true, then exception is called after this Hook reason.}    HR_Error: WriteLn('Error: ', Value);  end;end; begin  Sink := TSink.Create;  Content := TStringList.Create;  Content.Add('Hello!');  Content.Add('This is a SMTP send test.');  Content.Add('Hello from the other side!');  Content.Add('Regards,');  Attach := TStringList.Create;  Attach.Add('mismisc.pas');  Attach.Add('notused.pas');   SMTP := TMySMTPSend.Create;  try    SMTP.TargetHost := 'smtp.googlemail.com';    SMTP.TargetPort := '465';    SMTP.Username := 'user@gmail.com';    SMTP.Password := 'password here';    SMTP.FullSSL := True;    SMTP.Sock.OnStatus := @Sink.Progress;    SMTP.Sock.RaiseExcept := True;    try      if SMTP.SendMessage(        'user@gmail.com', // AFrom        'recipient@domain.com', // ATo        'Test subject FullSSL 2', // ASubject        Content,        Attach)      then        WriteLn('Success.')      else      begin        WriteLn('Failure!');    end;    except      on E: Exception do        WriteLn('EXCEPTION: ', E.Message);    end;     with SMTP do    begin      WriteLn;      WriteLn('  ResultCode: ', ResultCode);       WriteLn('ResultString: ', ResultString);      WriteLn('  FullResult: ', FullResult.Text);      WriteLn('    AuthDone: ', AuthDone) ;    end;  finally    SMTP.Free;  end; end. ---------------------------------------------------------------------- J-G · 2022-03-19 23:29 Thanks for the input y.ivanov - I'll need to spend some time looking at your code to see if I can incorporate it - or part thereof -  but I suspect that it's way over the top for my current needs and I certainly will not have access the the users e-mail password which it seems is a pre-requisite. ---------------------------------------------------------------------- alpine · 2022-03-20 00:30 Quote from: J-G on March 20, 2022, 12:29:01 amThanks for the input y.ivanov - I'll need to spend some time looking at your code to see if I can incorporate it - or part thereof -  but I suspect that it's way over the top for my current needs and I certainly will not have access the the users e-mail password which it seems is a pre-requisite. That's the sender password (for smtp), not the recipient one. ---------------------------------------------------------------------- J-G · 2022-03-20 01:30 Quote from: y.ivanov on March 20, 2022, 01:30:29 am... That's the sender password (for smtp), not the recipient one. Exactly - I only know the address to whom the e-mail should be sent  -  Mine!  -  I could of course set up a dialogue for the user to complete which would include such password information, but my initial idea was to simply open the Users default e-mail client with a      OpenURL('mailto:'+acEmail.Text+'?subject=');    call. That particular code comes from my accounting package and is used to open my e-mail client with my customers address in place which I use to send out my invoices (attachments) It doesn't fill the body of the message, it just sets up the e-mail client with the address in place. What I would like is to have the message generated and automatically inserted into the body of the e-mail ---- and (ideally) automatically sent. EDIT :   I've just tried adding '?message='+Reg_text  -  this last being a string variable with the information I want sent to me. As usual my e-mail client is opened with the address & subject in place but nothing in the message body. ---------------------------------------------------------------------- HeavyUser · 2022-03-20 03:37 Quote from: J-G on March 20, 2022, 02:30:32 amQuote from: y.ivanov on March 20, 2022, 01:30:29 am... That's the sender password (for smtp), not the recipient one. Exactly - I only know the address to whom the e-mail should be sent  -  Mine!  -  I could of course set up a dialogue for the user to complete which would include such password information, but my initial idea was to simply open the Users default e-mail client with a      OpenURL('mailto:'+acEmail.Text+'?subject=');    call. That particular code comes from my accounting package and is used to open my e-mail client with my customers address in place which I use to send out my invoices (attachments) It doesn't fill the body of the message, it just sets up the e-mail client with the address in place. What I would like is to have the message generated and automatically inserted into the body of the e-mail ---- and (ideally) automatically sent. EDIT :   I've just tried adding '?message='+Reg_text  -  this last being a string variable with the information I want sent to me. As usual my e-mail client is opened with the address & subject in place but nothing in the message body. send email through shellexecute example at https://stackoverflow.com/questions/1962765/how-can-a-delphi-program- send-an-email-with-attachments-via-the-default-e-mail-c Its in the question part. I do not know how well it will serve you though. ---------------------------------------------------------------------- AlexTP · 2022-03-20 08:26 I see that the example is useful, and I stored it to the Wiki, https://wiki.freepascal.org/E-mail ---------------------------------------------------------------------- alpine · 2022-03-20 09:20 Quote from: J-G on March 20, 2022, 02:30:32 amQuote from: y.ivanov on March 20, 2022, 01:30:29 am... That's the sender password (for smtp), not the recipient one. Exactly - I only know the address to whom the e-mail should be sent  -  Mine!  -  I could of course set up a dialogue for the user to complete which would include such password information, but my initial idea was to simply open the Users default e-mail client with a      OpenURL('mailto:'+acEmail.Text+'?subject=');    call. That particular code comes from my accounting package and is used to open my e-mail client with my customers address in place which I use to send out my invoices (attachments) It doesn't fill the body of the message, it just sets up the e-mail client with the address in place. What I would like is to have the message generated and automatically inserted into the body of the e-mail ---- and (ideally) automatically sent. EDIT :   I've just tried adding '?message='+Reg_text  -  this last being a string variable with the information I want sent to me. As usual my e-mail client is opened with the address & subject in place but nothing in the message body. Maybe I'm missing something, but you always can sent e-mail on you own behalf, i.e. from your own e-mail account to yourself. If the body of the message is what matters ... ---------------------------------------------------------------------- J-G · 2022-03-20 12:24 Quote from: HeavyUser on March 20, 2022, 04:37:08 am ...send email through shellexecute example at https://stackoverflow.com/questions/1962765/how-can-a-delphi-program- send-an-email-with-attachments-via-the-default-e-mail-c Its in the question part. I do not know how well it will serve you though. It seved me very well indeed thanks  HeavyUser !! The 'key' is knowing what reserved words are needed  -  I'd tried   '?message='    yesterday and   '?body='   this morning but that example (though it goes much further with ShellExecute) gave me the  '&body='        I might have found it by trial and error but then again.....  For now I'm happy for the program to Open the users e-mail client with the message ready to send but I'll look further at ShellExecute soon. ---------------------------------------------------------------------- alpine · 2022-03-20 13:47 As long for the OpenURL (it calls ShellExecuteW internally), I'm using it that way: 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 OpenURL(  'mailto:mail@my-company-domain.com?' +      'subject=' + EncodeURLElement(        'Register ' + Application.Title + ', v' + GetFileVersion      ) + '&' +      'body=' + EncodeURLElement(        'Hello,' + LineEnding +        'This is a registration request for:' + LineEnding + LineEnding +        '>>> Application: ' + Application.Title + ', v' + GetFileVersion + LineEnding +        '>>> User Name: ' + edtUser.Text + LineEnding +        '>>>         Key: ' + edtKey.Text      )  ); EncodeURLElement() is from fphttpclient unit. It fills up the body successfuly at least in my Mozilla Thunderbird. ---------------------------------------------------------------------- Remy Lebeau · 2022-03-21 17:19 Quote from: Sieben on March 19, 2022, 07:17:40 pmNowadays Synapse is part of OPM afaik. So is Indy, which also has an SMTP client (TIdSMTP) and an email class (TIdMessage).