3

this question: says that it is "hard" to put a component inside a visualforce email template because ' needs to be a child of component.'. Yet, is there a way to make this work?

We also need to stick to using the visualforce email template and cannot send the email in a different way. The object on which the email template is running has attachments. How can this be done?

3 Answers 3

2

I don't think you can do it with pure email template, you'll need some Apex, as suggested in other answers.

<messaging:emailTemplate> complains as soon as you'd try to stick <apex:repeat> in there (for example to create <messaging:attachment> nodes in a loop).

Even if you could - try referring to {!relatedTo.Attachments[0].Body} somewhere (can be in the plaintextBody tag), you'll get an error:

Binary fields cannot be selected in join queries

I think the closest it can get is to create the links to attachments (and if I recall correctly - SF converts attachments > 3 MB to links anyway).

<messaging:emailTemplate subject="hello" recipientType="Contact" relatedToType="Opportunity">
<messaging:htmlEmailBody >
    <p>This will list link to attachments... it's not a perfect solution but it's something.</p>

    <apex:repeat value="{!relatedTo.Attachments}" var="a">
    <p><a href="{!URLFOR($Action.Attachment.Download, a)}">{!a.Name}</a> ({!a.BodyLength} B)</p>
    </apex:repeat>
</messaging:htmlEmailBody>
<messaging:plainTextEmailBody >
    <apex:repeat value="{!relatedTo.Attachments}" var="a">
        {!a.Name} {!a.ContentType} {!a.Body} <!-- this "Body" reference causes the compilation to fail -->
    </apex:repeat>
</messaging:plainTextEmailBody>

<!--
<messaging:attachment filename="{!relatedTo.Attachments[0].Name}" renderAs="{!relatedTo.Attachments[0].ContentType}">
 {!relatedTo.Attachments[0].Body}  :(
</messaging:attachment>
-->
</messaging:emailTemplate>
3
  • You could always go with something a bit less dynamic with my second option I outlined here salesforce.stackexchange.com/questions/21705/… Dec 16, 2013 at 10:12
  • @PhilHawthorn but that would work for anything "PDF-able" only I think? Images should be ok but If I'd want to attach an excel file, a word doc, maybe another PDF with a scanned & signed copy of the contract (Inception ;))
    – eyescream
    Dec 16, 2013 at 10:28
  • ah yes, good point - overlooked that Dec 16, 2013 at 10:43
0

see this http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_templates_with_apex.htm.

Hope it may helpful

0

user670186,

Not sure if this is the answer you're looking for but here's my thoughts:

If you have a VF template ready, and if consider using messaging.singleemailmessage methods to send the email, you can - Use mail.setTemplateId to take the visualforce as template - Use Messaging.EmailFileAttachment to attach the file in the email.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .