Send CSV file attached to the email

此生再无相见时 提交于 2019-12-30 07:08:17

问题


I have an issue: i need to create csv file and send it attached to the particular emails. Honestly i never do that so i'm quite a lammer in this matter. Can anybody tell me with what i have to start or share some links?


回答1:


I think this what you are looking for, i've used it in the past works perfectly.

Hope it helps.

 <?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");
   ?>

Kind regards, Wesley.



来源:https://stackoverflow.com/questions/5816421/send-csv-file-attached-to-the-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!