There is an ability to send a report by Email in the viewer. To activate this capability, you should set the showSendEmailButton event of the viewer in the true value and add the onEmailReport event handler.

 

viewer.php

 

<?php

$options = new \Stimulsoft\Viewer\StiViewerOptions();

$options->toolbar->showSendEmailButton = true;

 

$viewer = new \Stimulsoft\Viewer\StiViewer($options);

$viewer->onEmailReport = true;

$viewer->renderHtml();

?>

 

 

 

If necessary, you may add a call to a JavaScript event, in the arguments of which you can find the required data for sending an email, get the type of report export, get the report itself, get the report export settings and change them. You can find a detailed description of available argument values in the Viewer Events chapter.

 

When sending a report by Email the menu of attachment format selection, which corresponds to the menu of report export selection is displayed. When you select a format, the dialog of input sending parameters will be displayed, among them: email receiver, theme, and text of a Email. After the sending is confirmed the onEmailReport event mentioned above will be invoked, where you can check and correct data, input in this form:

 

viewer.php

 

<?php

$viewer = new \Stimulsoft\Viewer\StiViewer();

$viewer->onEmailReport = 'onEmailReport';

$viewer->renderHtml();

?>

 

function onEmailReport(args) {

args.settings.subject = "Invoice: " + args.settings.subject;

}

 

 

 

Pure JavaScript doesn't have the work of functions with Email. There are functions for this capability, intended for sending on the PHP server-side. To use them, you should add the invoke of event handler on the client-side in the onEmailReport event. Also you should define the onEmailReport event of the same name on the server-side in the file of event handler. You can find a detailed description in the PHP Events Handler chapter.

 

viewer.php

 

<?php

$viewer = new \Stimulsoft\Viewer\StiViewer();

$viewer->onEmailReport = true;

$viewer->renderHtml();

?>

 

 

handler.php

 

$handler->onEmailReport = function ($args) {

$format = $args->format;

$fileName = $args->fileName;

$settings = $args->settings;

 

return \Stimulsoft\StiResult::success();

};

 

 

 

To send Email on the PHP server-side, in arguments of the event you should transfer data, such as log in, account's password, from which the sending will be made and server settings: its address, port and other parameters. The settings property in arguments of the event is intended for this. It is an object of the StiEmailSettings class, which contains all necessary properties to set Email sending. You can find a detailed description of available settings in the Viewer Events chapter.

 

The sample of setting minimum required settings of the mail server to send a report.

 

handler.php

 

$handler->onEmailReport = function ($args) {

$args->settings->from = 'mail.sender@stimulsoft.com';

$args->settings->host = 'smtp.stimulsoft.com';

$args->settings->port = 456;

$args->settings->login = '********';

$args->settings->password = '********';

 

return \Stimulsoft\StiResult::success();

};

 

 

 

Sample of checking and changing theme of an Email before sending.

 

handler.php

 

$handler->onEmailReport = function ($args) {

if (strlen($args->settings->subject) == 0)

$args->settings->subject = $args->formatName . ' report '.$args->settings->attachmentName;

 

return \Stimulsoft\StiResult::success();

};

 

 

 

Sample of adding addresses of additional an Email receivers with a report.

 

handler.php

 

$handler->onEmailReport = function ($args) {

$args->settings->cc[] = 'extra_recipient_one@stimulsoft.com';

$args->settings->bcc[] = 'hidden_recipient_one@stimulsoft.com';

$args->settings->bcc[] = 'hidden_recipient_two@stimulsoft.com John Smith';

 

return \Stimulsoft\StiResult::success();

};

 

 

 

Email sending settings

The viewer allows you to set values by default for forms of Email sending. The defaultEmailAddress, defaultEmailSubject and defaultEmailMessage properties are intended for this.

 

viewer.php

 

<?php

$options = new \Stimulsoft\Viewer\StiViewerOptions();

$options->toolbar->showSendEmailButton = true;

 

$options->email->defaultEmailAddress = 'recipient_address@stimulsoft.com';

$options->email->defaultEmailSubject = 'New Invoice';

$options->email->defaultEmailMessage = 'Please check the new invoice in the attachment';

?>