我已经被困在这几个小时了。但基本上,我有一个收件箱,可以收到包含我想要删除的内容的电子邮件,以及附带的PDF。我想出了如何取出我想要的所有HTML,但如何保存附加的PDF?
这是我到目前为止的鳕鱼:
public function read_emails(){ //设置用户检查
$strUser = "email";
$strPassword = "passwrd";
$delete_emails=false;
// open
$hMail = imap_open ("{webmail.somethingsomething.com:143/notls}INBOX", "$strUser", "$strPassword") or die("can't connect: " . imap_last_error());
// get headers
$aHeaders = imap_headers( $hMail );
// get message count
$objMail = imap_mailboxmsginfo( $hMail );
if($objMail != NULL)
{
// process messages
for( $idxMsg = 1; $idxMsg <= $objMail->Nmsgs; $idxMsg++ )
{
// get header info
$objHeader = imap_headerinfo( $hMail, $idxMsg );
// get from object array
$aFrom = $objHeader->from;
// process headers
for( $idx = 0; $idx < count($aFrom); $idx++ )
{
// get object
$objData = $aFrom[ $idx ];
// get email from
$strEmailFrom = $objData->mailbox . "@" . $objData->host;
//Get the subject
$message_subject = $objHeader->subject;
//Get the body of the message
$fullBody = imap_fetchbody($hMail,$idxMsg, 2);//displays full body including junk
$bodyMessage = quoted_printable_decode($fullBody);
//Get the msg structure
$message_structure = imap_fetchstructure($hMail, $idx);
//WHAT DO I DO HERE????
}
// delete message
if($delete_emails == true){
imap_delete( $hMail, $idxMsg );
}
}
// expunge deleted messages
if($delete_emails == true){
imap_expunge( $hMail );
}
//Clears the cache
imap_gc($hMail, IMAP_GC_ELT);
}
// close
imap_close( $hMail );
}
我有消息结构,如果我print_r我可以看到其中一个部分有附件,我可以看到它是一个PDF。不知道接下来会发生什么。我已经尝试过,似乎无法找到一个好的解决方案。
谢谢!
编辑:这是imap_fetchstructure的内容
stdClass对象([type] =&gt; 1 [编码] =&gt; 0 [ifsubtype] =&gt; 1 [subtype] =&gt; MIXED [ifdescription] =&gt; 0 [ifid] =&gt; 0 [ifdisposition ] =&gt; 0 [ifdparameters] =&gt; 0 [ifparameters] =&gt; 1 [参数] =&gt;数组([0] =&gt; stdClass对象([attribute] =&gt; boundary [value] =&gt; f46d044473ef81609f04d5f1997c ))[parts] =&gt;数组([0] =&gt; stdClass对象([type] =&gt; 1 [编码] =&gt; 0 [ifsubtype] =&gt; 1 [subtype] =&gt; ALTERNATIVE [ifdescription] =&gt; 0 [ifid] =&gt; 0 [ifdisposition] =&gt; 0 [ifdparameters] =&gt; 0 [ifparameters] =&gt; 1 [参数] =&gt;数组([0] =&gt; stdClass对象([ attribute] =&gt; boundary [value] =&gt; f46d044473ef81609b04d5f1997a))[parts] =&gt;数组([0] =&gt; stdClass对象([type] =&gt; 0 [编码] =&gt; 0 [ifsubtype] = &gt; 1 [subtype] =&gt; PLAIN [ifdescription] =&gt; 0 [ifid] =&gt; 0 [lines] =&gt; 1 [bytes] =&gt; 37 [ifdisposition] =&gt; 0 [ifdparameters] =&gt; ; 0 [ifparameters] =&gt; 1 [parameters] =&gt;数组([0] =&gt; stdClass对象([attribute] =&gt; charset [value] =&gt; ISO-8859-1)))[1] =&gt; stdClass对象([type] =&gt; 0 [编码] =&gt; 0 [ifsubtype] =&gt; 1 [subtype] =&gt; HTML [ifdescription] =&gt; 0 [ifid] =&gt; 0 [lines] =&gt; ; 1 [字节] =&gt; 37 [ifdisposition] =&gt; 0 [ifdparameters] =&gt; 0 [ifparameters] =&gt; 1 [参数] =&gt;数组([0] =&gt; stdClass对象([属性] =&gt; charset [value] =&gt; ISO-8859-1))))))[1] =&gt; stdClass对象([type] =&gt; 3 [编码] =&gt; 3 [ifsubtype] =&gt; 1 [subtype] =&gt; PDF [ifdescription] =&gt; 0 [ifid] =&gt; 0 [bytes] =&gt ; 179876 [ifdisposition] =&gt; 1 [disposition] =&gt; attachment [ifdparameters] =&gt; 1 [dparameters] =&gt;数组([0] =&gt; stdClass对象([attribute] =&gt; filename [value] =&gt; Account.pdf声明))[ifparameters] =&gt; 1 [parameters] =&gt;数组([0] =&gt; stdClass对象([attribute] =&gt; name [value] =&gt;帐户报表.pdf)))))
答案 0 :(得分:0)
根据doc,您在$message_structure->parts
中有一系列正文部分,因此您可以遍历此数组,并在部分的->type
和->subtype
对应时停止到PDF文件。类似的东西:
foreach ($message_structure->parts as $idx => $part) {
if (TYPEAPPLICATION === $part->type && 'pdf' === strtolower($part->subtype)) {
$data = imap_fetchbody($hMail, $idxMsg, $idx);
// Then you should check the $part->encoding
// to decode data appropriately, e.g.:
if (ENCBASE64 === $part->encoding) {
$data = base64_decode($data);
}
}
}