1

I have created an input text box where user will input email id separated by comma. Upon clicking the send button then an email will be sent to the email ids entered in the text box.

I have created the text box and on clicking the send button the email is getting triggered to the recipient.

My issue is handling more than 1 email ids separated by the comma.How am I going to handle multiple email ids separated by comma

Please find below the code -

Component:

<aura:component controller="accountList" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
    <aura:attribute name="myText" type="string" default="" />
    <ui:inputText label="Enter some text" value="{!v.myText}" />
    <lightning:button variant="brand" label="Send Email" title="Send Email" onclick="{! c.sendEmail }" />
</aura:component>

Helper JS:

sendEmail: function(component, event, helper) {`

    var mailIdd = component.get("v.myText");
    alert("hello " + mailIdd);
    var action = component.get("c.sendMailMethod");
    action.setParams({
        'mbody': "randomNumberBetween0and19",
        'mrecipient': mailIdd
    });

    $A.enqueueAction(action);
}

Apex controller:

@AuraEnabled
public static void sendMailMethod(String mbody, String mrecipient) {

    List < Messaging.SingleEmailMessage > mails = new List < Messaging.SingleEmailMessage > ();
    System.debug('OTP ' + mbody);
    // 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(mrecipient);
    mail.setToAddresses(sendTo);
    // Step 3: Set who the email is sent from
    mail.setReplyTo('[email protected]'); // change it with your mail address.
    mail.setSenderDisplayName('salesforce User');
    // Step 4. Set email contents - you can use variables!
    mail.setSubject('OTP');
    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);
}

2 Answers 2

2

In Apex class, you can use Use mrecipient.split(','); which will return list<String> and you can future use to send email.

Below is the code snippet:

@AuraEnabled
public static void sendMailMethod(String mbody, String mrecipient) {

    List < Messaging.SingleEmailMessage > mails = new List < Messaging.SingleEmailMessage > ();
    System.debug('OTP ' + mbody);
    // 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 = mrecipient.split(',');
    mail.setToAddresses(sendTo);
    // Step 3: Set who the email is sent from
    mail.setReplyTo('[email protected]'); // change it with your mail address.
    mail.setSenderDisplayName('salesforce User');
    // Step 4. Set email contents - you can use variables!
    mail.setSubject('OTP');
    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);
}
6
  • Buddy it is throwing me exception error
    – Rivu Indra
    Sep 18, 2019 at 13:52
  • can you post complete error description from debug log Sep 18, 2019 at 14:11
  • can you check once again i have updated code. Sep 18, 2019 at 14:17
  • can you tell me the change..I am not able to get it ..will incorporate it...
    – Rivu Indra
    Sep 18, 2019 at 15:10
  • I have removed sendTo.add(mrecipient) from code Sep 18, 2019 at 15:48
0

You will need to parse the list of Ids while looking for the comma separator to create your list<string> where the string is an Id. I'd recommend using a RegEx function of some kind.

You must log in to answer this question.

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