General Musing

blaze your trail

Proof of Concept: Google Docs Mail Merge Form with Text and HTML #wordpress

with one comment

I needed a way to be able to shamelessly plug the posts I recently bundled into the booklet “Write Something” again.In a similar way to the last time I did it in Proof of Concept: Google Docs Mail Merge Form

As I explained before I have set up a system to automatically mail somebody when the enter their address in the form, my issue was that I wanted to add a unique blogpost which they would only be able to get by signing up. Naturally I wanted to style it in the same way the posts are styled in this blog. Again I turned to the documentation, specifically the Class MailApp which I was using to send the mail. Using this documentation I had a starting point. I wanted three changes to the current script:

  1. keep the plain text
  2. add HTML message
  3. add inline images

First I created a new function to practice in called inlineImage in the Script Editor and a template to send in B2. The inline image retrieval is easily done by changing the example script to include the image I wanted te retrieve, and remembering to also edit the template image tag to refer to cid:writeSomeBlob rather than the true url. This embeds the image in the mail, and often means it passes through mail display filters.

And I needed to pass the variables I wanted to use to the new inlineImage function which could replace my call to MailApp.sendEmail in the original code.

function inlineImage(to,template) {    var writeSomeBlob =
    UrlFetchApp.fetch("http://farm4.static.flickr.com/3169/3109259359_550b7d1a32_m.jpg").
        getBlob().setName("writeSomeBlob");
// fetch image to embed

  MailApp.sendEmail(
    to,
    template.getRange("A1").getValue(), // mailaddress
    template.getRange("A2").getValue(), // plain text
    { htmlBody:
     template.getRange("B2").getValue(), // html
     inlineImages:
         {
           writeSome:writeSomeBlob // embed image
         }
    }
  );
}

Finally you replace the call to MailApp.sendEmail in sendEmails with a call to inlineImage.

inlineImage(rowData.mailAddress,templateSheet);

Read previous article in series…

And ensure that the Triggers functionality is still pointing to sendEmails. (Goto the menu, select the current project’s triggers… and verify the trigger which should be run On form submit.)

Image source: Search Engine People

Written by Daniël W. Crompton (webhat)

February 24, 2012 at 12:50 pm

One Response

Subscribe to comments with RSS.

  1. It’s really useful for me! Thanks for infomation.

    fat cow hosting

    March 12, 2012 at 2:39 pm


Please Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: