Send Email using aura components

This post shows how to send an email using aura component in salesforce lightning.

EmailParticipants.cmp

<aura:component description="EmailParticipants" implements="flexipage:availableForAllPageTypes" controller="EmailParticipantController">
    <ltng:require styles="{!join(',', $Resource.YA_Styles + '/LT_YA_Global_Css.css', $Resource.YA_Styles + '/LT_External_Typography.css', $Resource.Y_Styles + '/psStyles.css', $Resource.Y_Styles + '/popupCSS.css')}"/>    

    <aura:attribute name="Spinner" type="boolean" default="false"/>
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>

    <lightning:layoutItem size="12" smallDeviceSize="12" mediumDeviceSize="6" largeDeviceSize="12" padding="around-small">
        <lightning:card>
            
            <div class="slds-grid slds-wrap">
                <div class="slds-size_2-of-2">

                    <div class="slds-p-bottom_medium slds-p-left_medium slds-border_bottom">
                        <div class="slds-media">
                            <div class="slds-media__figure slds-p-right_medium">
                            <div class="slds-icon slds-page-header__icon ">
                                <lightning:icon iconName="action:email" size="small"/>
                            </div>
                            </div>
                            <div class="slds-media__body">
                                <div class="slds-p-top_small">
                                <h1>
                                    <span class="slds-page-header__title" title="Email Participants">Email Participants</span>
                                </h1>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="slds-grid slds-gutters slds-text-align_center slds-p-top--small">
                        <div class="slds-col">
                            <button class="slds-button slds-button_neutral" onclick="{!c.Send}">Send Email</button>                           
                        </div>
                    </div>

                    <div class="slds-p-around_medium">

                        <div class="slds-box">
                           
                        	<label class="slds-form-element__label" for="form-element-01">Email Address</label>
							<div class="slds-form-element__control">
								<input type="text" id="txtEmail" class="slds-input" />
							</div>
							<label class="slds-form-element__label" for="form-element-01">Subject</label>
							<div class="slds-form-element__control">
								<input type="text" id="txtSubject" class="slds-input" />
							</div>							
							<label class="slds-form-element__label" for="form-element-01">Message</label>
							<div class="slds-form-element__control">
								<lightning:inputRichText value="{!v.myMessage}" placeholder="Content for Participants"/>
                            </div>
                            <aura:if isTrue="{!v.showData}"> 
                            <label class="slds-form-element__label" for="form-element-01">Include Skills and Competency Report</label>
							<div class="slds-form-element__control">
								<ui:inputCheckbox aura:id="checkboxSkills" label="" change="{!c.showDatatable}"/>
                            </div>
                            
                            <label class="slds-form-element__label" for="form-element-01">Skills and Competency Table</label>
							<div class="slds-form-element__control">
								Nested Data Table!!
                            </div>
                            </aura:if>
                    
                        </div>
                     </div>

                </div>
            </div>
        </lightning:card>
    </lightning:layoutItem>

</aura:component>

EmailParticipantsController.js

({
	doInit : function(component, event, helper) {

    },
	Send : function(component, event, helper) {
        var email=helper._e('txtEmail').value;
        var Subject=helper._e('txtSubject').value;
        var Message=component.get("v.myMessage");        
        var regExpEmailformat = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
        
        if(email==''){
            alert('Email-Id is required');
        }
        else if(Subject==''){
            alert('Subject is required');
        }
        else if(Message==''){
         alert('Message is required');
        }
        else{
            if(!email.match(regExpEmailformat)){
                alert("Invalid Email Id");
            }
            else{
                helper.SendEmail(component);
            }
        }
    },
    
    showSpinner: function(component, event, helper) {        
        component.set("v.Spinner", true); 
    },
    
    hideSpinner : function(component,event,helper){        
        component.set("v.Spinner", false);
	},
	
	
 })

EmailParticipantsHelper.js

({
	SendEmail : function(component) {
		   var email=this._e('txtEmail').value;
		   var Subject=this._e('txtSubject').value;
		   var Message=component.get("v.myMessage");   
		   var action=component.get("c.processEmail");
		   action.setParams({
			   email:email,
			   Subject:Subject,
			   Message:Message
		   })
		   action.setCallback(this,function(e){
			   if(e.getState()=='SUCCESS'){
				   var result=e.getReturnValue();
				   if(result=='Success'){
					   alert('Email Send Successfully!');
				   }
				   else{
					   alert(result);
				   }
			   }
			   else{
				   alert(JSON.stringify(e.getError()));
			   }
		   });
		   $A.enqueueAction(action);
	},
	   
	   _e:function(ele){
		   return document.getElementById(ele);
	   },
	})

EmailParticipantController.cls

public with sharing class EmailParticipantController {

    
    @AuraEnabled
    public static string processEmail(String email, String Subject, String Message){
        String sMessage='';
        try{
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {email}; 
                mail.setToAddresses(toAddresses);
            mail.setSubject(Subject);
            mail.setHtmlBody(Message);
            Messaging.SendEmailResult [] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
            sMessage='Success';
        }
        catch(Exception ex){
            sMessage=ex.getLineNumber()+'\n'+ex.getCause()+'\n'+ex.getMessage()+'\n'+ex.getStackTraceString();
        }
        return sMessage;
    }


}

Permanent link to this article: https://salesforcebuddy.com/2020/05/send-email-using-aura-components/

Leave a Reply

Your email address will not be published.