2013-02-19 267 views
0

我正在使用pipe編程將電子郵件發送到腳本。使用此腳本,我可以成功將整個電子郵件保存爲我的服務器上的.txt文件。唯一要做的就是弄清楚如何保存電子郵件附件中的任何附件。 (此電子郵件地址只授予一個可信賴的來源,所以安全性不是問題)通過管道電子郵件保存電子郵件附件

,節省了整個郵件的.txt文件

工作代碼:

$fd = fopen("php://stdin", "r"); 
$email = ""; // This will be the variable holding the data. 
while (!feof($fd)) { 
$email .= fread($fd, 1024); 
} 
fclose($fd); 
/* Saves the data into a file */ 
$fdw = fopen("/home/lmshost22/public_html/pipemail.txt", "w+"); 
fwrite($fdw, $email); 
fclose($fdw); 

誰能幫助我的代碼,僅僅提取物附件(將始終是一個.csv文件)並將其保存在我的服務器上?

下面是.txt文件示出了用於附件的代碼:

------=_NextPart_000_0133_01CE0E98.061E7400 
Content-Type: application/vnd.ms-excel; 
    name="leads.csv" 
Content-Transfer-Encoding: quoted-printable 
Content-Disposition: attachment; 
    filename="leads.csv" 

ApplicationDetailId,DateCreated,VehicleInfoId,FirstName,MiddleName,LastNa= 
me,Suffix,Street,City,State,ZipCode,Email,HomePhoneArea,HomePhonePrefix,H= 
omePhoneSuffix,CellArea,CellPrefix,CellSuffix,WorkPhoneArea,WorkPhonePreF= 
ix,WorkPhoneSuffix,AmountBorrow,IsVehiclePaidOff,IsVehicleSalvaged,OweAmo= 
unt,TitleOwnership,IsInBankruptcy,IsInCreditCounseling,HearOfUs,VehicleIn= 
foId,Year,Make,Model,Trim,Miles,Engine,DriveTrain,Transmission,Options,Bo= 
ok,ClassCode,Door,FuelType,BodyStyle 
4523,2/18/2013 2:56:33 PM,4524,James,,Pruitt,,7900 = 
Carmelita,Atascadero,CA,93422,,702,=3D"353",=3D"9760",=3D"",=3D"",=3D"",=3D= 
"",=3D"",=3D"",=3D"2500.0000",True,False,0.0000,No = 
one,False,False,Google,4524,2001,=3D"Toyota",Tacoma = 
Xtracab,PreRunner,200000,V6 3.4 = 
Liter,2WD,Automatic,199443~199448~199471~199480~199508~4234190~,0.0000,1,= 
0,Gas,Pickup 

------=_NextPart_000_0133_01CE0E98.061E7400-- 

回答

1
  1. 附件意味着該消息是MIME /多部分,並且具有boundary標頭集合,這將限定的各消息部分。例如:

    Content-Type: multipart/mixed; 
        boundary="b1_bb1b331cd6dafa1dc6a19e3b2e090b07" 
    

    所以你要找到第一個通過類似:

    preg_match('/boundary="(.*)"/', $message, $matches); 
    
  2. 該消息由--與邊界串連分,由具體到那個部分,一個空行頭,後跟,然後是數據。例如:

    --b1_bb1b331cd6dafa1dc6a19e3b2e090b07 
    Content-Type: application/pdf; name="Invoice-37272.pdf" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment; filename="Invoice-37272.pdf" 
    
    // base64 data here // 
    

    ,消息被終止與'--' . $boundary . '--',如:--b1_bb1b331cd6dafa1dc6a19e3b2e090b07--

所以,你可以使用信息的這兩個位郵件分解成它們的組成部分,並找到/保存附件。

+0

該消息確實是多部分/混合。 MIME-Version:1.0 Content-Type:multipart/mixed; \t boundary =「---- = _ NextPart_000_0133_01CE0E98.061E7400」 X-Mailer:Microsoft Outlook 14.0 – user1789437 2013-02-19 18:25:06