[英]PHP: prefix 0 is removed from csv while sending in mail attachment
我正在向客戶端發送一封帶有 csv 附件的電子郵件。 雖然我的實際文件很少有前綴為 0 的數字。但是在郵件中獲取附件時,這些前綴將被刪除。 我不希望刪除這些前綴。 附件必須保持原樣。
例如:
實際 csv 中的數字:002569854745
隨附 csv 中的數字:2569854745
這是我的代碼
$myfile = __DIR__."/$filename";
$file_size = filesize($myfile);
$handle = fopen($myfile, "r");
$content = fread($handle, $file_size);
fclose($handle);
$multipartSep = '-----'.md5(time()).'-----';
$headers = array(
"From: from@gmail.com",
"BCC: bcc@wcities.com",
"Content-Type: multipart/mixed; boundary=\"$multipartSep\""
);
$attachment = chunk_split(base64_encode($content));
$txt = "test mail";
$body = "--$multipartSep\r\n"
. "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "\r\n"
. "$txt\r\n"
. "--$multipartSep\r\n"
. "Content-Type: text/csv\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"$filename\"\r\n"
. "\r\n"
. "$attachment\r\n"
. "--$multipartSep--";
//mail function
我懷疑您的 CSV 是否正在被修改。 我敢打賭,如果您檢查收到的原始文件,零都應該在那里。
問題將在於接下來會發生什么。
如果您在 Excel 之類的文件中打開帶有前導零的 CSV,它會將數字視為數字並刪除前導零。 如果要保留它們,則需要將每個值格式化為"=""002569854745"""
。 看到這個答案。
Excel 中的另一個選項是確保在導入 CSV 時更改導入配置,以便將每個字段視為文本,而不是使用其默認的“常規”格式,這會應用這樣的自動轉換。
如果您使用的不是 Excel 而是其他東西,您需要了解如何覆蓋這些被解釋為數字的字段。
您是否考慮過使用您標記此問題的 PHPMailer?
我沒有看到您檢查文件內容的編碼,也沒有跡象表明您實際上正在執行必要的步驟來實現format=flowed
,盡管這些步驟都可能與此問題有關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.