如何从PHP获取电子邮件及其附件

时间:2020-03-06 14:32:26  来源:igfitidea点击:

我正在为朋友的婚礼写一个照相馆网络应用程序,他们希望照相馆供客人提交当天拍摄的数码照片。

在评估了所有选项之后,我认为对于用户来说,最简单的事情是让他们使用熟悉的界面(他们的电子邮件),并让他们将图片作为附件发送。

我已经创建了一个邮箱,但是现在我需要连接并检索这些附件以进行自动处理,以添加到图库系统中。但是如何?我们是否看到过这样做的任何教程或者预制类?

解决方案

我认为我们想要一个MIME消息解析器。

我以前用过这个,它似乎可以正常工作,尽管我还没有在很大的附件上测试过(例如,我们可能会从数码相机上获得2-3MB的文件)。

我们是否已经具有读取POP3 / IMAP邮箱的系统?在同一站点上还有另一种也可以在POP3上使用的类(我相信也有一种IMAP),但是如果我们要下载大量的类,那么我们可能想研究一些基于C的解决方案,因为我相信是纯PHP。

我们正在使用什么MTA?如果使用postfix + maildrop,则可以创建一个过滤规则,该规则通过PHP脚本通过管道传递某些消息,然后再处理传入的邮件。 (google为maildrop和xfilter)。

我以前曾经做过很多事情,但是我找不到代码,这是我发现的缩小版本。它应该使我们走上正确的道路。我曾经从cronjob运行这种类型的脚本。对不起,我找不到最终版本。 ;(

// Open pop mailbox
if (!$mbox = imap_open ("{localhost:110/pop3/notls}INBOX", "user", "tester")) {
  die ('Cannot connect/check pop mail! Exiting');
}

if ($hdr = imap_check($mbox)) {
  $msgCount = $hdr->Nmsgs;
} else {
  echo "Failed to get mail";
  exit;
}

$MN=$msgCount;
$overview=imap_fetch_overview($mbox,"1:$MN",0);

for ($X = 1; $X <= $MN; $X++) {

  $file = imap_fetchbody($mbox, $X, 1);

  imap_delete($mbox, $X);
}

imap_expunge($mbox);
imap_close($mbox);

祝你好运!

如果要为此目的创建专用邮箱,则几乎绝对不需要使用过滤机制。相反,我们希望让邮箱成为应用程序的管道,并让应用程序简单地从stdin中读取消息,解析出正文,然后MIME解析正文以获取附件。

我知道,所有流行的基于unix的MTA(例如sendmail,postfix和qmail)都支持将邮箱作为管道。通常,我们可以在别名文件中定义它,如下所示:

sendmail或者postfix语法
msgsubmit:" | / usr / bin / php〜path / to / example.php"

然后,发送到msgsubmit @的邮件将路由到php程序进行传递。

这具有不依赖IMAP服务器或者MTA以外的任何其他服务器处于活动状态的优点,并且只要我们可以控制目标主机的MTA,它就可以正常工作。如果希望脚本检查系统上的所有消息,则需要进行过滤,我猜不是这样。

如果要将副本保留在某个位置的邮箱中(这不是一个坏主意),只需定义别名即可转到多个地址,如下所示:

msgsubmit: "| /usr/bin/php ~path/to/example.php", msgsubmit-box

或者后缀虚拟格式:

msgsubmit
    "| /usr/bin/php ~path/to/example.php"
    msgsubmit-box

我们是否考虑过使用Google的Picasa网络相册?
我们可以设置一个电子邮件地址,以将照片发送到并在线共享。
然后,我们可以获得这些照片的RSS提要,这是大多数程序员都喜欢的。
比MTA更熟悉。

<?php
//make sure that submit button name is 'Submit'
if(isset($_POST['Submit'])){

       $name = $_POST['visitorname'];
       $email = $_POST['visitoremail'];
       $message = $_POST['visitormessage'];

            $to="[email protected]";

          $subject="From ".$name;

          $from = $email;

          // generate a random string to be used as the boundary marker
          $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

          // now we'll build the message headers
          $headers = "From: $from\r\n" .
          "MIME-Version: 1.0\r\n" .
             "Content-Type: multipart/mixed;\r\n" .
             " boundary=\"{$mime_boundary}\"";

          // next, we'll build the invisible portion of the message body
          // note that we insert two dashes in front of the MIME boundary
          // when we use it
          $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
          $message . "\n\n";

 foreach($_FILES as $userfile)
          {
             // store the file information to variables for easier access
             $tmp_name = $userfile['tmp_name'];
             $type = $userfile['type'];
             $name = $userfile['name'];
             $size = $userfile['size'];

             // if the upload succeded, the file will exist
             if (file_exists($tmp_name))
             {

                // check to make sure that it is an uploaded file and not a system file
                if(is_uploaded_file($tmp_name))
                {

                   // open the file for a binary read
                   $file = fopen($tmp_name,'rb');

                   // read the file content into a variable
                   $data = fread($file,filesize($tmp_name));

                   // close the file
                   fclose($file);

                   // now we encode it and split it into acceptable length lines
                   $data = chunk_split(base64_encode($data));
                }

                // now we'll insert a boundary to indicate we're starting the attachment
                // we have to specify the content type, file name, and disposition as
                // an attachment, then add the file content.
                // NOTE: we don't set another boundary to indicate that the end of the
                // file has been reached here. we only want one boundary between each file
                // we'll add the final one after the loop finishes.
                $message .= "--{$mime_boundary}\n" .
                   "Content-Type: {$type};\n" .
                   " name=\"{$name}\"\n" .
                   "Content-Disposition: attachment;\n" .
                   " filename=\"{$fileatt_name}\"\n" .
                   "Content-Transfer-Encoding: base64\n\n" .
                $data . "\n\n";
             }
          }

$ok = @mail($to, $subject, $message , $headers);
if ($ok) {
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {

if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);

      }
    }
  }
else
  {

  }
echo "<span class='red'>E-mail has been sent successfully from $mail_name to $to</span>"; }
else{
echo "<span class='red'>Failed to send the E-mail from $from to $to</span>";
}
}
?>

p / s:我使用了此代码。希望其工作能为我们提供帮助。只需复制和粘贴即可。请确保文本字段名称与此页面中的名称相同。它适用于所有类型的文件。如有其他问题,请给我发电子邮件Shah @ mc-oren.com。无论如何,我也在学习过程中。=)。谢谢。