Email from Apex : A Breakdown of my CloudSpokes Chatter to Picasa Entry

I thought someone might find it useful to see how I approached my winning CloudSpokes Email Upload Chatter Pictures to Picasa entry a while back, specifically around how to send an email in Apex. I am posting this with permission from the CloudSpokes organizers  and this has been cross posted on their blog which you can find here. I have been asked to refrain from giving the full code so you won’t get the whole solution here, but it is available on this CloudSpokes GitHub repo.

So the idea was to email a photo to a Picasa web album leveraging the “Upload photos by email” functionality whenever a keyword was found in the post. I won’t go into this as Picasa appears to be getting engulfed by Google+ and I don’t know how the setup will work going forward. Regardless, I leveraged two triggers, the first was on the FeedItem object and the second was on the FeedComment object. In each trigger I essentially looked for a keyword in the chatter post/comment, defined during setup, and if found, grabbed the picture from the post and emailed it to Picasa.

Jeff Douglas did a wonderful job of explaining this all, so I’ll hand this over to him:

The important part that I want to share is how the email is sent. This is all handled inside a class which is called from either of the triggers. This is the definition of the SendEmail method. Not all of the parameters are needed to send an email, though they were necessary for this particular application.

public void SendEmail(FeedItem f,
                      String description,
                      String emailAddr,
                      String keyword,
                      Boolean AllowAlbumLoad){

First, you want to set up the basic message, setting the to address (which was passed in the emailAddr parameter), replyTo, subject (which comes from the post’s title), BccSender(sends email to the person sending the email) and PlainTextBody (which isn’t really used here).

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    //Set email address
    String[] toAddresses = new String[] {emailAddr};
    mail.setReplyTo(''); //the reply address doesn't matter
    mail.setBccSender(false);  //we don't want to Bcc ourselves on this
    mail.setPlainTextBody('Body Text - This will not be utilized, picasa will not use it.');

Once the basic email is set up, you need to create the attachment, which in this case was pulled from the feedItem passed in.

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); //create the attachment
    efa.setBody(f.ContentData); //set the body of the attachment to the Content Data
    efa.setFileName(f.ContentFileName); //set the file name appropriately
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});  //attach to the email

At this point you have an email and just need to send it. You do this by leveraging the Messaging class.

    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); //send the email
  1. No trackbacks yet.

Leave a Reply

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

You are commenting using your 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: