我一直在尝试使用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);
}
}
}
}
}
}
答案 0 :(得分:0)
您忘记了子类型PLAIN。你也必须解码它
if ($parts[0]->subtype == 'PLAIN') {
if (isset($structure->parts)) {
$message2 = imap_fetchbody($stream, $email_id, 1);
$message = quoted_printable_decode(base64_decode($message2));
}
}