PHP Send to PDF Outlook Outlook

I am using fpdf to generate pdf from php. everything is good. but I want to send PDF to Outlook.

I use $pdf->Output('file.pdf','D');

it works. I want to bind this file to Outlook.

How can i do this?

+3


source to share


1 answer


Something you can do is save the file to a temporary location on the server and then use something like PHPMailer to attach that saved file in an email. PHPMailer is much easier to use for attachments than the built-in function mail

.

You can temporarily save a PDF file in several ways. Here's one:

$tempfilename = time().'.pdf';
$pdf->Output($tempfilename,'F');

      

Then in PHPMailer you can attach it as such:

$mail->addAttachment($tempfilename);

      



And after that, you can delete the temporary file from the server.

unlink($tempfilename);

      

If PHPMailer cannot be used for your situation for some reason, you can use the built-in function mail

. If you are working from a new file or a small file where the cost of adding PHPMailer is relatively low, do so if you can. Otherwise, you can try adding code like this to yours $headers

. Adapted from the answer to use mail

to send attachments
:

//  Generate a random hash to send mixed content
$sep = md5(time());

//  End of line
$eol = PHP_EOL;

//  Content of file
$content = file_get_contents($tempfilename);
$content = chunk_split(base64_encode($content));

//  Add attachment to headers
$headers .= "--" . $sep . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . $tempfilename . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol . $eol;
$headers .= $content . $eol . $eol;
$headers .= "--" . $sep . "--";

      

+1


source







All Articles