function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Creating a VisualForce page overriding the 'EmailMessage' standard controller

I am attempting to create a VisualForce page which looks like the standard 'Send an Email' page.  To this page, I want  to add an extension which will automatically attach a document when the 'Send' button is clicked.

I can create a page which uses the 'EmailMessage' standard controller, but I cannot determine which component I must use to get the standard 'Send an email' page to load.  I have tried using the '<apex:detail>' component to no avail.

Is there a way to do this?  And if so, what elements should I be using to do this?  Further, is there any documentation prepared, or planned outlining what each of the standard controllers are, and what basic functionality is available for each?

I know that I could probably add a trigger to do what I want, but as I only want this to happen on a specific page I don't want to have to try to code the trigger to prevent the attachment from being attached to every outgoing email.

For those who are wondering, my business case is that I have a VisualForce page which generates an Invoice for our customers.  I want to provide a link to our end users on this page, which directs the user to a VisualForce page which looks like the 'Send an email' page.  On this new page, they can use the standard email functionality to write an email to a contact at the client site (including adding any templates they would like).  When they click 'Send', I want to attach a copy of the Invoice, generated as a pdf.

Any help would be appreciated.

Message Edited by m_roark on 12-02-2008 01:37 PM
m_roark -

Thanks for the detailed explanation - it helps.  Not sure if Chapter 13 in the Developer Guide would help you, but it does have an example of including an attachment with an email message.

I have looked at this functionality, and I could use it to create a page where my users can provide an email address, a subject, and a body, and sends an email to someone.  However, I want to provide the rich features of the standard 'Send an email' page, including Contact lookup, the ability to add templates, and the ability to add additional attachments. 

As I said in my original post, I have tried using the 'EmailMessage' standard controller for my VisualForce page, in the hopes that this standard controller exposes the appropriate elements, so that I could display an <apex:inputField> which was tied to the 'To:' Contact field, and would render as a Contact lookup.  However, I cannot figure out what the name is for the appropriate exposed field to use in the to my inputField.  I don't even know if there is one, due to the lack of documentation around standard controllers.

After reading the full of the chapter, what I want to do does not seem possible in the way I want to do it.  However, it does seem that I can make a VisualForce template, which will generate the pdf I want as an attachment using the <messaging:attachment> component.  This way, my users can select 'Send an email' from the Invoice custom object, and select my VisualForce template which will automatically include the dynamically generated Invoice pdf as an attachment!  I will be researching this further, and if this works for my use case, then I will post to note that this is a viable solution.

Message Edited by m_roark on 12-02-2008 02:43 PM
As promised, I am posting again to indicate that the solution I mentioned above, using a VisualForce template and the 'messaging:attachment' component, works to resolve my business case.
Thanks so much for posting the results, and glad you found a solution!
Could you include the code lines you used for <messaging:attachment> ? I've been trying to do something similiar. All my attempts of using the attachment componet give me a PDF file, but it never includes the the Visualforce Page I have created as the body. The PDF is simply a blank file.
 Here is my full source.  Basically, you use the 'messaging:attachment' to store the code for the VisualForce page you want to render as an attachment.  When you inputting this code into the 'messaging:attachment' component,
do not include a 'apex: page' component.  The 'messaging:attachment' takes the place of this component. 

This VisualForce template depends upon a custom object we use for tracking our invoices and invoice line items (so don't expect it to work immediately if you drop it into your environment), but could be easily modified to use a standard object or your own custom object.

<messaging:emailTemplate subject="Invoice" recipientType="Contact" relatedToType="License_Invoice__c">
<messaging:plainTextEmailBody >
Dear {!recipient.FirstName},

{!recipient.Account.Owner.FirstName} {!recipient.Account.Owner.LastName} asked me to send you the attached electronic copy(ies) of your {!relatedTo.Type__c} invoice for {!relatedTo.Product__c}. Please make check payable and remit via overnight courier to:


Please call {!recipient.Account.Owner.FirstName} at {!recipient.Account.Owner.Phone} if you have any questions or concerns.

Best regards,
<messaging:attachment renderAs="pdf" filename="{!relatedTo.Name}LineItem.pdf" ><style type="text/css">

.odd { 

background-color: White;}


{background-color: LightSteelBlue;}










</style><table width="653">
<td><apex:image rendered="true" value="{!$Resource.Logo}" width="233"></apex:image></td>
<td align="right">
<apex:outputText rendered="true" style="font-size:8pt" value="<company address line 1>"/><br/>
<apex:outputText rendered="true" style="font-size:8pt" value="<company address line 2>"/><br/>
<apex:outputText rendered="true" style="font-size:6pt" value="Tel "/>
<apex:outputText rendered="true" style="font-size:8pt" value="<company phone>"/>
<apex:outputText rendered="true" style="font-size:6pt" value="Fax "/>
<apex:outputText rendered="true" style="font-size:8pt" value="<company fax>"/><br/>
<apex:outputText rendered="true" style="font-size:8pt" value="<company website>"/>
</td></tr></table> <table><tr><td width="20px"></td><td>
<table style="WIDTH: 653px; HEIGHT: 55px" cellspacing="0" cellpadding="0">


<td class="largeText"><apex:outputText >{!relatedTo.Bill_To__r.Full_Name__c}{!relatedTo.Bill_To__r.Suffix__c}  </apex:OutputText></td>

<td align="right">Invoice Date:&nbsp;&nbsp;<apex:outputText style="font-size:8pt" value="{0}/{1}/{2}"><apex:param value="{!month(today())}"/><apex:param value="{!day(today())}"/><apex:param value="{!year(today())}"/> </apex:outputText></td></tr>

<tr><td class="largeText">

<apex:outputText >{!relatedTo.Bill_To__r.Title}</apex:outputText></td>

<td align="right">Invoice No:&nbsp;&nbsp;<apex:outputText style="font-size:8pt" value="{!relatedTo.Name}"></apex:outputText></td></tr>

<tr><td class="largeText">

<apex:outputText ><b>{!relatedTo.Bill_To__r.Account.Name}</b></apex:outputText></td>

<td align="right" style="font-size:8pt">Account No: {!relatedTo.Bill_To__r.Account.Billing_Code__c}</td></tr>

<tr><td class="largeText">

<apex:outputText >{!relatedTo.Bill_To__r.MailingStreet}</apex:outputText></td>

<td align="right"></td></tr>

<tr><td class="largeText">

<apex:outputText >{!relatedTo.Bill_To__r.MailingCity}</apex:outputText>,&nbsp;<apex:outputText >{!relatedTo.Bill_To__r.MailingState}&nbsp;<apex:outputText >{!relatedTo.Bill_To__r.MailingPostalCode}</apex:outputText></apex:outputText></td>

<td align="right"><apex:outputText style="font-size:8pt" value="For your reference our "></apex:outputText><apex:outputText style="font-size:8pt;font-weight:bold" value="TIN is <company TIN>"></apex:outputText></td></tr>


<table style="WIDTH: 653px">

<tr><td align="center" class="largeText"><Company Name> Invoice</td></tr>

<tr><td align="center" class="largeText">{!relatedTo.Type__c}</td></tr>


<apex:dataTable width="653px" rules="all" border="1" cellpadding="2" value="{!relatedTo.Invoice_Products__r}" var="ip" rowClasses="odd,even" headerClass="tableHeader">

<apex:facet name="caption"></apex:facet>

<apex:facet name="header"></apex:facet>

<apex:facet name="footer"></apex:facet>

<apex:column >

<apex:facet name="header">Product</apex:facet>

<apex:outputText value="{!ip.Full_Product__c}"/>


<apex:column ><apex:facet name="header">Amount</apex:facet>

<apex:outputField value="{!ip.Amount__c}"/>



<table width="653px"  class="largeText"><tr><td align="left"><strong>Payment Due By &nbsp;<apex:outputText value="{0}/{1}/{2}"><apex:param value="{!month(relatedTo.Invoice_Due__c)}"/><apex:param value="{!day(relatedTo.Invoice_Due__c)}"/><apex:param value="{!year(relatedTo.Invoice_Due__c)}"/> </apex:outputText></strong></td>

<td align="right"><strong>Total Due:&nbsp;&nbsp;&nbsp;<apex:outputField value="{!relatedTo.Invoice_Amount__c}"/></strong></td></tr></table>


            <br />

            <font face="Arial" class="largeText">Invoice Comments:<br />

            </font><apex:outputText style="font-size:8pt;font-names=Arial;font-bold=true;" value="{!relatedTo.Notes__c}"></apex:outputText>

            <br />




            <hr style="WIDTH: 646px; HEIGHT: 2px" align="left" size="2" />

            <br />

            <font size="1"><font face="Arial"><strong>Please make check payable and remit by overnight

            courier to:<br />


            <br />

<company address>
            <br />

            <br />


            <table width="653">



                        <td align="center">

                            <font face="Arial" size="2">For&nbsp;questions regarding this invoice, please contact <apex:outputLabel value="{!relatedTo.Account__r.Owner.FirstName} {!relatedTo.Account__r.Owner.LastName}"/> at <apex:outputLabel value="{!relatedTo.Account__r.Owner.Phone}" />.</font></td>





Message Edited by m_roark on 12-03-2008 08:25 AM

Thanks m_roak.

My problem was that I was referencing a separate visualforce page (tried to use apex:include). Unfortunately, I'm can't build the page directly into the attachment since the page I'm attaching calls a controllerextension.

I haven't seen a way to declare an extension of the relatedto. object or messaging:emailTemplate. I think my only option is to use the example from Chapter 13 to build a button/apex script that creates the PDF and sends the email.


Were you ever able to come up with a solution for getting the rich functionality (Contact lookup, Template selection, etc)?  I'm looking to override the standard Send Email interface for Cases (so our CSEs can update the case and send an email in one screen), but sacrificing the standard Send Email functionlity is not feasible.




No, I did not find this functionality.  I ended up using the methods descibed above, and prepopulated the necessary fields on the page load.

while  i  copied and paste i  am getting error this where should i add apex page tag attribute



error :


<apex:page> is required and must be the outermost tag in the markup at line 1 column 1