0

I need some help in my php code. My reports are done in eclipse BIRT giving them a .rptdesign file extension and I want to send it through gmail. I am successful in sending but the images are broken and the CSS is not applied. Here is my code

<html>
    <body>
    <hr>
        <form action="PhpRunAndRenderHTMLEmbed.php" method="POST">
            <input type="submit" name="submit" value="SEND">
        </form>
    <hr>
    </body>
</html>
<?php

if (!(get_cfg_var('java.web_inf_dir'))) {
  define ("JAVA_HOSTS", "127.0.0.1:8080");
  define ("JAVA_SERVLET", "/JavaBridge/JavaBridge.phpjavabridge");
}
$pth = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];
$path_parts = pathinfo($pth);
$imageURLPrefix = $path_parts['dirname'] ."/sessionChartImages/";
require_once("java/Java.inc");

session_start(); 

$here = getcwd();

$ctx = java_context()->getServletContext();
$birtReportEngine =        java("org.eclipse.birt.php.birtengine.BirtEngine")->getBirtEngine($ctx);
java_context()->onShutdown(java("org.eclipse.birt.php.birtengine.BirtEngine")->getShutdownHook());


try{


$report = $birtReportEngine->openReportDesign("${here}/TopNPercent.rptdesign");
$task = $birtReportEngine->createRunAndRenderTask($report);
$taskOptions = new java("org.eclipse.birt.report.engine.api.HTMLRenderOption");
$outputStream = new java("java.io.ByteArrayOutputStream");
$taskOptions->setOutputStream($outputStream);
$taskOptions->setEmbeddable(true);
$taskOptions->setOutputFormat("html");
$ih = new java( "org.eclipse.birt.report.engine.api.HTMLServerImageHandler");
$taskOptions->setImageHandler($ih);
$taskOptions->setEnableAgentStyleEngine(true);
$taskOptions->setBaseImageURL($imageURLPrefix . session_id());
$taskOptions->setImageDirectory($here . "/sessionChartImages/" . session_id());

$task->setRenderOption( $taskOptions );
$task->run();
$task->close();

} catch (JavaException $e) {
    echo $e; //"Error Calling BIRT";

}
echo $outputStream;

if(isset($_POST['submit'])){
    require 'PHPMailer/PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $mail->isSMTP();                                   // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';                    // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                            // Enable SMTP authentication
    $mail->Username = 'sample@gmail.com';          // SMTP username
    $mail->Password = 'password'; // SMTP password
    $mail->SMTPSecure = 'tls';                         // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                 // TCP port to connect to

    $mail->setFrom('sample@gmail.com', 'sample');
    $mail->addReplyTo('sample@gmail.com', 'sample');
    $mail->addAddress('sample@gmail.com');   // Add a recipient

    $mail->isHTML(true);  // Set email format to HTML


    $bodyContent = '<p>This is the HTML email sent from localhost using PHP</p>';

    $src = 'C:\xampp\htdocs\JavaBridge\sessionChartImages\6th1i2ih8j1r83o21iidi1qte0\custom6ab9ab811570c80985d2.png';
    $mail->AddEmbeddedImage($src, 'chart_image', 'custom6ab9ab811570c80985d2.png');

    $mail->Subject = 'BIRT PHP Email';
    $bodyContent .= $outputStream;
    $mail->Body = $bodyContent;


    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
}


?>

What should I do?

2 Answers2

0

The sending process doesn't have anything do to with CSS - that's entirely up to the client, though bear in mind that CSS support in email clients is extremely variable, and you can't use external style sheets. It's hard to tell because you've not shown what's in your message body.

When you say the image is broken, what do you mean?

To reference an embedded image you need to refer to its cid, like:

<img src="cid:chart_image" />
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • the image is not showing. It is only showing the broken image icon. Since JavaBridge has a sample PhpRunAndRenderHTMLEmbed.php file it renders the whole report as an HTML and displays it in a browser. But when I try to send an email containing the rendered report the image are not shown – Juan Carlos Cabotaje Sep 09 '16 at 06:47
0

It makes much more sense to send a graphical report as an attachment in the PDF format.

hvb
  • 2,484
  • 1
  • 10
  • 13