PHP IMAP POP 转发脚本发送以某种方式更改的图像附件

Posted

技术标签:

【中文标题】PHP IMAP POP 转发脚本发送以某种方式更改的图像附件【英文标题】:PHP IMAP POP forwarding script sends image attachments changed somehow 【发布时间】:2012-06-29 23:48:03 【问题描述】:

我正在运行一个脚本来登录 IMAP/POP 邮箱,将内容转发到新的电子邮件地址,然后将它们从邮箱中删除。

它现在几乎可以工作了,除了由于一些奇怪的、莫名其妙的原因,发送到我们 Ning 网站的转发附件图像看起来被压缩了,而直接发送到 Ning 的上传电子邮件地址的带有图像附件的电子邮件看起来格式完美。因此,我们转发的电子邮件格式有所不同。好像附件中的图片和原来的不完全一样。

压缩图片上传示例http://members.bigmanwalking.com/photo/a-test-photo?context=latest

任何帮助都会很棒...这是一个谜,为什么图像应该通过这个脚本突然失去形状。

<?php

      // Change to your mail server
      $host = "pop.1and1.co.uk";

      // Connecting to POP3 email server.
      $connection = imap_open("" . $host . ":110/pop3/notls", 'test@bigmanwalking.com', 'xxxx');

      // Total number of messages in Inbox
      $count = imap_num_msg($connection);
      echo $count . " messages found<br />";

      // Read Messages in Loop, Forward it to Actual User email and than delete it from current email account.
      for ($i = 1; $i <= $count; $i++) 
          $headers = imap_headerinfo($connection, $i);

          $subject = $headers->subject;

          $from = $headers->from[0]->mailbox . '@' . $headers->from[0]->host;
          if ($headers->cc[0]->mailbox)
              $cc = $headers->cc[0]->mailbox . '@' . $headers->cc[0]->host;
          $subject = $headers->subject;

          $structure = imap_fetchstructure($connection, $i);
          //$type = $this->get_mime_type($structure);

          // GET html BODY
          //$body = $this->get_part($connection, $i, "");

          $raw_body = imap_body($connection, $i);

          $attachments = array();

          if (isset($structure->parts) && count($structure->parts)) 
              for ($e = 0; $e < count($structure->parts); $e++) 
                  $attachments[$e] = array('is_attachment' => false, 'filename' => '', 'name' => '', 'attachment' => '');

                  if ($structure->parts[$e]->ifdparameters) 
                      foreach ($structure->parts[$e]->dparameters as $object) 
                          if (strtolower($object->attribute) == 'filename') 
                              $attachments[$e]['is_attachment'] = true;
                              $attachments[$e]['filename'] = $object->value;
                           //if (strtolower($object->attribute) == 'filename')
                       //foreach ($structure->parts[$e]->dparameters as $object)
                   //if ($structure->parts[$e]->ifdparameters)

                  if ($structure->parts[$e]->ifparameters) 
                      foreach ($structure->parts[$e]->parameters as $object) 
                          if (strtolower($object->attribute) == 'name') 
                              $attachments[$e]['is_attachment'] = true;
                              $attachments[$e]['name'] = $object->value;
                           //if (strtolower($object->attribute) == 'name')
                       //foreach ($structure->parts[$e]->parameters as $object)
                   //if ($structure->parts[$e]->ifparameters)

                  if ($attachments[$e]['is_attachment']) 
                      $attachments[$e]['attachment'] = @imap_fetchbody($connection, $i, $e + 1);
                      if ($structure->parts[$e]->encoding == 3) 
                          // 3 = BASE64
                          $attachments[$e]['attachment'] = base64_decode($attachments[$e]['attachment']);
                       //if ($structure->parts[$e]->encoding == 3)
                      elseif ($structure->parts[$e]->encoding == 4) 
                          // 4 = QUOTED-PRINTABLE
                          $attachments[$e]['attachment'] = quoted_printable_decode($attachments[$e]['attachment']);
                       //elseif ($structure->parts[$e]->encoding == 4)
                   //if ($attachments[$e]['is_attachment'])

                  if ($attachments[$e]['is_attachment']) 
                      $filename = $attachments[$e]['filename'];
                      $filename = $attachments[$e]['name'];
                      $filecontent = $attachments[$e]['attachment'];
                   //if ($attachments[$e]['is_attachment'])
               //for ($e = 0; $e < count($structure->parts); $e++)
           //if (isset($structure->parts) && count($structure->parts))



          echo "<pre>";
          echo "From: " . $headers->Unseen . "<br />";
          echo "From: " . $from . "<br />";
          echo "Cc: " . $cc . "<br />";
          echo "Subject: " . $subject . "<br />";
          echo "Content Type: " . $type . "<br />";
          echo "Body: " . $body . "<br />";


          $mail = new Zend_Mail();

          $mail->settype(Zend_Mime::MULTIPART_MIXED);

          for ($k = 0; $k < count($attachments); $k++) 
              $filename = $attachments[$k]['name'];
              $filecontent = $attachments[$k]['attachment'];

              if ($filename && $filecontent) 
                  $file = $mail->createAttachment($filecontent);
                  $file->filename = $filename;
               //if ($filename && $filecontent)
           //for ($k = 0; $k < count($attachments); $k++)


          $mail->setFrom($from);
          $mail->addTo('test@members.bigmanwalking.com');
          if ($cc)
              $mail->addCc($cc);
          $mail->setSubject($subject);
          $mail->setBodyHtml($body);
          $mail->send();

          // Mark the email messages once read
          imap_delete($connection, $i);

       //for ($i = 1; $i <= $count; $i++)
      // Delete all marked message from current email account.

      imap_expunge($connection);

?>

【问题讨论】:

我已将问题范围缩小到不将 MIME 类型应用于上述脚本中的附件。 【参考方案1】:

找到了答案。它是检测附件的 Mime 类型,并将 Mime 类型添加到外发电子邮件的附件中。完成后,照片以正确的比例出现在 Ning 上。

我需要的帮助在这里找到http://php.net/manual/en/function.imap-fetchstructure.php

【讨论】:

嘿@Nimloc 我知道这是一个旧线程,但我需要这个确切的解决方案。你身边还有最终剧本的副本吗?

以上是关于PHP IMAP POP 转发脚本发送以某种方式更改的图像附件的主要内容,如果未能解决你的问题,请参考以下文章

java 实现 email 邮件发送最简单优雅的方式(网易 163 为例)

使用php-imap查询操作邮件收件箱

Gmail,QMail,163邮箱的 IMAP/SMTP/POP3 地址

PHP IMAP 解码消息

outlook 设置POP3/IMAP/SMTP服务 (账号密码-授权码)发送获取授权码

常用电子邮件协议服务POP3/IMAP/SMTP/Exchange