我正在制作一个电子邮件客户端,我有一个功能可以保存服务器上附加到电子邮件的所有文件。文件正在保存在服务器上,但问题是每个文件的大小变为0 kB,每个文件的编码等于3。
我想知道如何根据附件的子类型为imap_fetchbody()
选择正确的选项。
这是我的代码:
if($attachments[$i]['is_attachment']){
$attachments[$i]['attachment'] = imap_fetchbody($imap, $uid, $i + 1;);
if($structure->parts[$i]->type == 1){
// 3 = BASE64
$attachments[$i]['attachment'] = imap_8bit($attachments[$i]['attachment']);
}
if($structure->parts[$i]->type == 2){
// 3 = BASE64
$attachments[$i]['attachment'] = imap_binary($attachments[$i]['attachment']);
}
if($structure->parts[$i]->type == 7){
// 3 = BASE64
$attachments[$i]['attachment'] = imap_base64($attachments[$i]['attachment']);
}
if($structure->parts[$i]->type == 4){
// 3 = BASE64
$attachments[$i]['attachment'] = imap_qprint($attachments[$i]['attachment']);
}
if($structure->parts[$i]->type == 3){
// 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->type == 4){
// 4 = QUOTED - PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
//return $message;
}
答案 0 :(得分:1)
您应该查看传输编码,而不是类型,因为它可以使用不同的编码对某种文件类型(例如,图像,类型5或TYPEIMAGE
)进行编码(例如base64或引号的可打印)。
做这样的事情:
if($structure->parts[$i]->encoding == ENC8BIT){
$attachments[$i]['attachment'] = imap_8bit($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == ENCBINARY){
$attachments[$i]['attachment'] = imap_binary($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == ENCBASE64){
$attachments[$i]['attachment'] = imap_base64($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == ENCQUOTEDPRINTABLE){
$attachments[$i]['attachment'] = imap_qprint($attachments[$i]['attachment']);
}