Base64编码的excel文件不能在电子邮件附件中解码

我正在创build一个excel文件并将其附加到电子邮件中。 电子邮件附带的excel文件发送,但内容全部被删除(N o s e> j×!êh®×è®Ø^¥ê‰Úz MMy¶9×Í'×)。

$sFileName = "BatchReport".date("Ymd"); $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()你不应该滚动你自己的附件处理。

https://codex.wordpress.org/Function_Reference/wp_mail

 wp_mail( $to, $subject, $message, $headers, $attachments ) 

您需要根据文档添加附件。

在你的情况下:

 $attachments = array(WP_PLUGIN_DIR."/cs-cart/includes/excel/".$sFileName.".xls");