0

I want to attach a file to an email. but the file fetches from a dynamic view. Is it possible?

Here is the code I have made in phpmailer.

  $url = 'http://somewebsite.com/parameter';
  $binary_content = file_get_contents($url);
  if ($binary_content === false) {
    throw new Exception("Could not fetch remote content from: '$url'");
  }
  $mail->AddStringAttachment($binary_content, "test.pdf", $encoding = 'base64', $type = 'application/pdf');

The code above successfully attached in the email. But the pdf file cannot be opened or an error.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

You have to use base64encode for attachements content here.

$url = 'http://somewebsite.com/parameter';
$binary_content = file_get_contents($url);
if ($binary_content === false) {
    throw new Exception("Could not fetch remote content from: '$url'");
}
$file_content = base64encode($binary_content);
$mail->AddStringAttachment($file_content, "test.pdf", $encoding = 'base64', $type = 'application/pdf');
zajonc
  • 1,935
  • 5
  • 20
  • 25