Question
“Is is possible to automatically email each group to a separate recipient?”
Solution
Starting with ReportBuilder 12, the Email Groups feature was added allowing a user to automatically export and send each group in a report to a separate recipient. Below are steps for utilizing this feature.
1. First create a report and open the groups dialog by selecting the Report | Groups main menu option.
2. Create a new group and select the option to “Email Group”. Now when you export this report (via printing) you will export and send each group as a separate file.
3. Now that we are going to email each group, we need to set up the email settings for each file being sent. This can be done in two ways: using the Group.OnGetEmailSettings event or using the Group.GroupFileSettings.EmailField property. The EmailField property can be set to a datafield in the connected dataset containing the email address(s) for each recipient. If the email addresses are elsewhere manual control over the EmailSettings for each report can be taken by implementing the OnGetEmailSettings event in Delphi or RAP. This event passes the TppEmailSettings object that will be used for the specific group report being sent. Use the TppEmailSettings properties to customize the email for each recipient.
4. Finally export the report as you would normally by selecting the print button and choosing to Print To File from the print dialog or printing manually in code.
Notes:
– Emailing groups is not supported from the email button in the Preview or from the TppEmail object (Report.SendMail).
– It is highly recommended that you use direct-connect email plugin such as Indy or Synapse to email groups.
Download: EmailGroupsDemo.zip
Sample Delphi code:
procedure TForm1.Button2Click(Sender: TObject);
begin
FEmailPos := 0;
ppReport1.AllowPrintToFile := True;
InitEmailSettings;
ppReport1.DeviceType := 'PDF';
ppReport1.Print;
end;
procedure TForm1.CreateEmailList;
begin
FEmailList := TStringList.Create;
FEmailList.Add('email1@someserver.com');
FEmailList.Add('email2@someserver.com');
FEmailList.Add('email3@someserver.com');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateEmailList;
end;
procedure TForm1.InitEmailSettings;
begin
ppReport1.EmailSettings.HostAddress := '';
ppReport1.EmailSettings.UserName := '';
ppReport1.EmailSettings.Password := '';
ppReport1.TextFileName := ExtractFilePath(Application.ExeName) + 'Invoice.pdf';
end;
procedure TForm1.ppGroup1GetEmailSettings(Sender, aEmailSettings: TObject);
var
lEmailSettings: TppEmailSettings;
begin
lEmailSettings := TppEmailSettings(aEmailSettings);
lEmailSettings.EmailSettings.Recipients.Clear;
lEmailSettings.EmailSettings.Recipients.Add(FEmailList[FEmailPos]);
//lEmailSettings.EmailSettings.Subject :=
lEmailSettings.EmailSettings.Body.Text := 'Pay up!!';
Inc(FEmailPos);
end;