电子邮件附件中的 Base64 编码 excel 文件未解码

Base64 encoded excel file not decoding in email attachments

我正在创建一个 excel 文件并将其附加到电子邮件中。邮件附上 excel 文件,但内容全乱 (N¬ŠÆ›j×!êh®×è®Ø^¥êè‰ÚÓzÛMy¶9×Í´×).

$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(),您不应该滚动您自己的附件处理。

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");