-1
<aura:component implements="force:lightningQuickAction" controller="sendEmailController">
    <aura:attribute name="email" type="string"/>
    <aura:attribute name="subject" type="string"/>
    <aura:attribute name="body" 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.email}</p>
                    </div>
                </div>
                <div class="slds-modal__footer slds-theme--default">
                    <button class="slds-button slds-button--brand" onclick="{!c.closeMessage}">Close</button>
                </div>
            </div>
        </div>
        <div class="slds-backdrop slds-backdrop--open"></div>
    </aura:if>


    <div class="slds-m-around--medium">
        <div class="slds-container--medium">
            <div class="slds-form--stacked">
                <div class="slds-form-element">
                    <label class="slds-form-element__label" for="CC">Email</label>
                    <div class="slds-form-element__control">
                        <ui:inputEmail class="slds-input" aura:id="email"  value="{!v.email}" required="true" />
                    </div>
                </div>
                <div class="slds-form-element">
                    <label class="slds-form-element__label" for="CC">Subject</label>
                    <div class="slds-form-element__control">
                        <ui:inputText class="slds-input" aura:id="subject"  value="{!v.subject}" />
                    </div>
                </div>
                <div class="slds-form-element">
                    <label class="slds-form-element__label" for="textareaSample2">Mail Body</label>
                    <div class="slds-form-element__control">
                        <lightning:inputRichText aura:id="body" value="{!v.body}" />
                    </div>
                </div>
                <div class="slds-form-element">    
                    <button class="slds-button slds-button--brand" onclick="{!c.sendMail}">Send</button>
                </div>
            </div>
        </div>
    </div>
</aura:component>

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

        var getEmail = component.get("v.email");
        var getSubject = component.get("v.subject");
        var getbody = component.get("v.body");

        if ($A.util.isEmpty(getEmail) || !getEmail.includes("@")) { // condition to check whether id is null and does not contain '@'
            alert('Please Enter valid Email Address'); // display alert message
        } else {
            helper.sendHelper(component, getEmail, getSubject, getbody); 
        }
    },

    closeMessage: function(component, event, helper) {//when closing the message, update every fields to null.
        component.set("v.mailStatus", false);
        component.set("v.email", null);
        component.set("v.subject", null);
        component.set("v.body", null);
    },

}) 

public class sendEmailController {
    @AuraEnabled 
    public static void sendMailMethod(String mMail ,String mSubject ,String mbody){

        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();

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

        List<String> sendTo = new List<String>();
        sendTo.add(mMail);
        mail.setToAddresses(sendTo);
        mail.setReplyTo('[email protected]'); 
        mail.setSenderDisplayName('Iswarya Sekar'); 
        mail.setSubject(mSubject);
        mail.setHtmlBody(mbody);
        mails.add(mail);
        Messaging.sendEmail(mails);
    }   
}

({
    sendHelper: function(component, getEmail, getSubject, getbody) {
        // call the server side controller method   
        var action = component.get("c.sendMailMethod"); //calls server side controller

        action.setParams({ //Used to set data to be passed to the server-side controller
            'mMail': getEmail,
            'mSubject': getSubject,
            'mbody': getbody
        });

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") { //if state is success, displays success message
                var storeResponse = response.getReturnValue();
                component.set("v.mailStatus", true);
            }

        });
        $A.enqueueAction(action); //adds server side actions to queue.
    },
})
2
  • so what is a question? Feb 13, 2018 at 12:58
  • In email field, the contact's email should be auto populated Feb 13, 2018 at 13:02

1 Answer 1

0

As this action is on contact record so first add the interface in your component

<aura:component controller="sendEmailController"
    implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

Now in your controller Init method Call the Apex controller and return the email using Contact Id

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

        // Prepare the action to load account record
        var action = component.get("c.getContactEmail");
        action.setParams({"contactId": component.get("v.recordId")});

        // Configure response handler
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state === "SUCCESS") {
                component.set("v.email", response.getReturnValue());
            } else {
                console.log('Problem getting account, response state: ' + state);
            }
        });
        $A.enqueueAction(action);
    },

In case you need Here's controller method

 @AuraEnabled
 public static String getContactEmail(Id contactId) {
      // Perform isAccessible() checks here
      return [SELECT Email FROM Contact WHERE Id = :contactId LIMIT 1].Email;
  }

Configure Components for Record-Specific Actions

Also you can use Lightning Data service and can load the record details without using Apex and can get the email from record.

5
  • but email field is not auto populating? Feb 13, 2018 at 13:12
  • @IshwaryaSekar Yes you need to populate it from code. Did you add my code and interface? Feb 13, 2018 at 13:15
  • Yes i added. but its not working and what is getContactEmail? Feb 13, 2018 at 13:18
  • @IshwaryaSekar you are missing the important part. You need one controller method which will return the current contact's email Id. Check my updated answer. Feb 13, 2018 at 13:29
  • @ Tushar Sharma.. Thank you so much. Its working fine for me !! Feb 14, 2018 at 4:33

Not the answer you're looking for? Browse other questions tagged .