0

I have a problem using the DOMPDF library to generate a pdf and at the same time send it by mail.

What I need is that the moment you click on a button in html is generated and sent the pdf file at the same time; So far only send me the mail with a pdf already generated, but I need the pdf to be generated and sent. The data is collected first in a database and then the pdf is generated with the most recent record in the database.

I hope someone can help me.

Thank you

Note: without using frameworks

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
SmithBit
  • 41
  • 1
  • 4

2 Answers2

0

You'll have to use either the built-in SMTP functions of PHP or use a library such as PHPMailer (not a framework)

Once your PDF is generated, save it somewhere on the server, hold on to the full path to the PDF file and call a mail script to send the mail with an attachment (the attachment being the PDF)

Using PHP Mailer it could look something like this:

$mail = new PHPMailer;
$mail->addAddress('your_email@example.com');
$mail->addAttachment('/path/to/your/file.pdf', 'name as shown in the email.pdf');
$mail->send();

Of course you'd have to configure your mail with the host from where it will be sent, authenticate etc... but that wasn't what you asked for.

Just replace /path/to/your/file.pdf with whatever the path is + filename of your PDF file (on the server) and replace name as shown in the email.pdf with the name you want the receiver of the email to see, for example 'invoice.pdf'

Florian Humblot
  • 1,121
  • 11
  • 29
0

Try this

<?php
define('DBHOST','localhost');
define('DBNAME','user');
define('DBUSER','root');
define('PASS','');
$dbh = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME,DBUSER,PASS);
$q_sel="SELECT name,email from user_details";
$stmt_query=$dbh->prepare($q_sel);
$user_detail_exe=$stmt_query->execute();
$user_details=$stmt_query->fetchAll();
?>
<html>
    <head>
        <title>
            Send Email Generating Dompdf
        </title>
    </head>
    <body>
        <form  action="send_email.php">
            <input type="submit" value="EMAIL"  />
            <table  style="width:100%" border="1">
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
                <?php 
                foreach ($user_details as $user_detail)
                {
                ?>
                    <tr>
                        <td><?php echo $user_detail['name'];?></td>
                        <td><?php echo $user_detail['email'];?></td>            
                    </tr>
                <?php   
                }
                ?>
            </table>
        </form>
    </body>
</html>

send_email.php

<?php 
$name='user_detaiil.pdf';
require_once 'dompdf/dompdf_config.inc.php';
require_once 'Swift/lib/swift_required.php'; 
define('DBHOST','localhost');
define('DBNAME','user');
define('DBUSER','root');
define('PASS','');
$dbh = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME,DBUSER,PASS);
$q_sel="SELECT name,email from user_details";
$stmt_query=$dbh->prepare($q_sel);
$user_detail_exe=$stmt_query->execute();
$user_details=$stmt_query->fetchAll();
$pdf_data='<html>
            <body>
                <table style="width:100%" border="1">
                    <tr>
                    <th>Name</th>
                    <th>Email</th>
                    </tr>';
                    foreach ($user_details as $user_detail)
                    {

                        $pdf_data.='<tr>
                            <td>'.$user_detail['name'].'</td>
                            <td>'.$user_detail['email'].'</td>          
                        </tr>';
                    }

                $pdf_data.='</table>
            </body>
        </html>';


    $dompdf=new DOMPDF();
    $dompdf->load_html($pdf_data);
    $dompdf->render();
    $canvas =$dompdf->get_canvas();
    $date = date("d/m/Y");
    $canvas->page_text(10, 770,"Date: $date" , $font, 12);
    $canvas->page_text(520, 770, "Page: {PAGE_NUM}", $font, 12);
    $output = $dompdf->output();
    $file_to_save = 'reports/user_detail.pdf';
    file_put_contents($file_to_save, $output);
    //$dompdf->stream($name);
    ob_start();
    $HasFunctions = true;
    $SendsMail = true;

    $message = Swift_Message::newInstance()

    ->setSubject('User Detail Report')

    ->setFrom(array('test@gmail.com'))

    ->setTo(array('test@gmail.com'))

    ->setBody($messagetext, 'text/html');

    $message->attach(Swift_Attachment::fromPath("reports/user_detail.pdf"));

    $transport = Swift_SmtpTransport::newInstance("smtp.fake.com", 25)
    ->setUsername("Username")
    ->setPassword("Password");

    $mailer = Swift_Mailer::newInstance($transport);

    $result = $mailer->send($message);

    unlink("reports/user_detail.pdf");
    if($result)
    {
        header("LOCATION:dompdf.php");
    }
?>
Ninad
  • 63
  • 1
  • 3
  • 15