0

我正在尝试在 PHP 中将 CSV 文件作为附件发送,并使用邮件功能作为附件。以下代码工作正常。它成功附加了 CSV 文件并将其发送给收件人,但附加的输出文件(通过电子邮件发送)以文本格式 (.txt) 出现。我不知道我在哪里犯了错误以及我需要更改标题以在附加的电子邮件中保留原始 CSV 文件格式。

        $path ="/myhost/public_html/csv/"; 
        $file_name = $path."Test";
        $file_name.=".csv";
        $from     = "some@myemail.co.uk";
        $to       = "other@hisemail.co.uk";
        $fat=$file_name;
        $subject  = $_POST[subject];
        //   $message  = $_POST[message];
        //replace \n with <br>
        $message = str_replace("\n", "<br>",$message);
        //report
        echo "<b><font color=#8080FF> From: $from </b><br>";
        echo "<b>To: $to </b><br>";
        echo "<b>Subject: $subject</b><br><br></font>";
        // Obtain file upload variables
        $fileatt      = $_FILES[$fat]['tmp_name'];
        $fileatt_type = $_FILES[$fat]['type'];
        $fileatt_name = $_FILES[$fat]['name'];

        $headers = "From: $from  \n";


        // if($_FILES['fileatt']['size'] > 0)
        if (file_exists($fat)) {
           // Read the file to be attached ('rb' = read binary)
           $file = fopen($fat,'rb');
           $data = fread($file,filesize($fat));
           fclose($file);
           // Generate a boundary string
           $semi_rand = md5(time());
           $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
           // Add the headers for a file attachment
           $headers .= "MIME-Version: 1.0\n" .
                    "Content-Type: multipart/mixed;\n" .
                    " boundary=\"{$mime_boundary}\"";
           // Add a multipart boundary above the  message
           $message = "This is a multi-part message in MIME format.\n\n" .
           "--{$mime_boundary}\n" .
           "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
           "Content-Transfer-Encoding: 7bit\n\n" .
           $message . "\n\n";

           // Base64 encode the file data
           $data = chunk_split(base64_encode($data));
           // Add file attachment to the message
           $message .= "--{$mime_boundary}\n" .
           "Content-Type: {$fileatt_type};\n" .
           " name=\"{$fileatt_name}\"\n" .
           //"Content-Disposition: attachment;\n" .
           //" filename=\"{$fileatt_name}\"\n" .
           "Content-Transfer-Encoding: base64\n\n" .
           $data . "\n\n" .
           "--{$mime_boundary}--\n";
        } else echo "File error!  ";

        //send the mail
        if(mail($to, $subject, $message,$headers))echo "<b><font color=#FF0000>Message was sent!<b></font>";
        else echo "<b><font color=#FF0000>Message error!<b></font>";

我只想在电子邮件附件中保留原始文件格式。请帮帮我。

4

2 回答 2

1

我建议使用库来处理电子邮件附件。这种事情会很快变得复杂,并且其他人已经弄清楚了附加 CSV 文件时需要发生的所有细节。我会使用类似http://swiftmailer.org/

您可以执行上面的所有代码,或者更像...

require_once 'lib/swift_required.php';

$message = Swift_Message::newInstance()
  ->setSubject('Your subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ->addPart('<q>Here is the message itself</q>', 'text/html')
  ->attach(Swift_Attachment::fromPath('my-document.csv'));

示例来自:http ://swiftmailer.org/docs/messages.html

于 2012-10-05T12:46:35.480 回答
0
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";

这可能有帮助吗?

于 2012-10-05T12:45:11.587 回答