I would like to create a custom button in the Contact detail page that when we click on it, it sends an email template capturing the information of that paricular contact.

How can I do this? Any code would be very much appreciate it!


  • Where are you sending it to? What data are you trying to capture? It might be as simple as a custom button, or you might need some code. The devil is in the details.
    – sfdcfox
    Jul 31, 2013 at 19:57
  • This is an example: an user is in the Contact detail page of John Williams. He wants to send an email to John Williams by just clicking on a custom button on the top of the detail page. The email is an email template with HTML tags to capture the information of John Williams such as Name, email, phone number, company, and so on.
    – Chicho
    Jul 31, 2013 at 20:13

2 Answers 2


Salesforce.com has specifically prevented us developers from having an easy time using the standard interface to send emails without user intervention. We can create a link to go to the Send Email page, but the user still has to click on Send to send the email. Here's the link:


Simply go to Setup | Customize | Contacts | Buttons, Links, and Actions, and create this as a new button or link of the "URL" type (you can use the same window). Change the Template ID to match the email template you'd like to auto-select.

If you want a one-click send feature, you'd need Visualforce.


Here's a sample Visualforce page:

<apex:page standardController="Contact" extensions="sendEmail" action="{!sendEmail}">
    <apex:pageMessages />

And associated controller:

public with sharing class sendEmail {

    ApexPages.StandardController controller;

    public sendEmail(ApexPages.StandardController controller) {
        this.controller = controller;

    public PageReference sendEmail() {
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message });
        return controller.view();


Be sure to use "GET CSRF protection" on the page to prevent spoofing. Set the button or link behavior to "display in existing window without header or sidebar". From the user's perspective, it is nearly instantaneous.

Finally, you could also use the AJAX Toolkit's sforce.connection.sendEmail, but this will use API calls unnecessarily (but also avoid the white page flicker of the Visualforce solution).

Updated: I added the actual logic to send an email.

Updated: Here's a JavaScript-only version. Note that this will use API calls in addition to Mass Email limits, while Visualforce will only use Mass Email limits.

(function() {
sforce.connection.sessionId = "{!$Api.Session_ID}";
var message = new sforce.SingleEmailMessage();
message.replyTo = "{!$User.Email}";
message.targetObjectId = "{!Contact.Id}";
message.templateId = "00X30000000h4jF";
var result = sforce.connection.sendEmail([message]);
  if(result[0].success) {
     alert("I sent the email as you requested, master.");
  } else {
     alert("I had a booboo.");
  • Thanks for the URL. However, by creating a Visualforce I think an user would still have a window poping up and he would have to click to send the email? I need to create a one-click send feature. The user should click on the button and the email should be sent without opening any window. Is this possible?
    – Chicho
    Jul 31, 2013 at 20:50
  • With Visualforce, you can have it operate in the same window. I'll update with an example.
    – sfdcfox
    Jul 31, 2013 at 20:53
  • See the additional code. This example is fully functional in my database and sends an email. Replace the template ID (00X...) with your own template.
    – sfdcfox
    Aug 1, 2013 at 20:30
  • 1
    That's what my previous code does. It sends the email. However, I'll also include a JavaScript version for you.
    – sfdcfox
    Aug 1, 2013 at 20:54
  • 1
    I don't know everything. That's where Google helps. But I have invented thousands of random solutions to small problems. Google thankfully indexes my posts for me, since I can only go back 100 pages on the forums.
    – sfdcfox
    Aug 2, 2013 at 2:09

A solution I've used is to (1) add a checkbox field to the account/contact that defaults to 'false', (2) add a button to the contact page that changes that checkbox's value to true, and (3) create a workflow rule that triggers when the checkbox equals 'true' and sends an email alert. You can also make the workflow rule doa field update to reset the checkbox back to false if you want your users to be able to send multiple emails.

You must log in to answer this question.

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