title: Bad message number error
body: <p>I am using a script to forward email addresses and getting bad message number error. the message number is parsed as an argument for the function and i am not sure how to get the message number and insert it as the argument. Please help. I am new to php. The error points out  <code>$structure = imap_fetchstructure($connection, $id, FT_UID);</code> and <code>$result['text'] = imap_body($connection, $id, FT_UID);</code> parts. Please help me.</p>
  
  <pre><code> &lt;?php
  require_once '../swift/lib/swift_required.php';
  $hostname = '{imap.xyz.com:993/imap/ssl}INBOX';
  $username = 'email';
  $password = 'password';
  
  /* try to connect */
  $connection = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
  
  ini_set('memory_limit', '256M');
  
  function Message_Parse($id)
  {   
  global $connection;
      if (is_resource($connection))
      {
          $result = array
          (
              'text' =&gt; null,
              'html' =&gt; null,
              'attachments' =&gt; array(),
          );  
  
                  $structure = imap_fetchstructure($connection, $id, FT_UID);
          //print_r($structure);
  //array_key_exists — Checks if the given key or index exists in the array
          if (is_array($structure) &amp;&amp; array_key_exists('parts', $structure))
          {
              foreach ($structure-&gt;parts as $key =&gt; $part)
              {
                  if (($part-&gt;type &gt;= 2) || (($part-&gt;ifdisposition == 1) &amp;&amp; ($part-&gt;disposition == 'ATTACHMENT')))
                  {
                      $filename = null;
                      if ($part-&gt;ifparameters == 1)
                      {
                          $total_parameters = count($part-&gt;parameters);
                          for ($i = 0; $i &lt; $total_parameters; $i++)
                          {
                              if (($part-&gt;parameters[$i]-&gt;attribute == 'NAME') || ($part-&gt;parameters[$i]-&gt;attribute == 'FILENAME'))
                              {
                                  $filename = $part-&gt;parameters[$i]-&gt;value;
                                  break;
                              }
                          }
                          if (is_null($filename))
                          {
                              if ($part-&gt;ifdparameters == 1)
                              {
                                  $total_dparameters = count($part-&gt;dparameters);
  
                                  for ($i = 0; $i &lt; $total_dparameters; $i++)
                                  {
                                      if (($part-&gt;dparameters[$i]-&gt;attribute == 'NAME') || ($part-&gt;dparameters[$i]-&gt;attribute == 'FILENAME'))
                                      {
                                          $filename = $part-&gt;dparameters[$i]-&gt;value;
  
                                          break;
                                      }
                                  }
                              }
                          }
                      }
                      $result['attachments'][] = array
                      (
                          'filename' =&gt; $filename,
                          'content' =&gt; str_replace(array("\r", "\n"), '', trim(imap_fetchbody($connection, $id, ($key + 1), FT_UID))),
                      );
                  }
  
                  else
                  {
                      if ($part-&gt;subtype == 'PLAIN')
                      {
                          $result['text'] = imap_fetchbody($connection, $id, ($key + 1), FT_UID);
                      }
  
                      else if ($part-&gt;subtype == 'HTML')
                      {
                          $result['html'] = imap_fetchbody($connection, $id, ($key + 1), FT_UID);
                      }
  
                      else
                      {
                          foreach ($part-&gt;parts as $alternative_key =&gt; $alternative_part)
                          {
                              if ($alternative_part-&gt;subtype == 'PLAIN')
                              {
                                  echo '&lt;h2&gt;' . $alternative_part-&gt;subtype . ' ' . $alternative_part-&gt;encoding . '&lt;/h2&gt;';
  
                                  $result['text'] = imap_fetchbody($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                              }
  
                              else if ($alternative_part-&gt;subtype == 'HTML')
                              {
                                  echo '&lt;h2&gt;' . $alternative_part-&gt;subtype . ' ' . $alternative_part-&gt;encoding . '&lt;/h2&gt;';
  
                                  $result['html'] = imap_fetchbody($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                              }
                          }
                      }
                  }
              }
          }
  
          else
          {
              $result['text'] = imap_body($connection, $id, FT_UID);
          }
  
          $result['text'] = imap_qprint($result['text']);
          $result['html'] = imap_qprint(imap_8bit($result['html']));
  
          return $result;
  
      }
  
      return false;
  }
  
   $emails = imap_search($connection,'ALL');
  //  rsort($emails);
  
    /* for every email... */
   foreach($emails as $email_number) {
       echo $email_number;
  $result = Message_Parse($email_number);
  //$data = $result['attachments'];
  $transport = Swift_MailTransport::newInstance();
  $mailer = Swift_Mailer::newInstance($transport);
  //$attachment = Swift_Attachment::newInstance($result['attachments'], $filename, 'audio/mp3');
  $message = Swift_Message::newInstance('test message1 ')
    -&gt;setFrom(array('from address' =&gt; 'name'))
    -&gt;setTo(array('to address.com'))
    -&gt;setBody($result['text']);
  //  -&gt;attach($attachment);
  
    $result1 = $mailer-&gt;send($message);
   }
  
  ?&gt;
  </code></pre>