2011-04-28 47 views
4

我有一個問題:我需要創建CSV文件,並將其附加到特定的電子郵件。 老實說,我從來沒有這樣做,所以我在這件事上相當沉重。任何人都可以告訴我,我必須開始或分享一些鏈接?發送CSV文件附加到電子郵件

+3

你堅持什麼方面?你有什麼代碼? – 2011-04-28 09:25:17

回答

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 = "[email protected]"; 
    $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"); 
    ?> 

親切的問候, 韋斯利。

+0

偉大的工作!!!!! – 2011-04-28 09:35:39

+0

沒問題,樂於幫忙! – Wesley 2011-04-28 09:42:22

+0

偉大的答案,thanx,衛斯理! – Kuen 2011-04-28 13:26:22