Unable to attach pdf to email in php -
i've tried follow examples given in forum, example given freestate here - error php mail(): .
below code copied revised example. emails fine. , attachment shown in email when received. cannot opened , 125k in size.
i have verified file size returned "$file_size = filesize($file);". "fopen" open stream. in fact before added multipart header, see content in body of text (albeit not readable).
i'm trying attach basic pdf file, on window's platform (windows 10), apache 2.4, php 5.6.16.0.
what cause attachment fail attach properly?
emailer('//servername/path/', 'documentname.pdf'); function emailer($path, $filename) { $eol = php_eol; $to = "xxxx@yyyyyyyy.com"; $subject = "file name: ".$filename; $body = "the written content of body"; $file = $path.$filename; $file_size = filesize($file); $handle = fopen($file, "r"); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $header = "from: nameofperson <their@emailaddress.com>".$eol. "mime-version: 1.0\r\n". "content-type: multipart/mixed; boundary=\"".$uid."\""; $message = "--".$uid.$eol. "content-type: text/html; charset=iso-8859-1".$eol. "content-transfer-encoding: 8bit".$eol.$eol. $body.$eol. "--".$uid.$eol. "content-type: application/pdf; name=\"".$filename."\"".$eol. "content-transfer-encoding: base64".$eol. "content-disposition: attachment; filename=\"".$filename."\"".$eol. $content.$eol. "--".$uid."--"; echo (mail($to, $subject, $message, $header))?'success':'failure'; }
try below code:
$name = "name"; $email = "email address"; $to = "$name <$email>"; $from = "xyz"; $subject = "test subject"; $mainmessage = "hi, here's file."; $fileatt = "./test.pdf"; $fileatttype = "application/pdf"; $fileattname = "newname.pdf"; $headers = "from: $from"; // file $file = fopen ( $fileatt, 'rb' ); $data = fread ( $file, filesize ( $fileatt ) ); fclose ( $file ); // attach file $semi_rand = md5 ( time () ); $mime_boundary = "==multipart_boundary_x{$semi_rand}x"; $headers .= "\nmime-version: 1.0\n" . "content-type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message = "this multi-part message in mime format.\n\n" . "-{$mime_boundary}\n" . "content-type: text/plain; charset=\"iso-8859-1\n" . "content-transfer-encoding: 7bit\n\n" . $mainmessage . "\n\n"; $data = chunk_split ( base64_encode ( $data ) ); $message .= "--{$mime_boundary}\n" . "content-type: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "content-disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "content-transfer-encoding: base64\n\n" . $data . "\n\n" . "-{$mime_boundary}-\n"; // send email if (mail ( $to, $subject, $message, $headers )) { echo "the email sent."; } else { echo "there error sending mail."; }
Comments
Post a Comment