我正在创建一个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);
我见过其他人使用相同的代码,但似乎没有正确解码文件内容的问题。我做错了什么?
答案 0 :(得分:2)
当您使用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");