views:

1424

answers:

2

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:

+6  A: 

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.

ΤΖΩΤΖΙΟΥ
A: 

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", "[email protected]", "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.

Chris