+ Start a Discussion
JeriMorrisJeriMorris 

VF Email Template with a PNG Attachment

I need to create a VF Email template that creates an email message with a PNG attachment. The PNG content comes from an Attachment to the record that is the relatedToType for the email message.

 

I've created an email template that includes a messaging:attachment with renderAs="image/png" where the attachment's content comes from a custom component.

 

The template looks like this:

 

<messaging:emailTemplate subject="Test With Generated Attachment" recipientType="User" relatedToType="Account">
    <messaging:htmlEmailBody >
    <html>
    <body>
    <p>This email contains a PNG attachment where the content comes from an Attachment to the Account.</p>
    </body>
    </html>
    </messaging:htmlEmailBody>

    <messaging:attachment renderAs="image/png"  filename="test.png">
        <c:Sample />
    </messaging:attachment>
</messaging:emailTemplate>

 

The custom component looks like this:

 

<apex:component controller="Sample" access="global" >{!thePNG}</apex:component>

 

The component's controller looks like this:

 

public class Sample {
    public Blob thePNG {
        get {
            if (thePNG == null) {
                Attachment att = [select Body from Attachment where ParentId='001E000000Lr7wW' and Name='Casey.png' limit 1];

                // Returning att.Body results in an email message with a PNG attachment,
                // but the attachment can't be opened as a PNG
                return att.Body;
                
                // Assuming att.Body is Base64-encoded, I tried decoding it, but
                // the email doesn't appear to be sent.
                //return EncodingUtil.base64Decode(att.Body.toString());
            }
            return thePNG;
        }
        set;
    }
}

 Eventually, the query in the controller will be more sophisticated, getting the PNG from an Attachment related to the account record it's called with, but for now, it's simple. The point is that the PNG isn't in a well-known place like in a specific document -- it's in an Attachment related to the Account.

 

If the controller just returns the Attachment's Body, the email is sent with an attachment, but I can't open the PNG - it's not being sent as a valid PNG file. If I base64-decode the Body, the email doesn't even appear to be sent.

 

Given that the PNG is stored in an Attachment related to the Account, how can I include it as an email attachment in a VF email template?

 

Also, before you suggest it, I can't construct the email in Apex as a Messaging.SingleEmailMessage because the email has to be sent by workflow, not by Apex, to avoid the governor limits associated with the number of emails that can be sent via Apex in a 24-hour period.

 

Thanks for your help!

Best Answer chosen by Admin (Salesforce Developers) 
MrTheTylerMrTheTyler

Hi Jeri,

 

  Read this documentation page and you will find a statement that makes your objective appear not possible with the current platform - "Things to note about using renderAs: Currently, PDF is the only supported content converter."

 

  I've personally have been hacking on this problem for 2 hours since it seemed interesting/challenging and all approaches confirm the above - that unless the renderAs="PDF", the platform will simply convert whatever is in between the <messaging:attachment> tag as raw text.

 

  My suggestion would be to use a PDF attachment that contains the image or host the png file via a public site that the user can download from.

 

 

Hope This Helps!

 

Tyler Hudson
Contact Us - We Can Help!

Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x123

All Answers

MrTheTylerMrTheTyler

Hi Jeri,

 

  Read this documentation page and you will find a statement that makes your objective appear not possible with the current platform - "Things to note about using renderAs: Currently, PDF is the only supported content converter."

 

  I've personally have been hacking on this problem for 2 hours since it seemed interesting/challenging and all approaches confirm the above - that unless the renderAs="PDF", the platform will simply convert whatever is in between the <messaging:attachment> tag as raw text.

 

  My suggestion would be to use a PDF attachment that contains the image or host the png file via a public site that the user can download from.

 

 

Hope This Helps!

 

Tyler Hudson
Contact Us - We Can Help!

Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x123

This was selected as the best answer