There are two basic approaches to this, one where you pull email from Gmail and another where Gmail pushes the email to you.
Poll Gmail (Pull)
Using this approach, you would periodically connect to Gmail to download new email messages. The rough order of steps is to:
- log into Gmail using IMAP or POP3
- retrieve the MIME emails
- parse the emails to retrieve the photos
When parsing the MIME emails, you can identify images by filtering for MIME parts that have Content-Type
set to image types, e.g. image/jpeg
and Content-Disposition
set to attachment
. The images may be encoded, e.g. Content-Transfer-Encoding: base64
, so you may want to decode the data before saving it.
There are libraries to help with this in most popular languages.
Here's a PHP example on Stack Overflow I wrote to download MIME messages from Gmail using IMAP:
Gmail Forwarding (Push)
If you prefer not to log into Gmail, you can set up a SMTP mail server (e.g. Postfix, Exim, Sendmail, etc.) to receive email and have Gmail forward you emails that it receives. This way, you wouldn't need to connect to Gmail over the network. Then on your server you can either periodically connect to it or you can write a filter in the server to process it for you. I've done both, using a local connection and writing mail server processing filters.