php 發送帶pdf附件的email 出現的是一長串字串 請教怎麼解決

來源:互聯網
上載者:User
php 發送帶pdf附件的email 出現的是一長串字串 請問如何解決
RT,發出的email附件不是一個pdf檔,而變成了一長串字串,

我的發送email的代碼如下:

function mail($mailto, $userid, $data){
global $dsql,$cfg_adminemail,$cfg_webname,$cfg_basehost,$cfg_memberurl;
$mailtitle = $cfg_webname.":通知";
$file=fopen("D:/xampp/htdocs/pdftemp/dsfasdf.pdf","r");
$fileName=$data['oid'].'.pdf';
$mimeType ="application/pdf";
$boundary = "nextpart_".uniqid(""); //分隔字元1
$boundary2 = "nextpart2_".uniqid(""); //分隔字元2

//email頭
$headers = "From: ".$cfg_adminemail."\r\nReply-To: $cfg_adminemail";
$headers .= "Content-type: multipart/mixed; boundary=\"$boundary1\"\r\n";

//讀取檔案
$read = fread($file, filesize("D:/xampp/htdocs/pdftemp/dsfasdf.pdf"));
//我們用base64方法把它編碼
$read = base64_encode($read);
//把這個長字串切成由每行76個字元組成的小塊
$read = chunk_split($read);

//以下是mailbody本文
$mailbody = "--$boundary1----$boundary2
Content-type: text/plain;charset=iso-8859-1;
Content-transfer-encoding: 8bit";
$mailbody .= '致: '.$userid.',



This is mailbody--$boundary2--';

//以下是附件部分
$mailbody .="--$boundary1
Content-type: text/html; name=$fileName
Content-disposition: inline; filename=$fileName
Content-transfer-encoding: 8bit
$read
--$boundary1--";


$status = smail($mailto,$mailtitle,$mailbody,$headers);
setMail($mailto, $mailtitle, $mailbody, $headers, $status);
return $status;
} php email pdf

分享到:


------解決方案--------------------
你用phpmailer把
------解決方案--------------------
你的這個可能是沒有指定attachment的類型?


// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";


推薦樓上的
$email = new PHPMailer();
$email->From = [email protected]';
$email->FromName = 'Your Name';
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( [email protected]' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

------解決方案--------------------
可能是boundary設定有誤,導致解析錯誤。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.