Question
“How do I email separate reports such as invoices to different addresses at the same time?”
Solution
Create a loop to continually update the email settings and search criterial of a report before sending an email for each record.
Download: SendMultipleEmails.zip
Sample Delphi code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
procedure TForm1.Button2Click(Sender: TObject); var liIndex: Integer; begin CreateCustomerList(); for liIndex := 0 to FCustomerList.Count - 1 do begin Query1.Close; Query1.SQL.Text := 'SELECT * FROM "customer.db" Customer ' + 'WHERE CustNo = ' + FCustomerList[liIndex]; Query1.Open; InitEmail(); ppReport1.SendMail; end; end; procedure TForm1.InitEmail; begin //Clear previous email settings. ppReport1.EmailSettings.Clear; //Note: The email addressed generated for this example are invalid. ppReport1.EmailSettings.Recipients.Add(Query1['Contact'] + '@' + Query1['Company'] + '.com'); ppReport1.EmailSettings.Subject := 'Invoice'; ppReport1.EmailSettings.FromAddress := 'me@mycompany.com'; ppReport1.EmailSettings.FromName := 'MyName'; ppReport1.EmailSettings.Body.Add('Dear ' + Query1['Contact'] + ',' + #13#10); ppReport1.EmailSettings.Body.Add('Attached is your current invoice. Please keep this for your records.'); ppReport1.EmailSettings.Body.Add('Thank you for your continued business.' + #13#10); ppReport1.EmailSettings.Body.Add('Sincerely,' + #13#10); ppReport1.EmailSettings.Body.Add('MyName, MyCompany'); ppReport1.EmailSettings.ShowEmailDialog := True; //Optional: Comment out to send all emails at once. end; procedure TForm1.CreateCustomerList; begin if FCustomerList = nil then FCustomerList := TStringList.Create; Query1.Open; Query1.First; while not(Query1.Eof) do begin FCustomerList.Add(IntToStr(Query1['CustNo'])); Query1.Next; end; Query1.First; Query1.Close; end; |