This example shows how to send a report via Email using
Stimulsoft Viewer for PHP with server-side email handling.
First, you need to include the Stimulsoft libraries using
Composer. This allows you to use all required classes for the viewer, report, and email functionality.
After including the Composer autoloader, import the required Stimulsoft classes using the use statement. These classes are used to work with the viewer, reports, email events, and result handling:
<?php
require_once '../vendor/autoload.php';
use Stimulsoft\Events\StiEmailEventArgs;
use Stimulsoft\Report\StiReport;
use Stimulsoft\StiResult;
use Stimulsoft\Viewer\StiViewer;
...
Next, create a
Viewer object and configure JavaScript-related options. This includes setting the relative path to scripts and adding additional HTML elements, such as a favicon:
$viewer = new StiViewer();
$viewer->javascript->relativePath = '../';
$viewer->javascript->appendHead('<link rel="shortcut icon" href="/../favicon.ico" type="image/x-icon">');
Then, configure the viewer toolbar to display the
Send Email button. This allows users to send the currently displayed report by email directly from the viewer interface:
$viewer->options->toolbar->showSendEmailButton = true;
Next, you can define default values for the email form. These values will be shown to the user when the email dialog is opened, but they can be changed before sending:
$viewer->options->email->defaultEmailAddress = 'mail.recipient@stimulsoft.com';
$viewer->options->email->defaultEmailSubject = 'Default subject';
$viewer->options->email->defaultEmailMessage = 'Default message for email body.';
After that, define the
onEmailReport event. This event is executed on the server-side and is responsible for configuring SMTP settings and sending the email. Sensitive data such as login and password are not passed to the client side:
$viewer->onEmailReport = function (StiEmailEventArgs $args) {
$args->settings->from = 'mail.sender@stimulsoft.com';
$args->settings->host = 'smtp.stimulsoft.com';
$args->settings->login = '********';
$args->settings->password = '********';
// Optional parameters
//$args->settings->name = 'John Smith';
//$args->settings->port = 465;
//$args->settings->secure = 'ssl';
//$args->settings->cc[] = 'copy1@stimulsoft.com';
//$args->settings->bcc[] = 'copy2@stimulsoft.com';
return StiResult::getSuccess('The Email has been sent successfully.');
};
Next, process the incoming request. If the request is related to email sending or other viewer actions, it will be handled immediately on the server-side:
$viewer->process();
Then, create a
Report object and load a report file. The
loadFile() method does not load the report on the server-side; instead, it generates JavaScript code that loads the report on the client side:
$report = new StiReport();
$report->loadFile('../reports/SimpleList.mrt');
$viewer->report = $report;
Finally, render the viewer as a complete HTML page. The viewer will display the report and provide full interactive functionality, including the ability to send the report by email:
$viewer->printHtml();
As a result, the report is displayed directly in the browser, and users can send it by email using the built-in viewer interface, while all email configuration and SMTP credentials remain securely on the server-side.
Auf dem Screenshot unten Sie können das Ergebnis des Beispiel-Codes ansehen: