Recent

Author Topic: Send email with attachments  (Read 3735 times)

Przemyslav

  • New Member
  • *
  • Posts: 44
Send email with attachments
« on: August 22, 2018, 03:23:18 pm »
Hello everybody ;) I have big problem with sending email with attachment. My example program you can see in attachment.

When i close my program with Button, program saves all texts from ComboBoxes, Edits, Memos to file called report.txt. File report.txt in his name contains date and time when file was generated for example: 22.08.2018_15.02.20_report.txt. I can generate multiple report files in one hour. I have problem with add last generate file to email.

My code for button:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn1Click(Sender: TObject);
  2. const
  3.   OutputFileName = 'report.txt';
  4. var
  5.   OutputFile: TStringList;
  6.   time: TFormatSettings;
  7.   Mail: TSendMail;
  8. begin
  9.   GetLocaleFormatSettings(1045, czas);
  10.   time.TimeSeparator := '.';
  11.  
  12.   if (Application.MessageBox('Allert!', 'Raport', MB_ICONQUESTION + MB_YESNO) <> IDYES) then Exit;
  13.  
  14.   OutputFile := TStringList.Create;
  15.  
  16.   OutputFile.Add('');
  17.   OutputFile.Add('-----------------------------------------------------------------------------------------------------------------');
  18.   OutputFile.Add('');
  19.   OutputFile.Add('Dyżur pełnili:');
  20.   (......)
  21.   OutputFile.SaveToFile('reports\\' + DateToStr(Now, time) + '_' + TimeToStr(Now, time) + '_' + OutputFileName);
  22.  
  23.   Mail := TSendMail.Create;
  24.  
  25.   try
  26.       Mail.Sender := '*************' <'*************'>;
  27.       Mail.Receivers.Add('*************');
  28.       Mail.Subject := 'Hello';
  29.       Mail.Message.Add('How are you');
  30.       Mail.Attachments.Add('reports\\report.txt');  // for send files, optional
  31.       Mail.Smtp.UserName := '*************';
  32.       Mail.Smtp.Password := '*************';
  33.       Mail.Smtp.Host := 'smtp.gmail.com';
  34.       Mail.Smtp.Port := '587';
  35.       //Mail.Smtp.SSL := True;
  36.       mail.Smtp.FullSSL:=false;
  37.       Mail.Smtp.TLS := True;
  38.       Mail.Send;
  39.       ShowMessage('SUCCESS :) ');
  40.     finally
  41.       Mail.Free;
  42.     end;
  43.  
  44. end;
   

I created file in this line:

Code: Pascal  [Select][+][-]
  1. OutputFile.SaveToFile('reports\\' + DateToStr(Now, time) + '_' + TimeToStr(Now, time) + '_' + OutputFileName);
       

And i have problem to select the last generated file from line above iin this section:

Code: Pascal  [Select][+][-]
  1.  Mail.Attachments.Add('reports\\report.txt');  // for send files, optional    
« Last Edit: August 22, 2018, 03:39:11 pm by Przemyslav »

fred

  • Full Member
  • ***
  • Posts: 202
Re: Send email with attachments
« Reply #1 on: August 22, 2018, 03:47:15 pm »
Put the filename in a var and use it for both cases:

Code: Pascal  [Select][+][-]
  1. filename := 'reports\\' + DateToStr(Now, time) + '_' + TimeToStr(Now, time) + '_' + OutputFileName;
  2.  
  3. OutputFile.SaveToFile(filename);
  4.  
  5. Mail.Attachments.Add(filename);  // for send files, optional

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Send email with attachments
« Reply #2 on: August 22, 2018, 03:48:47 pm »
hi,
i would also separate the view / GUI layer and the send email functionality.
in model - view - controller this separation is the norm.
has advantages.
Lazarus 2.0.2 64b on Debian LXDE 10

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: Send email with attachments
« Reply #3 on: August 22, 2018, 04:00:45 pm »
I put this code in var:

Code: Pascal  [Select][+][-]
  1. const
  2.   OutputFileName = 'report.txt';
  3. var
  4.   OutputFile: TStringList;
  5.   time: TFormatSettings;
  6.   Mail: TSendMail;
  7.   filename: string;
  8.  
  9. begin
  10.   ....
  11.   filename := 'reports\\' + DateToStr(Now, time) + '_' + TimeToStr(Now, time) + '_' + OutputFileName;
  12.   OutputFile.SaveToFile(filename);
  13.  
  14.   Mail := TSendMail.Create;
  15.  
  16.   try
  17.      ....
  18. Mail.Attachments.Add(filename);  // for send files, optional
  19.     finally
  20.       Mail.Free;
  21.     end;
  22.  
  23. end;  
  24.  
  25.  
  26.  
  27.  

But i have errors:

Code: Pascal  [Select][+][-]
  1. unit1.pas(372,48) Error: Incompatible type for arg no. 2: Got "TDateTime", expected "TFormatSettings"
  2. unit1.pas(372,77) Error: Incompatible type for arg no. 2: Got "TDateTime", expected "TFormatSettings"
« Last Edit: August 22, 2018, 04:02:53 pm by Przemyslav »

MichaelBM

  • New Member
  • *
  • Posts: 38
Re: Send email with attachments
« Reply #4 on: August 22, 2018, 04:35:24 pm »
DateToStr(Now, time)

"time" is not valid.

See https://www.freepascal.org/docs-html/rtl/sysutils/datetostr.html
« Last Edit: August 22, 2018, 04:37:28 pm by MichaelBM »
"Everything must be made as simple as possible. But not simpler.”

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: Send email with attachments
« Reply #5 on: August 22, 2018, 05:15:10 pm »
OK thank you :) it's working now :) Thank you very much :)

 

TinyPortal © 2005-2018