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
Ranjith MerguRanjith Mergu 

how to send email to current logged user when we click on the button on lightning page Using the "Aura Components"

Best Answer chosen by Ranjith Mergu
Prathyu bPrathyu b
Hi Ranjith,
Try the below code to send an email to logged in user
Create below components :
1. SendEmail.cmp  -- Lightning componnet
2. EmailSendController  -- apex controller
SendEmail.cmp 
   
<aura:component controller="EmailSendController">
    <aura:attribute name="mailId" type="String"/>
   <aura:attribute name="mailStatus" type="boolean" default="false"/>
   <aura:if isTrue="{!v.mailStatus}">
      <div role="alertdialog" tabindex="-1" aria-labelledby="prompt-heading-id" aria-describedby="prompt-message-wrapper" class="slds-modal slds-fade-in-open slds-modal--prompt">
         <div class="slds-modal__container">
            <div class="slds-modal__header slds-theme--error slds-theme--alert-texture">
               <h2 class="slds-text-heading--medium" id="prompt-heading-id">Mail Status</h2>
            </div>
            <div class="slds-modal__content slds-p-around--medium">
               <div>
                  <p>Email Sent successfully to {!v.mailId}</p>
               </div>
            </div>
         </div>
      </div>
      <div class="slds-backdrop slds-backdrop--open"></div>
   </aura:if>
   <div class="slds-m-around--medium">
      <div class="slds-container--medium">
          <lightning:button label="sendEmail" onclick="{! c.sendMail}"/>
      </div>
   </div>
</aura:component>

SendEmailController.js

({
    sendMail: function(component, event, helper) {
            helper.sendHelper(component);
    },
})

SendEmailHelper.js

({
    sendHelper: function(component) {
        var action = component.get("c.sendMailMethod");    
        action.setCallback(this, function(response) {
            var state = response.getState();
            alert('state&& '+state);
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
                alert('res '+storeResponse);
                component.set("v.mailStatus", true);
               // component.set("v.mail", UserInfo.getUserEmail());
            }
 
        });
        $A.enqueueAction(action);
    },
})


EmailSendController -- apex controller

public class EmailSendController {
 @AuraEnabled 
    public static void sendMailMethod( ){
    String mMail ;
        String mSubject = 'test mail from lightning';
        String mbody = 'Hi' +UserInfo.getFirstName();
        mMail = UserInfo.getUserEmail();
        System.debug('user mail id '+mMail);
     List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();     
  
     // Step 1: Create a new Email
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
    // Step 2: Set list of people who should get the email
       List<String> sendTo = new List<String>();
       sendTo.add(mMail);
       mail.setToAddresses(sendTo);
    
    // Step 3: Set who the email is sent from
       mail.setReplyTo('noreply@gmail.com'); // change it with your mail address.
       mail.setSenderDisplayName('salesforce User'); 
    
    // Step 4. Set email contents - you can use variables!
      mail.setSubject(mSubject);
      mail.setHtmlBody(mbody);
    
    // Step 5. Add your email to the master list
      mails.add(mail);
    
  // Step 6: Send all emails in the master list
     Messaging.sendEmail(mails);
   }   
}

Here i have used dummy data for subject and email body. Update the code as per your requirment and test it.
Let me know if you have any...

Thanks
 

All Answers

Prathyu bPrathyu b
Hi Ranjith,
Try the below code to send an email to logged in user
Create below components :
1. SendEmail.cmp  -- Lightning componnet
2. EmailSendController  -- apex controller
SendEmail.cmp 
   
<aura:component controller="EmailSendController">
    <aura:attribute name="mailId" type="String"/>
   <aura:attribute name="mailStatus" type="boolean" default="false"/>
   <aura:if isTrue="{!v.mailStatus}">
      <div role="alertdialog" tabindex="-1" aria-labelledby="prompt-heading-id" aria-describedby="prompt-message-wrapper" class="slds-modal slds-fade-in-open slds-modal--prompt">
         <div class="slds-modal__container">
            <div class="slds-modal__header slds-theme--error slds-theme--alert-texture">
               <h2 class="slds-text-heading--medium" id="prompt-heading-id">Mail Status</h2>
            </div>
            <div class="slds-modal__content slds-p-around--medium">
               <div>
                  <p>Email Sent successfully to {!v.mailId}</p>
               </div>
            </div>
         </div>
      </div>
      <div class="slds-backdrop slds-backdrop--open"></div>
   </aura:if>
   <div class="slds-m-around--medium">
      <div class="slds-container--medium">
          <lightning:button label="sendEmail" onclick="{! c.sendMail}"/>
      </div>
   </div>
</aura:component>

SendEmailController.js

({
    sendMail: function(component, event, helper) {
            helper.sendHelper(component);
    },
})

SendEmailHelper.js

({
    sendHelper: function(component) {
        var action = component.get("c.sendMailMethod");    
        action.setCallback(this, function(response) {
            var state = response.getState();
            alert('state&& '+state);
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
                alert('res '+storeResponse);
                component.set("v.mailStatus", true);
               // component.set("v.mail", UserInfo.getUserEmail());
            }
 
        });
        $A.enqueueAction(action);
    },
})


EmailSendController -- apex controller

public class EmailSendController {
 @AuraEnabled 
    public static void sendMailMethod( ){
    String mMail ;
        String mSubject = 'test mail from lightning';
        String mbody = 'Hi' +UserInfo.getFirstName();
        mMail = UserInfo.getUserEmail();
        System.debug('user mail id '+mMail);
     List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();     
  
     // Step 1: Create a new Email
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
    // Step 2: Set list of people who should get the email
       List<String> sendTo = new List<String>();
       sendTo.add(mMail);
       mail.setToAddresses(sendTo);
    
    // Step 3: Set who the email is sent from
       mail.setReplyTo('noreply@gmail.com'); // change it with your mail address.
       mail.setSenderDisplayName('salesforce User'); 
    
    // Step 4. Set email contents - you can use variables!
      mail.setSubject(mSubject);
      mail.setHtmlBody(mbody);
    
    // Step 5. Add your email to the master list
      mails.add(mail);
    
  // Step 6: Send all emails in the master list
     Messaging.sendEmail(mails);
   }   
}

Here i have used dummy data for subject and email body. Update the code as per your requirment and test it.
Let me know if you have any...

Thanks
 
This was selected as the best answer
Ranjith MerguRanjith Mergu
Thanks for your Reply,

Here i have a concern, how to attach a file to this component. like i have list of records in table i need to send those records to current logged in user

Thanks,
Ranjith