Avatar of Mike Tew
Mike Tew
Flag for Canada asked on

PDF attachment error with wp_mail

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\?

// get file info

$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

WordPressPHPWeb Development

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Mike Tew

Hi Ray,
It may come to that. I was hoping to use the temp directory so its gone as soon as the session is done. I have had some success today with a template in code canyon. However, jquery accept: "application/pdf" is breaking the jquery validate plugin.

I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
Ray Paseur

The author is contemplating a risky and technically incompetent approach to document delivery.  A better strategy with a code example is give here:
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck