简体   繁体   English

PHP:在发送邮件附件时从 csv 中删除前缀 0

[英]PHP: prefix 0 is removed from csv while sending in mail attachment

I am sending an email to the client with csv attached file.我正在向客户端发送一封带有 csv 附件的电子邮件。 While my actual file have few numbers with prefix 0. But when getting the attachment in the mail, those prefix is being removed.虽然我的实际文件很少有前缀为 0 的数字。但是在邮件中获取附件时,这些前缀将被删除。 I don't want those prefix to be removed.我不希望删除这些前缀。 Attachment has to be as it is.附件必须保持原样。

eg:例如:
number in actual csv: 002569854745实际 csv 中的数字:002569854745
number in attached csv: 2569854745随附 csv 中的数字:2569854745

here is my code这是我的代码

$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

I'm doubtful that your CSV is being modified at all.我怀疑您的 CSV 是否正在被修改。 I'd bet that if you inspected the raw file received, the zeroes would all be right there as they should be.我敢打赌,如果您检查收到的原始文件,零都应该在那里。

The problem will be in what happens next.问题将在于接下来会发生什么。

If you open a CSV with leading zeroes in something like Excel, it will treat the numbers as numbers and drop the leading zeroes.如果您在 Excel 之类的文件中打开带有前导零的 CSV,它会将数字视为数字并删除前导零。 If you want to keep them, you need to format each value like "=""002569854745""" .如果要保留它们,则需要将每个值格式化为"=""002569854745""" See this answer .看到这个答案

Another option in Excel is to ensure that when you import a CSV, you alter the import config so that it treats every field as text, rather than using its default "General" format, which applies automatic conversions like this. Excel 中的另一个选项是确保在导入 CSV 时更改导入配置,以便将每个字段视为文本,而不是使用其默认的“常规”格式,这会应用这样的自动转换。

If you're not using Excel but something else, you'll need to find out how to override these fields being interpreted as numbers.如果您使用的不是 Excel 而是其他东西,您需要了解如何覆盖这些被解释为数字的字段。

Have you considered using PHPMailer that you tagged this question with?您是否考虑过使用您标记此问题的 PHPMailer?

I don't see you checking encodings on the file content, and there's no sign that you are actually performing the necessary steps to implement format=flowed either, though nether of those are likely to do with this issue.我没有看到您检查文件内容的编码,也没有迹象表明您实际上正在执行必要的步骤来实现format=flowed ,尽管这些步骤都可能与此问题有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM