Wednesday, August 24, 2011

Pictures from GMail to Picasa Web Albums with Google Apps Script

Recently I've been receiving quite a lot of pictures as e-mail attachments. These pictures are automatically and regularly sent to me from an IP Camera. At first, I found this amusing to watch new pictures each time I received a new e-mail. But it quickly became annoying having many unread e-mails I didn't want to see right now.
Well, filtering them in a specific label and setting them as read was quickly done.
What now if I find time to have quick look at new pictures. I still have to open every e-mail one by one and click on every photo if I want to display it in its original size...? Definitely not what I want.

Obviously an online photo storage like Picasa Web Albums is what I should use but my pictures still comes in my inbox/label.

I came up with several ideas to copy these pictures to Picasa Web Albums:
Let's have a quick look at both ideas.
Using filters in GMail, you can easily forward an e-mail to a given e-mail address.

As a matter of fact, Picasa offers a way to send new photos to a specific album. You just have to define a secret word in your Picasa settings.

The only problem now is that auto-forwarding in GMail needs e-mail address validation. When you try to add your new address it sends an e-mail containing a code you then have to type in order to confirm you own or at least you know the owner of the address to forward to.
As you might have guessed, there is no way to get to that confirmation e-mail. Too bad...

Now the second idea: using Google Apps Script to access my GMail inbox, find my pictures and send them using Picasa Web Albums Data API.

There are two ways to create a Google Apps Script:
  • from Google SpreadSheet
  • from Google Sites
I chose to use Google Sites for this, simply because it offers a nicer interface to manage scripts.
First thing is to create a Google Sites site or use an existing one.
Then go to More actions > Manage site > Apps Script and create a new script.

Now let's begin the code part by looking for our e-mails:

    //All my pictures are in e-mails filtered and put in this label.
    var myLabel = "myLabel"; //<-- the label as you typed it in GMail.
    var gLabel = GmailApp.getUserLabelByName("myLabel");
    var threads = gLabel.getThreads();
    for (var x in threads) {
      var thread = threads[x];
      var msgs = thread.getMessages();
      for (var y in msgs) {
        var msg = msgs[y];
        var attachments = msg.getAttachments();
        //ToDo: do something with these attachments.

Easy right ? The attachments variable is actually an array of Blobs.

The next step is to send them to Picasa. A first look at Google Apps Script documentation shows support for lot of Google services like Google Docs, Google Calendar or again Google Contacts but no direct support for Picasa Web Albums.

Well Romain Vialard, a Google Apps Script guru, gave a first hint with a nice peace of code allowing to authenticate to Picasa Web Albums Data API using 3-Legged OAuth and get the album list feed. Almost what we want !
The only difference is that we want to post our attachments to a given album.
In other words, we want to post our attachments or should I say their binary content. So using Romain's code, posting an attachment using Picasa Web Album API would mean something like that:

var url = ""
   var requestData = {
      "method": "POST",
      "contentType": "image/jpeg"
      "payload": attachment.getBytes(),
      "oAuthServiceName": "picasaweb",
      "oAuthUseToken": "always"
    var result = UrlFetchApp.fetch(url, requestData);

But the problem is that UrlFetchApp.fetch() method's doesn't accept bytes as payload (yet?) and only wants a String.
I quickly googled for a way to send those bytes anyway but couldn't come up with a working solution, so if anyone ever got to make this works, any comment is more than welcomed.

For now we have to find a way to send those pictures where we want them to be: in our Picasa album.
We actually already have our solution from our first idea: sending them to our newly created e-mail address.

And here is the corresponding code:

var attachments = msg.getAttachments();

    var albumTitle = "yourAlbumTitle";
    var picasaEmailAddr = "";
    var options = {
      "attachments": attachments

    //Subject must be you album title
    GmailApp.sendEmail(picasaEmailAddr, albumTitle, "", options);
    //Marking e-mail as read (why not ?)
    //Don't wanna keep e-mails/attchments once they are in Picasa.

And we're done !
Of course, you should not forget to catch errors and implement a way to be notified of any problem.


  1. I attached a jpg file to an email and sent the same to my picasa address 112373zopfan at picasaweb com but the same is not appearing in my picasa photos.

    What do I do?

  2. Hi vovvy !
    Sorry for the late response.
    Do you still have the problem ?
    Did you try sending this file "manually" from GMail to your PicasaWeb e-mail address just to make sure this works fine.
    Let me know how I can help. You can even provide me some of your code if you want to have a look .

  3. Wow what a Great Information about World Day its very nice informative post. thanks for the post. Buy Business Google Reviews,

  4. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Buy Social Mail Accounts