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.
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:
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
andContent-Disposition
set toattachment
. 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.