We help IT Professionals succeed at work.
Get Started

PDF attachment error with wp_mail

m2ew
m2ew asked
on
909 Views
Last Modified: 2016-05-28
I'm using wp_mail to send a PDF attachment through. However, every time I send the file it says there's an error or it corrupt when I download the attachment. Also, I'm trying to include a message with the attachment, but that isn't working either.

$upload_type is always equal to application/pdf

Am I missing and \r\n\?

<?php
// get file info
$upload_name=$_FILES["uploaded_file"]["name"];
$upload_type=$_FILES["uploaded_file"]["type"];
$upload_size=$_FILES["uploaded_file"]["size"];
$upload_temp=$_FILES["uploaded_file"]["tmp_name"];

$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);

$file = chunk_split(base64_encode($file));
$num = md5(time());

//Normal headers

$headers = 'Reply-To: '. $_POST["email"] . "\r\n";
$headers .= 'Bcc: miket@wastewiseproducts.com' . "\r\n";
$headers  .= "From: info@wastewiseproducts.com\r\n";
$headers  .= "MIME-Version: 1.0\r\n";
$headers  .= "Content-Type: multipart/mixed; ";
$headers  .= "boundary=".$num."\r\n";
$headers  .= "--{$num}\r\n";

// This two steps to help avoid spam

$headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";

// With message

$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\r\n";
$headers .= "--".$num."\r\n";

// Attachment headers

$headers  .= "Content-Type: ".$upload_type." ";
$headers  .= "name=\"".$upload_name."\"r\n";
$headers  .= "Content-Transfer-Encoding: base64\r\n";
$headers  .= "Content-Disposition: attachment; ";
$headers  .= "filename=\"".$upload_name."\"\r\n";
$headers  .= "".$file."\r\n";
$headers  .= "--".$num."--";

$message = "test";

if (wp_mail($to, $subject, $message, $headers)){
  echo "sent";
} else{
  echo "fail";
?>

Open in new window

Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE