Gmail save image programmatically

346 views Asked by At

I run a small website. I want my users to send an email with attached photos from their mobile devices to our website gmail address. Is there a way of programmatically logging into gmail and saving the image file locally?

I realize that 99.9% of the time this is done by letting users upload the image file directly on the website. But I am wondering if there is a way to do this using email, because in this particular case it is more user friendly.

Cheers.

3

There are 3 answers

0
Grokify On

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:

  1. log into Gmail using IMAP or POP3
  2. retrieve the MIME emails
  3. 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.

0
mba12 On

Yes, you'll need to code pop3 email reading in your language of choice.

Java: http://www.oracle.com/technetwork/java/javamail/index.html
Python: https://docs.python.org/2/library/poplib.html
PHP: https://code.google.com/a/apache-extras.org/p/phpmailer/wiki/PHPMailer

Here's a good example in java: http://alvinalexander.com/java/javamail-pop-pop3-reader-email-inbox-example

0
Amit Agarwal On

You can use a Google script to save Gmail attachments to Google Drive automatically.

Next installs the PC client from Drive on your computer and the downloaded files will instantly appear locally.