To enable users to send an email from anywhere in Lightning Experience, you can create a global quick action. Alternatively, you can create an object-specific email action. After you create a global or object-specific email action, you can add predefined values and add the action to page layouts.
If the global quick action and object-specific action don’t meet your requirements, consider using an LWC quick action. For example, use an LWC quick action if you want to:
Display prequisite content before displaying the email composer
Set predefined field values for an email based on user input
The LWC quick email action isn’t supported in Experience Builder sites.
In your component’s HTML file, import the navigation services from the lightning/navigation module and the page reference utilities from lightning/pageReferenceUtils.
Here’s a basic syntax example that demonstrates how to display the email composer from an LWC quick action.
1import{LightningElement}from "lwc";2import{NavigationMixin}from "lightning/navigation";3import{encodeDefaultFieldValues}from "lightning/pageReferenceUtils";45export default class EmailQuickActionExample extends NavigationMixin(LightningElement){6 handleClick(){7 var pageRef = {8 type: "standard__quickAction",9 attributes:{10 apiName: "Global.SendEmail",11},12 state:{13 recordId: "00QB000000BLjUrMAL",14 defaultFieldValues: encodeDefaultFieldValues({15 HtmlBody: "Pre-populated text for the email body.",16 Subject: "Pre-populated Subject of the Email",17 To: "target@example.com",18}),19},20};2122 this[NavigationMixin.Navigate](pageRef);23}24}
In this example, the handleClick function launches the email composer with pre-populated content on the Subject and Body text.
To include default text in the email composer, use the encodeDefaultFieldValues function. To populate the email with input from your component, pass the field values into the encodeDefaultFieldValues function.
Default Email Field Values
By default, Salesforce prepopulates the To field with a contact or lead email address when you open the email action from the contact or lead record home pages. Ensure that the fields you specify in the encodeDefaultFieldValues function aren’t Read-Only in the Send Email global action’s layout. If the HTML Body and Subject fields are Read-Only, the email draft doesn’t include pre-populated text for those fields.
These fields are supported:
ValidatedFromAddress
ToAddress
CcAddress
BccAddress
Subject
HTMLBody
RelatedToId
The ValidatedFromAddress field accepts an organization-wide email address or the running user’s verified email address. The Subject and HTMLBody fields have no default value.
You can set predefined field values for an email based on user input. This example adds the test@example.com recipient to the To field.
To use this LWC quick action example, add it to the contact record page. The component retrieves the associated account ID on the contact that’s being viewed using @api recordId, which makes the component aware of its record context. To retrieve the associated account ID, use the getRecord wire adapter.
If a contact record includes an email field value, the ToAddress field displays both test@example.com and the contact’s email. The RelatedToId field preselects the associated account on the contact if it’s available.
Example: Set the Email Recipient on User Input on an Account
Using an LWC quick action, you can display a helpful message before displaying the email composer. To improve usability, you can also get user input to help determine the field values on your email composer.
To use this LWC quick action example, add it to the account record page. The component displays a message with the account record ID using @api recordId, which makes the component aware of its record context. It uses the lightning-record-picker base component to let users search for a contact associated on the account they’re viewing. When a user selects a contact, the getRecord wire adapter retrieves the email for the selected contact and displays the email composer.
sendEmail.html
1<template>2 <div class="slds-m-around_medium">3 <p>You are viewing record {recordId}. Please choose an email recipient.</p>4 <lightning-record-picker5 object-api-name="Contact"6 label="Contacts"7 filter={filter}8 display-info={displayInfo}9 onchange={handleEmailChange}10 >11 </lightning-record-picker>12 </div>13</template>
The lightning-record-picker base component uses the filter attribute to filter the searchable contacts to only those contacts that are associated to the account.
In your component’s JavaScript, add an event handler using the standard__quickAction page reference type. Pass in the default field values using the state object.
1import{LightningElement, api, wire}from "lwc";2import{NavigationMixin}from "lightning/navigation";3import{encodeDefaultFieldValues}from "lightning/pageReferenceUtils";4import{getRecord}from "lightning/uiRecordApi";5import EMAIL_FIELD from "@salesforce/schema/Contact.Email";67const fields = [EMAIL_FIELD];89export default class SendEmail extends NavigationMixin(LightningElement){10 @api recordId;11 @api contactId;12 contact;13 email;14 errors;1516 // Limit the filter to contacts with17 // the AccountId that match the account's recordId18 get filter(){19 return{20 criteria:[21{22 fieldPath: "AccountId",23 operator: "eq",24 value: this.recordId,25},26],27};28}2930 // Set the labels on the record picker results31 displayInfo = {32 primaryField: "Name",33 additionalFields:["Title", "Email"],34};3536 // Set the user input on contactId,37 // which is used to retrieve the contact record using getRecord38 handleEmailChange(e){39 this.contactId = e.detail.recordId;40}4142 // Retrieve the email information for the selected contact43 @wire(getRecord, {recordId: "$contactId", fields})44 wiredContact({error, data}){45 if(error){46 console.log("getRecord error: ", error);47}else if(data){48 this.contact = data;49 this.email = this.contact.fields.Email.value;50 this.setEmail();51}52}5354 // Display the email composer with predefined values,55 // including the retrieved email from the selected contact56 setEmail(){57 var pageRef = {58 type: "standard__quickAction",59 attributes:{60 apiName: "Global.SendEmail",61},62 state:{63 recordId: this.recordId,64 defaultFieldValues: encodeDefaultFieldValues({65 Subject: "Pre-populated Subject of the Email",66 ToAddress: this.email,67 HtmlBody: "Pre-populated text for the email body."68}),69},70};71 this[NavigationMixin.Navigate](pageRef);72}73}
To make this component available as a quick action, update the .js-meta.xml configuration file.
Alternatively, you can create the email as a headless action, which opens a modal with the email composer directly. Opening a modal that displays an email composer is similar to the global Send Email action.