2
我一直在嘗試使用imap_fetchbody($stream, $msgno, $option) 取回電子郵件正文,但沒有成功。Php Imap抓取電子郵件
然後我試圖使用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);
}
}
}
}
}
}
我可以得到這個答案。 –