Search code examples
phpemailtemporary-files

Create and mail temporary pdf in php


When clicking the button a pdf is generated and opens the pdf in the web browser. I would like for the pdf to be stored temporary in a variable and included in mail as attachment when the buttons i clicked.

I'm struggling with how to save and attach the file.

<a class="button"  href="http://mydomian.com/pdf001.php">Send PDF</a>
<?php
     $to      = '[email protected]';
     $subject = 'the subject';
     $message = 'hello';
     $headers = 'From: [email protected]' . "\r\n" .
     'Reply-To: [email protected]' . "\r\n" .
     'X-Mailer: PHP/' . phpversion();
     mail($to, $subject, $message, $headers);
?>

Solution

  • Like Comment under your original post say

    Use PHPMailer

    <?php
    require_once('../class.phpmailer.php');
    
    $mail             = new PHPMailer(); // defaults to using php "mail()"
    
    $mail->AddReplyTo("[email protected]","First Last");
    
    $mail->SetFrom('[email protected]', 'First Last');
    
    $mail->AddReplyTo("[email protected]","First Last");
    
    $address = "[email protected]";
    $mail->AddAddress($address, "John Doe");
    
    $mail->Subject    = "Php send pdf test";
    
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
    $mail->MsgHTML($body);
    
    $mail->AttachFromString (base64_encode(file_get_contents($urlToPdf)));      // attachment
    // in your case it would be http://mydomian.com/pdf001.php
    
    
    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }
    ?>
    

    I edited this code from online real quick and think that could work. with your tweaking for each variable.