我正在创建一个excel文件并将其附加在电子邮件中。电子邮件发送时附带excel文件,但内容全乱了(N¬ŠÆ›jx ! -êh®×è®Ø^¥êè‰ÚÓzÛMy¶9×Í´x)。
$sFileName = "BatchReport".date("Y-m-d");
$eContent = chunk_split(base64_encode(file_get_contents(WP_PLUGIN_DIR."/cs-cart/includes/excel/".$sFileName.".xls")));
$sUniqueID = md5(uniqid(time()));
$sHeaders = "From: " . (($sFrom) ? $sFrom : "Cervid Solutions <admin@" . str_replace("www.","",$_SERVER["HTTP_HOST"]) . ">") . "'n" .
"MIME-Version: 1.0'n" .
"Content-Type: multipart/mixed; boundary=".$sUniqueID."'n'n" .
"This is a multi-part message in MIME format.'r'n" .
"--".$sUniqueID."'r'n" .
"Content-Type: text/plain; charset=ISO-8859-1'r'n" .
"Content-Transfer-Encoding: 7bit'r'n'r'n" .
$sMessage."'r'n'r'n" .
"--".$sUniqueID."'r'n" .
"Content-Type: application/octet-stream; name=".$sFileName.".xls"."'n" .
"Content-Transfer-Encoding: base64'r'n" .
"Content-Disposition: attachment; filename=".$sFileName.".xls"."'r'n'r'n" .
$eContent."'r'n'r'n" .
"--".$sUniqueID."--";
wp_mail($sTo, $sSubject, $sMessage, $sHeaders);
我见过其他人使用相同的代码,但似乎没有同样的问题,文件内容被正确解码。我做错了什么?
当您使用wp_mail()
时,您不应该滚动自己的附件处理。
wp_mail( $to, $subject, $message, $headers, $attachments )
您需要根据文档添加附件。
在你的例子中:
$attachments = array(WP_PLUGIN_DIR."/cs-cart/includes/excel/".$sFileName.".xls");