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
JohnDuraiJohnDurai 

clicking on button trigger email using Aura component

I have community page, inside that I have component with submit button once i click on submit button it should trigger email to the email id stored in Custom metadata/custom settings. can some one suggest how to do it in aura framework.

onclick="{!c.submitFormForEmail}">
                                                    Submit
                                                </button>
TobyKutlerTobyKutler
Sounds straightforward. You just need to call this Apex (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm) from your Aura component to send the email. Then query the metadata to retrieve the email template and set it to your body and also do email.setTreatBodiesAsTemplate(true); so you can send the template properly. 

your Aura comp will look something like this: 
let action = component.get('c.sendAnEmailMsg');
action.setParams({
"fromAddress": component.get('v.fromAddress'),
"toAddressesStr": component.get('v.toAddresses'),
"ccAddressesStr": component.get('v.ccAddresses'),
"bccAddressesStr": component.get('v.bccAddresses'),
"subject": component.get('v.subject'),
"whoId": component.get('v.whoId'),
"whatId": component.get('v.whatId'),
"body": component.get('v.emailBody'),
"senderDisplayName": component.get('v.senderName'),
"contentDocumentIds": docIds,
"attachmentIds": attIds,
"createActivity": setActivity,
"sendAsSingle" : component.get('v.sendAsSingle'),
"mergeBodies" : component.get("v.emailAndMergeBody"),
"extraAddresses" : component.get("v.extraAddresses")
});
    action.setCallback(this, function (response) {
                let state = response.getState();
                if (state === "SUCCESS") {
                    component.find('notifLib').showToast({
                        variant: 'success',
                        "title": "Success!",
                        "message": "E-mail has been sent successfully."
                    });
                    component.set('v.emailSent', true);

                } else if (state === "ERROR") {
                    helper.showErrorToast(component, response, false);
                    console.log('hit error')
                    component.set('v.showSpinner', false);
                    return;
                }
}

Apex: 
 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {'user@acme.com'}; 
String[] ccAddresses = new String[] {'smith@gmail.com'};
  

// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);

// Specify the address used when the recipients reply to the email. 
mail.setReplyTo('support@acme.com');

// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.
mail.setSubject('New Case Created : ' + case.Id);

// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);

// Optionally append the Salesforce email signature to the email.
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);

// Specify the text content of the email.
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created.<p>'+
     'To view your case <a href=https://MyDomainName.my.salesforce.com/'+case.Id+'>click here.</a>');

//Specify to treat as Email Template
email.setTreatBodiesAsTemplate(true);

// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });