Fetching attachments from gmail via either python or php

I have been trying to find information on how to retrieve attachments from a gmail account in either python or PHP, I'm hoping that someone here can be of some help, thanks.

Related:


Asked by: Emma534 | Posted: 06-10-2021






Answer 1

You will have to enable IMAP access to your GMail account (Settings → Forwarding and POP/IMAP), and then use imaplib.IMAP4_SSL to access it.

Use the raw text of every message as an argument to email.message_from_string in order to process any attachments.

Answered by: Cadie223 | Posted: 07-11-2021



Answer 2

I took the code above and fixed it and tested it. It works with PHP5.

<?php 

$gmail_username = 'email@gmail.com';
$gmail_password = 'password';
$imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $gmail_username, $gmail_password) or die("can't connect: " . imap_last_error());
$savefilepath = '//Server/share/Local/Pathname/'; //absolute path to images directory
$imagefilepath = '/Local/Pathname/'; //relative path to images directory
$savethumbpath = '/Local/Pathname/'; //relative path to images directory
$headers = imap_headers($imap);
foreach ($headers as $mail) {
    $flags = substr($mail, 0, 4);
    //Check for unread msgs, get their UID, and queue them up
    if (strpos($flags, "U")) {
        preg_match('/[0-9]+/',$mail,$match);
        $new_msg[] = implode('',$match);     
    }
}
if ($new_msg) {
    foreach ($new_msg as $result) {
        $structure = imap_fetchstructure($imap,$result);
        $parts = $structure->parts;
        foreach ($parts as $part) {
            if ($part->parameters[0]->attribute == "NAME") {
                //Generate a filename with format DATE_RANDOM#_ATTACHMENTNAME.EXT
                $savefilename = date("m-d-Y") . '_' . rand() . '_' . $part->parameters[0]->value;
                   save_attachment(imap_fetchbody($imap,$result,2),$savefilename,$savefilepath,$savethumbpath);
                imap_fetchbody($imap,$result,2); //This marks message as read
            } 
        }
    }
}
/* grab emails */
$emails = imap_search($imap,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
  /* put the newest emails on top */
  $total = imap_num_msg($imap);
  /* for every email... */
  for( $i = $total; $i >= 1; $i--) {
        $headers = imap_header($imap, $i);
        $from = $headers->from[0]->mailbox . "@" . $headers->from[0]->host;
        echo $from . "\n";
        imap_delete($imap,$i);
        imap_mail_move($imap,"$i:$i", "[Gmail]/Trash"); // Change or remove this line if you are not connecting to gmail. The path to the Trash folder in your Gmail may be different, capitalization is relevant.
  }
}else{
        echo "no emails";
}
/* close the connection */
   imap_expunge($imap);
   imap_close($imap);

function save_attachment( $content , $filename , $localfilepath, $thumbfilepath ) {
     if (imap_base64($content) != FALSE) {   
         $file = fopen($localfilepath.$filename, 'w');
         fwrite($file, imap_base64($content));
         fclose($file);
     }
}
?>

Answered by: Daisy538 | Posted: 07-11-2021



Answer 3

I found some code that works! this will download any attachments to a selected folder

<?php 

$gmail_username = 'username@gmail.com';
$gmail_password = 'mypassword';

$imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $gmail_username, $gmail_password) or die("can't connect: " . imap_last_error());
$savefilepath = 'path/to/images_folder/'; //absolute path to images directory
$imagefilepath = 'images/'; //relative path to images directory

$headers = imap_headers($imap);
foreach ($headers as $mail) {
    $flags = substr($mail, 0, 4);
    //Check for unread msgs, get their UID, and queue them up
    if (strpos($flags, "U")) {
        preg_match('/[0-9]+/',$mail,$match);
        $new_msg[] = implode('',$match);     
    }
}

if ($new_msg) {
    foreach ($new_msg as $result) {
        $structure = imap_fetchstructure($imap,$result);
        $parts = $structure->parts;
        foreach ($parts as $part) {
            if ($part->parameters[0]->attribute == "NAME") {
                //Generate a filename with format DATE_RANDOM#_ATTACHMENTNAME.EXT
                $savefilename = date("m-d-Y") . '_' . mt_rand(rand(), 6) . '_' . $part->parameters[0]->value;
                save_attachment(imap_fetchbody($imap,$result,2),$savefilename,$savefilepath,$savethumbpath);
                imap_fetchbody($imap,$result,2); //This marks message as read
            } 
        }
    }
}

imap_close($imap);

function save_attachment( $content , $filename , $localfilepath, $thumbfilepath ) {
    if (imap_base64($content) != FALSE) {   
        $file = fopen($localfilepath.$filename, 'w');
        fwrite($file, imap_base64($content));
        fclose($file);
    }
}
?>

Answered by: Ted103 | Posted: 07-11-2021



Answer 4

I recently worked on this topic and here is the code which works well. It also saves the details of the attachments in a word document with the following details:
-> Date
-> Time
-> From
-> Email ID
-> Subject










<?php 


 /*
    *   Gmail attachment extractor.
    *
    *   Downloads attachments from Gmail and saves it to a file.
    *   Uses PHP IMAP extension, so make sure it is enabled in your php.ini,
    *   extension=php_imap.dll
    *
    */

    header('Content-type:application\msword');
    header('Content-Disposition:attachment;Filename=document_name.doc');
    set_time_limit(0); 
    /* connect to gmail with your credentials */
    $hostname = '{imap.googlemail.com:993/imap/ssl}INBOX';
    $username = 'email@gmail.com';
    $password = 'password';
    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to       
    Gmail: ' . imap_last_error());
    /* get all new emails. If set to 'ALL' instead 
    * of 'NEW' retrieves all the emails, but can be 
    * resource intensive, so the following variable, 
    * $max_emails, puts the limit on the number of emails downloaded.
    * 
    */
    $emails = imap_search($inbox,'ALL');
    /* useful only if the above search is set to 'ALL' */
    $max_emails = 110;


    /* if any emails found, iterate through each email */
    if($emails){
    $count = 1;

    /* put the newest emails on top */
    rsort($emails);
    /* for every email... */
    foreach($emails as $email_number){
    /* get information specific to this email */
    //$overview = imap_fetch_overview($inbox,$email_number,0);
    $check=imap_check($inbox);
    $result=imap_fetch_overview($inbox,"1:{$check->Nmsgs}",0);
    foreach($result as $overview){
    echo "#{$overview->msgno}({$overview->date})-From: {$overview->from}
    {$overview->subject}\n"}
    /* get mail message */
    $message = imap_fetchbody($inbox,$email_number,2);
    /* get mail structure */
    $structure =imap_fetchstructure($inbox, $email_number);
    //$functions = array('function1' => imap_fetchstructure($inbox,
    $email_number));
    //print_r(array_keys($functions));
    $attachments = array();
    //print_r(array_keys($attachments[$i]));
    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);
     /* 4 = QUOTED-PRINTABLE encoding */
     if($structure->parts[$i]->encoding == 3){ 
     $attachments[$i]['attachment'] = base64_decode($attachments[$i]  
           ['attachment']);
      }
      /* 3 = BASE64 encoding */
      elseif($structure->parts[$i]->encoding == 4){ 
      $attachments[$i]['attachment'] =              

      quoted_printable_decode($attachments[$i]['attachment']);
      }

       }
      }
     }
       /* iterate through each attachment and save it */
       foreach($attachments as $attachment)
       {
       if($attachment['is_attachment'] == 1){
        $filename = $attachment['name'];
        if(empty($filename)) $filename = $attachment['filename'];
        if(empty($filename)) $filename = time() . ".dat";
       /* prefix the email number to the filename in case two emails
       * have the attachment with the same file name.
       */
       $fp = fopen($email_number . "-" . $filename, "w+");
       fwrite($fp, $attachment['attachment']);
        fclose($fp);
        }

        }

          if($count++ >= $max_emails) break;


         }

       } 

        /* close the connection */

       imap_close($inbox);

  ?>

Answered by: First Name861 | Posted: 07-11-2021



Answer 5

The php docs for imap_open explain connecting to gmail in the comments (e.g. 31-Oct-2007 07:50):

$mbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX", "username@gmail.com", "password") or die("can't connect: " . imap_last_error());

Where, obviously, you have to fill in the actual username and password as appropriate, and then to identify the attachments in the parts of the email you follow the instructions from: http://www.electrictoolbox.com/extract-attachments-email-php-imap/

Which, summarizing, says that you use:

// in a for($i=1;$i<$nummsgs;$i++) loop over all the messages in the inbox $structure = imap_fetchstructure($mbox, $i);

to identify the attachments in the structure. However, that is a rather elaborate process of deconstructing MIME messages (which have a lot of optional variability that must be accounted for), so the fundamentals/outline of a function for that is in that electrictoolbox page.

Answered by: Carlos179 | Posted: 07-11-2021



Similar questions

Erratic behavior with email attachments using python smtplib

My email script is based on this script at Fine Frog. I'm using this script to send HTML log files from a number of remote machines using a variety of ISPs. The attachment isn't being sent consistently, however. It does work 80% of the time, but I'm getting two types of weird behavior. The first is where the em...


linux - python -- grabbing email attachments to disk?

I have a bunch of 500 emails, each with attachments. I'd like to write a python script that would scroll through all email messages, and saves these attachments to disk. How do I go about it? Also, suppose I could choose which email program will be used to retrieve the messages from the POP3 server -- would it be best to use Evolution? Thunderbird? some other program?


Python - Forward IMAP Email with Attachments (imaplib, smtplib)

I'm having trouble forwarding mail with attachments. Can anyone point me in the right direction? I'm guessing it's in the Fetch statement but am unsure. import sys import uuid import re import smtplib import email address = '' username = '' password = '' def checkMail(): M = imaplib.IMAP4_SSL(address) M.login(username, password) M.select("INBOX", readonly=True) typ, data = M.search(None, '...


Parsing Multipart emails in python and saving attachments

I am pretty new to python and I am trying to parse email from gmail via python's imaplib and email. It is working pretty well but I am having issues with email attachments. I would like to parse out all of the plaintext from the email while ignoring any HTML that may be inserted as a secondary content type while also removing and saving all other attachments. I have been trying the following: ...im...


base64 - Python mail: encoded attachments are truncated

I'm using the following function to send an email message with two attachments in my python script: import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate from email import Encoders ... def sendMail(sender_name, to, subject, text, files=None,server="localhost"): assert type(to...


python - Search for messages with attachments with GMail IMAP

How to find all emails with attachments in GMail account using IMAP access? I'm on Python but is there only way to do this is to check each message? Thanks


python - Is postfix maiming my HTML attachments?

Update: I just discovered this inside the postfix configuration documents. Any ideas on how I could include my graphic in this message and not encounter the character limit issue would be great. I've written a Python-based milter for postfix that extracts attachments and replaces them with an H...


How to extract attachments from msg files using Python

How do I extract attachments from msg files using Python...? import os import sys import email import errno import mimetypes from optparse import OptionParser if __name__ == "__main__": fp = open("C:/FNFData/emails/e_1512_1_message.msg") msg = email.message_from_file(fp) fp.close() counter = 1 print msg['From'] print msg['Subject'] for part in msg.walk(): # multipart/*...


python - How to extract attachments from msg files

How do I extract attachments from msg files using python...? Sample code snippet will be helpful...!


python - How do I download only unread attachments from a specific gmail label?

I have a Python script adapted from Downloading MMS emails sent to Gmail using Python import email, getpass, imaplib, os detach_dir = '.' # directory where to save attachments (default: current) user = raw_input("Enter your GMail username:") pwd = getpass.getpass("Enter your password: ") # connectin...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top