Link to home
Start Free TrialLog in
Avatar of Mike Tew
Mike TewFlag 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:' . "\r\n";
$headers  .= "From:\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

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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.
Avatar of Rob
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
The author is contemplating a risky and technically incompetent approach to document delivery.  A better strategy with a code example is give here: