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:
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;