发送附加到电子邮件的CSV文件

时间:2011-04-28 09:24:03

标签: php csv email-integration

我遇到了一个问题:我需要创建csv文件并将其附加到特定的电子邮件中。 老实说,我从来没有这样做,所以我在这件事上非常笨拙。任何人都能告诉我我必须开始或分享一些链接吗?

1 个答案:

答案 0 :(得分:18)

我认为这就是你要找的东西,我过去使用过的作品非常完美。

希望它有所帮助。

 <?php
    $cr = "\n";
    $csvdata = "First Name" . ',' . "Last Name"  . $cr;
    $csvdata .= $txtFName . ',' . $txtLName . $cr;

    $thisfile = 'file.csv';

    $encoded = chunk_split(base64_encode($csvdata));

    // create the email and send it off

    $subject = "File you requested from RRWH.com";
    $from = "scripts@rrwh.com";
    $headers = 'MIME-Version: 1.0' . "\n";
    $headers .= 'Content-Type: multipart/mixed;
        boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n";

    $message = '

    This is a multi-part message in MIME format.

    ------=_NextPart_001_0011_1234ABCD.4321FDAC
    Content-Type: text/plain;
            charset="us-ascii"
    Content-Transfer-Encoding: 7bit

    Hello

    We have attached for you the PHP script that you requested from http://rrwh.com/scripts.php
    as a zip file.

    Regards

    ------=_NextPart_001_0011_1234ABCD.4321FDAC
    Content-Type: application/octet-stream;  name="';

    $message .= "$thisfile";
    $message .= '"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename="';
    $message .= "$thisfile";
    $message .= '"

    ';
    $message .= "$encoded";
    $message .= '

    ------=_NextPart_001_0011_1234ABCD.4321FDAC--

    ';

    // now send the email
    mail($email, $subject, $message, $headers, "-f$from");
   ?>

亲切的问候, 韦斯利。