1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Mail attachment download

Discussion in 'PHP' started by Jon@30442, Apr 29, 2021.

  1. #1
    Hi,
    I have a php script which I use to download an email attachment from my inbox to a folder on my site.
    When the email is sent direct from the server that creates the .csv report, the script recognizes there is an email, but does not download the email attachment. If I have that email sent to my email (outlook exchange) and then I forward it to my site, the script works - sees the email and downloads the attachment to the specified folder.
    I cant work this out. Any clues?

    I don't think it is an issue with the script as I use pretty much the same code for the same purpose in other areas of the site, I feel its to do with the attachment being created in the first place. Unfortunately, I can do much with the creation of the csv file, I can only change it to an excel file of a PDF.

    Here is the script
    
    
    
    
    set_time_limit(3000);
    // ******************************************************
    // Part 1 Transfer
    // ******************************************************
    //
    
    
    $hostname = '{nysite.com:993/imap/ssl}INBOX';
    $username = 'files@mysite.com';
    $password = 'password';
    $saveloc = 'DataFiles/';
    $backUpsaveloc = 'DataFilesBackup/';
    
    
    
    sleep(3);
    $inbox = imap_open($hostname,$username,$password) or die('cant connect');
    $emails = imap_search($inbox,'ALL');
    $max_emails = 20;
    
        if($emails) {
    
        $count = 1;
        rsort($emails);
       
        foreach($emails as $email_number)
            {
            $overview = imap_fetch_overview($inbox,$email_number,0);
            $message = imap_fetchbody($inbox,$email_number,2);
            $structure = imap_fetchstructure($inbox, $email_number);
            $attachments = array();
            if(isset($structure->parts) && count($structure->parts))
                {
                for($i = 0; $i < count($structure->parts); $i++)
                    {
                    $attachments[$i] = array(
                        'is_attachment' => false,
                        'filename' => '',
                        'name' => '',
                        'attachment' => ''
                        );
                        if($structure->parts[$i]->ifdparameters)
                        {
                            foreach($structure->parts[$i]->dparameters as $object)
                            {
                                if(strtolower($object->attribute) == 'filename')
                                {
                                    $attachments[$i]['is_attachment'] = true;
                                    $attachments[$i]['filename'] = $object->value;
                                }
                            }
                        }
        
                        if($structure->parts[$i]->ifparameters)
                        {
                            foreach($structure->parts[$i]->parameters as $object)
                            {
                                if(strtolower($object->attribute) == 'name')
                                {
                                    $attachments[$i]['is_attachment'] = true;
                                    $attachments[$i]['name'] = $object->value;
                                }
                            }
                        }
       
                        if($attachments[$i]['is_attachment'])
                        {
                            $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
                             if($structure->parts[$i]->encoding == 3)
                            {
                                $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                            }
                            elseif($structure->parts[$i]->encoding == 4)
                            {
                                $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                            }
       
                            
                        }
                       
                    }
                    sleep(5);
           //       imap_delete($inbox, $email_number);
                    sleep(2);
                }
              
               
               
                $type=".txt";
    
                foreach($attachments as $attachment)
                {
                    if($attachment['is_attachment'] == 1)
                    {
                        $filename = $attachment['filename'];
                        $subject = $overview[0]->subject ;
                        $subject = str_replace(' ', '', $subject);
                        if (substr($subject,0,3) == "FW:")
                        {
                            $subject = substr($subject, 3);
                        }
                       
                        $fp = fopen($saveloc .$subject .$type, "w+");   
                        fwrite($fp, $attachment['attachment']);
                        $fpb = fopen($backUpsaveloc .$subject .$type, "w+");   
                        fwrite($fpb, $attachment['attachment']);
                       
                        fclose($fp);
                        }
                       
                    }
                    if($count++ >= $max_emails) break;
                }
            } 
    sleep(10);
    //imap_expunge($inbox);
    imap_close($inbox);
    sleep(10);
    
    
    PHP:
     
    Jon@30442, Apr 29, 2021 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    It seems to me that the script on your server, which is creating the attachment is not doing its work as expected. Missing some headers etc while creating email.
    This is why this download script is not parsing the email correctly, missing the attachment.

    When you forward it to outlook and resend, then outlook is fixing the error, and script fetches the download.
     
    JEET, May 1, 2021 IP
  3. Jon@30442

    Jon@30442 Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Hi,
    Yes that is also my suspicion. Unfortunately, I have no way of modifying what get sent, its the cards I'm dealt.
    I guess I need to keep looking for a script that will read the attachment. There has to be a way, email clients can read the emails.
     
    Jon@30442, May 2, 2021 IP