【问题标题】:Php Imap fetching emailsPhp Imap 获取电子邮件
【发布时间】:2015-07-21 12:39:45
【问题描述】:

我一直在尝试使用imap_fetchbody($stream, $msgno, $option) 获取电子邮件正文 但没有成功。

然后我尝试使用imap_fetchstructure($stream, $msgno) 并使用它们自己的子类型手动解码每种类型,例如:-

1. Alternative
2. Related
3. Mixed

对于前两个我可以用这样的东西解码

**1. Alternative**

if ($structure->subtype == 'ALTERNATIVE') {
    if (isset($structure->parts)) {
        $body2 = imap_fetchbody($stream, $email_id, 2);
        if ($body2 == null) {
            $body2 = imap_fetchbody($stream, $email_id, 1);
        }
        $body = quoted_printable_decode($body2);
    }
}

**2. Related**

if ($structure->subtype == 'RELATED') {
    if (isset($structure->parts)) {
        $parts = $structure->parts;
        $i = 0;
        $body2 = imap_fetchbody($stream, $email_id, 1.2);
        if ($body2 == null) {
            $body2 = imap_fetchbody($stream, $email_id, 1);
        }
        $body = quoted_printable_decode($body2);
        foreach ($parts as $part) {
            if ($parts[$i]) {
            }
            $i++;
            if (isset($parts[$i])) {
                if ($parts[$i]->ifid == 1) {
                    $id = $parts[$i]->id;
                    $imageid = substr($id, 1, -1);
                    $imageid = "cid:" . $imageid;
                    if ($parts[$i]->ifdparameters == 1) {
                        foreach ($parts[$i]->dparameters as $object) {
                            if (strtolower($object->attribute) == 'filename') {
                                $filename = $object->value;
                            }
                        }
                    }
                    if ($parts[$i]->ifparameters == 1) {
                        foreach ($parts[$i]->parameters as $object) {
                            if (strtolower($object->attribute) == 'name') {
                                $name = $object->value;
                            }
                        }
                    }
                    $body = str_replace($imageid, $filename, $body);
                }
            }
        }
    }
}

但是当涉及到混合时,我要么不知道,要么不知道该怎么做,或者我在哪里犯错。

**3. mixed**

if ($structure->subtype == 'MIXED') {
    if (isset($structure->parts)) {
        $parts = $structure->parts;
            // subtype = ALTERNATIVE
            if ($parts[0]->subtype == 'ALTERNATIVE') {
                if (isset($structure->parts)) {                                                                          
                    $body2 = imap_fetchbody($stream, $email_id, 1.2);
                    if ($body2 == null) {
                        $body2 = imap_fetchbody($stream, $email_id, 1);
                    }
                    $body = quoted_printable_decode($body2);
                }
            }
            // subtype = RELATED
            if ($parts[0]->subtype == 'RELATED') {
                if (isset($parts[0]->parts)) {
                    $parts = $parts[0]->parts;
                    $i = 0;
                    $body2 = imap_fetchbody($stream, $email_id, 1.1);
                    if ($body2 == null) {
                        $body2 = imap_fetchbody($stream, $email_id, 1);
                    }
                    $body = quoted_printable_decode($body2);
                    $name = "";
                    foreach ($parts as $part) {
                        if ($parts[0]) {
                        }
                        $i++;
                        if (isset($parts[$i])) {
                            if ($parts[$i]->ifid == 1) {
                                $id = $parts[$i]->id;
                                $imageid = substr($id, 1, -1);
                                $imageid = "cid:" . $imageid;
                                if ($parts[$i]->ifdparameters == 1) {
                                    foreach ($parts[$i]->dparameters as $object) {
                                        if (strtolower($object->attribute) == 'filename') {
                                            $filename = $object->value;
                                        }
                                    }
                                }
                                if ($parts[$i]->ifparameters == 1) {
                                    foreach ($parts[$i]->parameters as $object) {
                                        if (strtolower($object->attribute) == 'name') {                                                                                  
                                             $name = $object->value;
                                        }
                                    }
                                }
                            }
                            $body = str_replace($imageid, $name, $body);
                        }
                    }
                }
            }
        }
    }

【问题讨论】:

  • 我能得到答案吗?

标签: php imap


【解决方案1】:

您忘记了子类型 PLAIN。你也必须解码它

if ($parts[0]->subtype == 'PLAIN') {
            if (isset($structure->parts)) {                                        
                    $message2 = imap_fetchbody($stream, $email_id, 1);
                    $message = quoted_printable_decode(base64_decode($message2));
            }
    }

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 2013-02-16
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    相关资源
    最近更新 更多