メールをブラウザで受信するプログラムを作ったが、
件名とか、受信日時や、送信元などは、取得できたが、
本文と、添付ファイルが混ざった場合、
本文と添付ファイルをあわせて本文として受信してしまい、きもちがわるい。

本文と添付ファイルをどうやって分離するのか・・。

こんなサイト発見。

メールで送られてきた内容をPHPのプログラムで受けて、MySQLのDBに放り込むというシステムを作りたいと思っています。添付ファイルを切り出す関数とか、改行処理や文字コードのTipsとか、ヘッダ切り出しの方法とか、そういうのを解説しているページはないでしょうか。POPはqmailで、qmail->PHPプログラムの部分は分かります。


そうそう、こんな質問と同じ感じ。
2番目の回答者さんでさるid:upride さんの情報によると、

http://php.s3.to/
PHP!

写メールBBSのソースが宜しいかと思います

このソースはクライアントプル型ですが

調査。
http://php.s3.to/script.php
これの写メールBBSをダウンロードしてみて、ソースコードを調査。
多分、ここを解析すればいいのでしょう。というところにめぼしをつけたけど、
今日は、本当に久しぶりの休みなので、も仕事はここで終わりたい。
ので、ここに、ちょっとメモしておく。

多分、↓を調査すればよさげ。でも、今日はもうしんどい。

  // マルチパートならばバウンダリに分割
  if (eregi("\nContent-type:.*multipart/",$head)) {
    eregi('boundary="([^"]+)"', $head, $boureg);
    $body = str_replace($boureg[1], urlencode($boureg[1]), $body);
    $part = split("\r\n--".urlencode($boureg[1])."-?-?",$body);
    if (eregi('boundary="([^"]+)"', $body, $boureg2)) {//multipart/altanative
      $body = str_replace($boureg2[1], urlencode($boureg2[1]), $body);
      $body = eregi_replace("\r\n--".urlencode($boureg[1])."-?-?\r\n","",$body);
      $part = split("\r\n--".urlencode($boureg2[1])."-?-?",$body);
    }
  } else {
    $part[0] = $dat[$j];// 普通のテキストメール
  }
  foreach ($part as $multi) {
    list($m_head, $m_body) = mime_split($multi);
    $m_body = ereg_replace("\r\n\.\r\n$", "", $m_body);
    if (!eregi("Content-type: *([^;\n]+)", $m_head, $type)) continue;
    list($main, $sub) = explode("/", $type[1]);
    // 本文をデコード
    if (strtolower($main) == "text") {
      if (eregi("Content-Transfer-Encoding:.*base64", $m_head)) 
        $m_body = base64_decode($m_body);
      if (eregi("Content-Transfer-Encoding:.*quoted-printable", $m_head)) 
        $m_body = quoted_printable_decode($m_body);
      $text = convert($m_body);
      if ($sub == "html") $text = strip_tags($text);
      // 電話番号削除
      $text = eregi_replace("([[:digit:]]{11})|([[:digit:]\-]{13})", "", $text);
      // 下線削除
      $text = eregi_replace("[_]{25,}", "", $text);
       // mac削除
      $text = ereg_replace("Content-type: multipart/appledouble;[[:space:]]boundary=(.*)","",$text);
      // 広告等削除
      if (is_array($word)) {
        $text = str_replace($word, "", $text);
      }
      // 文字数オーバー
      if (strlen($text) > $maxtext) $text = substr($text, 0, $maxtext)."...";
      $text = str_replace(">",">",$text);
      $text = str_replace("<","&lt;",$text);
      $text = str_replace("\r\n", "\r",$text);
      $text = str_replace("\r", "\n",$text);
      $text = preg_replace("/\n{2,}/", "\n\n", $text);
      $text = str_replace("\n", "<br>", $text);
    }
    // ファイル名を抽出
    if (eregi("name=\"?([^\"\n]+)\"?",$m_head, $filereg)) {
      $filename = ereg_replace("[\t\r\n]", "", $filereg[1]);
      while (eregi("(.*)=\?iso-2022-jp\?B\?([^\?]+)\?=(.*)",$filename,$regs)) {
        $filename = $regs[1].base64_decode($regs[2]).$regs[3];
        $filename = convert($filename);
      }
    }
    // 添付データをデコードして保存
    if (eregi("Content-Transfer-Encoding:.*base64", $m_head) && eregi($subtype, $sub)) {
      $tmp = base64_decode($m_body);
      if (!$original || !$filename) $filename = $now.".".$sub;
      if (strlen($tmp) < $maxbyte && !eregi($viri, $filename) && $write) {
        $fp = fopen($tmpdir.$filename, "w");
        fputs($fp, $tmp);
        fclose($fp);
        $attach = $filename;
      } else {
        $write = false;
      }
    }
  }