Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
({
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.
},
})
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;
}
@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.