我正在使用管道编程来向脚本发送电子邮件。使用此脚本,我可以成功地将整个电子邮件保存为我的服务器上的.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--
答案 0 :(得分:1)
附件意味着邮件是MIME / Multipart,并且设置了boundary
标头,用于界定各种邮件部分。例如:
Content-Type: multipart/mixed;
boundary="b1_bb1b331cd6dafa1dc6a19e3b2e090b07"
所以你想首先通过以下方式找到:
preg_match('/boundary="(.*)"/', $message, $matches);
消息除以--
与边界连接,然后是特定于该部分的标题,空白行,然后是数据。例如:
--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--
因此,您可以使用这两个信息将您的消息分解为其组成部分并查找/保存附件。