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
Eric TeseurEric Teseur 

Lightning Component does't send email

I have a custom object with several fields. One of them have Lookup on Contact. And in my component I try to send email with the rich-text from record to email address linking with Contact, but nothing happen. After clicking on the component, I get a modal box, then I just want to click on the Email button without entering the any address (the address is Contact.Email related to this record) My rich-text edit should be the body of the letter, but the data from my record should be stored in it. Any solutions?

Thank you

 

 

Component:

<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global">

   <aura:attribute name="record" type="Rotation__c" default="{ 'sobjectType' : 'Rotation__c' }"/>
    
    <aura:attribute name="recordId" type="Id" />
   
  <aura:attribute name="isOpen" type="Boolean" default="false"/>
  
 <aura:attribute name="myVal" type="String" />
    
    <aura:attribute name="email" type="string"/>
   <aura:attribute name="subject" type="string"/>
   <aura:attribute name="bodytext" type="string"/>

    
    <!--!  <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> -->
  <!--Use "slds-m-around- -xx-large" class to add standard Large padding to the component--> 
  <div class="slds-m-around--xx-large">
    <button class="slds-button slds-button--brand" onclick="{!c.openModel}">Rotation details</button>  
  
  <!--Use aura:if tag to display Model Box, on the bese of conditions. [isOpen boolean attribute] -->   
    <aura:if isTrue="{!v.isOpen}">
      
   <!--###### MODAL BOX Start From Here ######--> 
      <div role="dialog" tabindex="-1" aria-labelledby="header99" class="slds-modal slds-fade-in-open ">
        <div class="slds-modal__container">
          <!-- ###### MODAL BOX HEADER Part Start From Here ######-->
          <div class="slds-modal__header">
            <button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close" onclick="{!c.closeModel}">
            X
                
                
            <span class="slds-assistive-text">Close</span>
            </button>
              <lightning:recordEditForm
                     aura:id="editForm"
                     recordId="{!v.recordId}"
                     recordTypeId="{!v.recordTypeId}"
                     onsuccess="{!c.handleSuccess}"
                                    
                     objectApiName="Rotation__c">
                     <lightning:messages />
                  <div class="slds-form-element__control">
               <h2 id="header99" class="slds-text-heading--medium">Rotation Edit</h2>
                  <lightning:inputField fieldName="Name" value="{!v.record.Name}"/>
               </div>
                <div class="slds-form-element__control">
                  <lightning:inputField fieldName="Contact__c" value="{!v.record.Contact__c}"/>
               </div>
                 
                  
                  <div class="slds-form-element__control">
                  <lightning:inputField fieldName="Amount__c" value="{!v.record.Amount__c}"/>
               </div>
                 
                 
               </div>
                  
                   <div class="slds-form-element__control">
                  <ui:inputEmail class="slds-input" aura:id="email"  value="{!v.email}" required="true" placeholder="enter@email.com"/>
                    </div>
                  <div class="slds-form-element__control">
                        <lightning:inputField fieldName="Payment__c" value="{!v.record.Payment_Method__c}"/>
                        <lightning:inputRichText value="{!v.record.Payment__c}"/>
               </div>
         
                    </lightning:recordEditForm>
           
          </div>     
          <!--###### MODAL BOX FOOTER Part Start From Here ######-->
          <div class="slds-modal__footer">
            <button class="slds-button slds-button--neutral" onclick="{!c.closeModel}" >Cancel</button>
             <lightning:button variant="brand" label="Save" type="submit" onclick = "{!c.handleSubmit}"/>
             <lightning:button variant="brand" label="Email" type="submit" onclick = "{!c.sendMailMethod}"/>
              
         
          </div>
        </div>
      </div>
      <div class="slds-backdrop slds-backdrop--open"></div>
      <!--###### MODAL BOX Part END Here ######-->
    
 </aura:if>
  </div>
</aura:component>

Controller:

({
   openModel: function(component, event, helper) {
      // for Display Model,set the "isOpen" attribute to "true"
      component.set("v.isOpen", true);
   },
 
   closeModel: function(component, event, helper) {
      // for Hide/Close Model,set the "isOpen" attribute to "Fasle"  
      component.set("v.isOpen", false);
   },
 
    handleSubmit:function(component,event, helper){
      event.preventDefault();
      var eventFields = event.getParam("fields");
      component.find('editForm').submit(eventFields);
      //var result = component.find("editForm").submit();
       component.set("v.isOpen", false);
  },
  
   handleSuccess :function(component, helper) {
      var homeEvt = $A.get("e.force:navigateToSObject");
      homeEvt.setParams({
          "recordId": component.get("v.recordId")
      });
      homeEvt.fire();     
  },
    
 
     
    sendMailMethod: function(component, event, helper) {
        // when user click on Send button 
        // First we get all 3 fields values 	
        var getEmail = component.get("v.email");
        var getSubject = component.get("v.subject");
        var getbody = component.get("v.bodytext");
        var getrecordId = component.get("v.recordId");
    
        
        // check if Email field is Empty or not contains @ so display a alert message 
        // otherwise call call and pass the fields value to helper method    
        if ($A.util.isEmpty(getEmail) || !getEmail.includes("@")) {
            alert('Please Enter valid Email Address');
        } else {
            helper.sendHelper(component, event, helper,getEmail, getSubject, getbody, getrecordId);
        }
    },
 
    // when user click on the close buttton on message popup ,
    closeMessage: function(component, event, helper) {
        component.set("v.mailStatus", false);
        component.set("v.email", null);
        component.set("v.subject", null);
        component.set("v.bodytext", null);

}, 
   
})

Helper:

({
 sendHelper: function(component,event,helper, getEmail, getSubject, getbody, getrecordId) {
        console.log('helperrecid'+getrecordId);
        // call the server side controller method 	
        var action = component.get("c.sendMailMethod");
        // set the 3 params to sendMailMethod method 
        action.setParams({
            'mMail': getEmail,
            'mSubject': getSubject,
            'mbody': getbody,
            'recordid':getrecordId
        });
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
                var applicationEvent = $A.get("e.c:EventCloseModelPopup");
                applicationEvent.setParams({"message" : 'success'})
                applicationEvent.fire();
                console.log('cmpEvent '+applicationEvent);
                console.log('applicationEvent event fired');                
            }
            
        });
        $A.enqueueAction(action);
}
})

Apex Method:

public static void sendMailMethod(String mMail ,String mSubject ,String mbody,string recordId){

        //Inserting the record id in Email subject
        string subrec= '('+ recordId +')';  
        // For storing Relation Id
        string opcid ='';
        //Email Sending Start
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
        //Create a new Email
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();        
        //Set list of people who should get the email        
        List<String> sendTo = mMail.split(',');
        //Setting Senders
        mail.setToAddresses(sendTo);        
        String addrUrl='e62ubs9wkx9ma0e6akh8qbbwtli5o6hpjy.7f-4wpbcua2.ap5.apex.salesforce.com';

        // Set who the email is sent from
        mail.setReplyTo(addrUrl); // change it with your mail address.
        mail.setSenderDisplayName(UserInfo.getName() ); 
        system.debug('>>>>>=='+ UserInfo.getName() );   
        // Subject from Email Component and Record ID
        mail.setSubject(mSubject + subrec );
        //Setting Body from Email Component
        mail.setHtmlBody(mbody);        
        // Add your email to the master list
        mails.add(mail);       
         //Add Inbound Email Message for contact
            EmailMessage caseEmailMessage = new EmailMessage();
            caseEmailMessage.fromAddress= UserInfo.getUserEmail();
            caseEmailMessage.ToAddress = mMail;
            caseEmailMessage.FromName = UserInfo.getName();
            caseEmailMessage.Subject = mSubject;
            caseEmailMessage.HtmlBody = mbody;
            caseEmailMessage.Incoming= False;
            caseEmailMessage.TextBody = mbody;
            caseEmailMessage.status = '3';

            if(recordId.startsWith('003')){
                caseEmailMessage.Contact__c = recordId;
            }

            insert caseEmailMessage;  


             // Add Email Message Relation for id of the sender
            EmailMessageRelation emr = new EmailMessageRelation();
            emr.EmailMessageId = caseEmailMessage.id;
            emr.RelationAddress = UserInfo.getUserEmail();
            emr.RelationType = 'FromAddress';

            //Contact Email Message Relation Id
            if(recordId.startsWith('003')){
                 emr.RelationId = recordId;
            }


          //Reservation Email Message Relation Id
            else if(recordId.startsWith('a1B')){
                 emr.RelationId = recordId;
            }

                        insert emr;     


            // Send all emails in the master list
            Messaging.sendEmail(mails);



}
 

 

Alain CabonAlain Cabon
Hello,

 <lightning:inputRichText value="{!v.record.Payment__c}"/>

 var getbody = component.get("v.bodytext");

Where is the link between Payment__c and bodytext ?
Eric TeseurEric Teseur
I'm sorry maybe I missed. But how I can combine the body of the Email and the Rich-Edit for field in one field?
Alain CabonAlain Cabon

 <lightning:inputRichText value="{!v.bodytext}"/>
 var getbody = component.get("v.bodytext");

<lightning:inputRichText value="{!v.record.Payment__c}"/>
 var getbody = component.get("v.record.Payment__c");

Your posted code is not complete that is why nobody helped you (the missing parts could be correct or incorrect, we don't know).