tags:

views:

87

answers:

2

I am wanting to send a pdf file in php mail. I know the name and location of the file (an invoice printed in cup-pdf) and would like to sent this automatically in php when i click a button in my website. How can this be done? Thanks

+1  A: 

You should use one of the classes built for that, for example PEAR Mail. For a bit more explanation, assuming we use just a plain text UTF8 content with the PDF file.

  • uniqboundary should be replace by some sort of unique string
  • $fileType is the MIME type of your file

Here the code :

$headers = 'MIME-Version: 1.0'."\r\n"
    .'Content-Type: multipart/related; boundary="--uniqboundary"'."\r\n";

$body = '--uniqboundary."\r\n".
    .'Content-Type: text/plain; charset=utf-8'."\r\n"
    .'Content-Transfer-Encoding: 8bit'."\r\n\r\n"
    .$text
    .'--uniqboundary'."\r\n"
    .'Content-Type: '.$fileType.'; name="'.basename($filename).'"'."\r\n"
    .'Content-Transfer-Encoding: base64'."\r\n"
    .'Content-Disposition: attachment; filename="'.basename($filename).'"'."\r\n\r\n";

$lineSize = filesize($filename) + 1;
$f = fopen($filename, 'r');
$chunks[] = chunk_split(base64_encode(fread($f, $lineSize)));
fclose($f);

$body .= implode("\r\n", $chunks)
    .'--uniqboundary--'."\r\n";

mail($to, $subject, $body, $headers);

It should work.

Serty Oan
A: 

phpmailer is a good options for this, but is needed a smtp account to make it

shadow_of__soul