-> A custom link on the contacts page invokes a VF page
-> This VF page simulates the Send an Email page while auto populating a predefined email template:
<apex:page standardController="contact" extensions="custom_button_controller" id="ThePage">
<apex:form id="theForm">
<apex:pageBlock id="theBlock" mode="inlineEdit">
<apex:outputLabel value="To "/>
<apex:outputField value="{!Contact.name}"/>
<br/>
<br/>
<apex:outputLabel value="Cc "/>
<apex:inputtext value="{!Cc}"/>
<br/>
<br/>
<apex:outputLabel value="Subject "/>
<apex:outputField value="{!emailTemp.subject}"/>
<br/>
<br/>
<p> Body </p>
<apex:inputTextarea value="{!emailTemp.HtmlValue}" richText="true" rows="25" cols="100"/>
<br/>
<br/>
<apex:commandButton action="{!sendEmail}" value="Send Email" id="theButton"/>
</apex:pageBlock>
</apex:form>
</apex:page>
-> The apex code:
public class custom_button_controller {
string contact_id{get;set;}
public EmailTemplate emailTemp{get;set;}
public string cc{get;set;}
contact c;
public custom_button_controller(ApexPages.StandardController controller) {
contact_id = apexpages.currentpage().getparameters().get('id');
c = [Select FirstName from contact where id=: contact_id];
emailTemp = [select Subject, Name, Id, HtmlValue From EmailTemplate where id='00X50000001XtXB'];
}
public pagereference sendEmail(){
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
//message.setTemplateId('00X50000001XtXB');
message.setTargetObjectId(contact_id);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for (Attachment a : [select Name, Body, BodyLength from Attachment])
{
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}
//message.setCcAddresses();
emailTemp.HtmlValue = emailTemp.HtmlValue.replace('{!Contact.FirstName}',c.FirstName);
message.setHtmlBody(emailTemp.HtmlValue);
message.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{message});
return null;
}
}
What is working:
-
Neat editable preview of the HTML body on the VF page (except for merge field, explained below)
-
Sending email successfully with the right attachments
-
Successful inline editing of the email body, which results in an updated body sent
What is NOT working:
-
Email recipient sees merge field {!Contact.FirstName}, instead of the merge field value. Rather than reading Dear Diary, it's Dear {!Contact.FirstName},. This merge field (not its value) is also seen in the text area preview that the user can edit before sending out the email. Update: Got this working by using replace() to replace merge field with contact.firstname. See code
-
Adding cc address to the email. Tried setCcAddress() in the code. Need help adding this cc address before sending out the email
-
Displaying attachments as well as giving the user the option to add and delete attachments all on the same single 'Send an Email' VF page
-
When the send button is clicked, the page (browser tab) is closed and the user views the concerned contact record
-
Having the To field on the VF page as a lookup field to show emails of contacts and users
Appreciate your time.
Best Answer
Update one more time : The code below can send email, add attachements, delete attachments on the fly and reload the same page to send more emails. Page:
Controller :
Page for adding new attachment
Page:
Controller: