php Mail_MIME - Generating and Attaching PDF

1.7k Views Asked by At

I am trying to write a PHP script that will generate a PDF and email it. My PDF generator works perfectly as an independent URL, but for some reason when I try to make the script email the generated PFD, the received file can't be opened. Here is the code:

include_once('Mail.php');
include_once('Mail/mime.php');

$attachment = "cache/form.pdf";

// vvv This line seems to be where the breakdowns is vvv
file_put_contents( $attachment, file_get_contents( "http://www.mydomain.com/generator.php?arg1=$arg1&arg2=$arg2" ) );

$message = new Mail_mime();
$message->setTXTBody( $msg );
$message->setHTMLBody( "<html><body>$msg</body></html>" );
$message->addAttachment( $attachment );
$body = $message->get();

$extraheaders = array(  "From"      => $from,
            "Cc"        => $cc,
            "Subject"   => $sbj );

$mail = Mail::factory("mail");

$headers = $message->headers( $extraheaders );
$to = array(    "Jon Doe <[email protected]>",
        "Jane Doe <[email protected]>" );
$addresses = implode( ",", $to );

if( $mail->send($addresses, $headers, $body) )
    echo    "<p class=\"success\">Successfully Sent</p>";
else
    echo    "<p class=\"error\">Message Failed</p>";

unlink( $attachment );

The line that I marked does generate a PDF file in the cache folder, but it will not open, so that seems to be a problem. However, when I try to attach a PDF file already exists, I have the same problem. I have tried $message->addAttachment( $attachment, "Application/pdf" ); as well, and it does not seem to make a difference.

2

There are 2 best solutions below

0
On BEST ANSWER

I am pretty sure it must be an ini problem blocking file_get_contents(). However I came up with a better solution. I reworked the generator.php file and turned it in to a function definition. So I've got:

include_once('generator.php');
$attachment = "cache/form.pdf";
file_put_contents( $attachment, my_pdf_generator( $arg1, $arg2 ) );
...
$message->addAttachment( $attachment, "application/pdf" );

This way I do not need to write the file first. It works great (although I am still having slight issues with Outlook/Exchange Server, but I think that is a largely unrelated issue).

3
On

Typically web server directories should have write permissions locked down. This is probably why you are experiencing problems with file_put_contents('cache/form.pdf').

// A working example: you should be able to cut and paste, 
// assuming you are on linux.
$attachment = "/var/tmp/Magick++_tutorial.pdf";
file_put_contents($attachment, file_get_contents( 
     "http://www.imagemagick.org/Magick++/tutorial/Magick++_tutorial.pdf"));

Try changing where you are saving the pdf to a directory that allows write and read permissions to everyone. Also make sure this directory is not on your web server.

Also try changing the following three things

From

$message = new Mail_mime();

To

// you probably don't need this the default is
// $params['eol'] - Type of line end. Default is ""\r\n""
$message = new Mail_mime("\r\n");

From

$extraheaders = array(  
       "From"      => $from,
       "Cc"        => $cc,
       "Subject"   => $sbj,
     );

To

$extraheaders = array(  
        "From"      => $from,
        "Cc"        => $cc,
        "Subject"   => $sbj,
        'Content-Type' => 'text/html'
    );

From

$message->addAttachment($attachment);

To

// the default second argument is $c_type = 'application/octet-stream'
$isAttached = $message->addAttachment($attachment, 'aplication/pdf');
if ($isAttached !== true) {
    // an error occured
    echo $isAttached->getMessage();
}

And you always want to make sure that you call

$message->get();

before

$message->headers($extraheaders);

or the whole thing wont work