title: Email forwarding in PHP
body: <p>My following code is tested by my friends. They said its working fine. But I am unable to use it. Can anybody find where the error is.</p>
  
  <p>`</p>
  
  <pre><code>&lt;?php
    try
    {
    // Change to your mail server
    $host = "pop.gmail.com";
  
    // Connecting to POP3 email server.
    $connection = imap_open("{" . $host . ":995/pop3/notls}", 'someusername@gmail.com', 'somepassword');
  
    // Total number of messages in Inbox
    $count = imap_num_msg($connection);
    echo $count . " messages found&lt;br /&gt;";
  
    // Read Messages in Loop, Forward it to Actual User email and than delete it from current email account.
    for ($i = 1; $i &lt;= $count; $i++) {
        $headers = imap_headerinfo($connection, $i);
  
        $subject = $headers-&gt;subject;
  
        $from = $headers-&gt;from[0]-&gt;mailbox . '@' . $headers-&gt;from[0]-&gt;host;
        if ($headers-&gt;cc[0]-&gt;mailbox)
            $cc = $headers-&gt;cc[0]-&gt;mailbox . '@' . $headers-&gt;cc[0]-&gt;host;
        $subject = $headers-&gt;subject;
  
        $structure = imap_fetchstructure($connection, $i);
        $type = $this-&gt;get_mime_type($structure);
  
        // GET HTML BODY
        $body = $this-&gt;get_part($connection, $i, "");
  
        //$raw_body = imap_body($connection, $i);
  
        $attachments = array();
  
        if (isset($structure-&gt;parts) &amp;&amp; count($structure-&gt;parts)) {
            for ($e = 0; $e &lt; count($structure-&gt;parts); $e++) {
                $attachments[$e] = array('is_attachment' =&gt; false, 'filename' =&gt; '', 'name' =&gt; '', 'attachment' =&gt; '');
  
                if ($structure-&gt;parts[$e]-&gt;ifdparameters) {
                    foreach ($structure-&gt;parts[$e]-&gt;dparameters as $object) {
                        if (strtolower($object-&gt;attribute) == 'filename') {
                            $attachments[$e]['is_attachment'] = true;
                            $attachments[$e]['filename'] = $object-&gt;value;
                        } //if (strtolower($object-&gt;attribute) == 'filename')
                    } //foreach ($structure-&gt;parts[$e]-&gt;dparameters as $object)
                } //if ($structure-&gt;parts[$e]-&gt;ifdparameters)
  
                if ($structure-&gt;parts[$e]-&gt;ifparameters) {
                    foreach ($structure-&gt;parts[$e]-&gt;parameters as $object) {
                        if (strtolower($object-&gt;attribute) == 'name') {
                            $attachments[$e]['is_attachment'] = true;
                            $attachments[$e]['name'] = $object-&gt;value;
                        } //if (strtolower($object-&gt;attribute) == 'name')
                    } //foreach ($structure-&gt;parts[$e]-&gt;parameters as $object)
                } //if ($structure-&gt;parts[$e]-&gt;ifparameters)
  
                if ($attachments[$e]['is_attachment']) {
                    $attachments[$e]['attachment'] = @imap_fetchbody($connection, $i, $e + 1);
                    if ($structure-&gt;parts[$e]-&gt;encoding == 3) {
                        // 3 = BASE64
                        $attachments[$e]['attachment'] = base64_decode($attachments[$e]['attachment']);
                    } //if ($structure-&gt;parts[$e]-&gt;encoding == 3)
                    elseif ($structure-&gt;parts[$e]-&gt;encoding == 4) {
                        // 4 = QUOTED-PRINTABLE
                        $attachments[$e]['attachment'] = quoted_printable_decode($attachments[$e]['attachment']);
                    } //elseif ($structure-&gt;parts[$e]-&gt;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 &lt; count($structure-&gt;parts); $e++)
        } //if (isset($structure-&gt;parts) &amp;&amp; count($structure-&gt;parts))
        /**** ****/
  
  
        /*echo "&lt;pre&gt;";
        echo "From: " . $headers-&gt;Unseen . "&lt;br /&gt;";
        echo "From: " . $from . "&lt;br /&gt;";
        echo "Cc: " . $cc . "&lt;br /&gt;";
        echo "Subject: " . $subject . "&lt;br /&gt;";
        echo "Content Type: " . $type . "&lt;br /&gt;";
        echo "Body: " . $body . "&lt;br /&gt;";*/
        $mail = new Zend_Mail();
  
        $mail-&gt;settype(Zend_Mime::MULTIPART_MIXED);
  
        for ($k = 0; $k &lt; count($attachments); $k++) {
            $filename = $attachments[$k]['name'];
            $filecontent = $attachments[$k]['attachment'];
  
            if ($filename &amp;&amp; $filecontent) {
                $file = $mail-&gt;createAttachment($filecontent);
                $file-&gt;filename = $filename;
            } //if ($filename &amp;&amp; $filecontent)
        } //for ($k = 0; $k &lt; count($attachments); $k++)
  
  
        $mail-&gt;setFrom($from);
        $mail-&gt;addTo('testmail@softmail.me');
        if ($cc)
            $mail-&gt;addCc($cc);
        $mail-&gt;setSubject($subject);
        $mail-&gt;setBodyHtml($body);
        $mail-&gt;send();
  
        // Mark the email messages once read
        //imap_delete($mbox, 1);
    } //for ($i = 1; $i &lt;= $count; $i++)
    // Delete all marked message from current email account.
  
    imap_expunge($mbox);
    echo 'If you see this, the number is 1 or below';
    }
    catch(Exception $e)
    {
    echo 'Message: ' .$e-&gt;getMessage();
    }
  ?&gt;
  </code></pre>