Sending and Testing Emails in Apex

Sometimes you just need to send a simple confirmation email to let users know something worked. Usually with some kind of contact form or job application form for example. Users also REALLY like to know what they sent worked and was received, which is where sending a simple email message from Apex will work just fine.

First, lets create the sending class and method

public class EmailSend {

    public static void sendEmail(String candidate){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[]{candidate});
        mail.setReplyTo('hr_address@my_company.com');
        mail.setSubject('Thank You');

        // https://help.salesforce.com/articleView?id=000340122&type=1&mode=1
        // org-wide email address needs to be set
        OrgWideEmailAddress[] owea = [SELECT Id FROM OrgWideEmailAddress WHERE Address = 'hr_address@my_company.com'];
        if ( owea.size() > 0 ) {
            mail.setOrgWideEmailAddressId(owea.get(0).Id);
        }

        mail.setHtmlBody('<p>Your message has been recieved</p>');

        mail.setPlainTextBody('Your message has been recieved');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
    }

}

Gist Link

Great! Now all we need to do to call this is pass it an email address we’re sending to.

EmailSend.sendEmail('contact@kbcarte.com');

To use this, I set an Organization-Wide email address. This is usually a catchall HR email address. And only really used for the From address.

Now, how do we test it? Great question! We’ll use governor limits. By getting the email invocations from the Limits class, we can check to make sure one was sent.

@isTest
public class TestEmailSend {
    @isTest
    static void testTheEmail(){
        Test.startTest();
            // the method we're testing
            // https://gist.github.com/techb/7519e95bac3caa2b8adb3f65d2dc2dc8
            EmailSend.sendEmail('test@test.com');
        
            // we assert buy what governor limits say 
            Integer invocations = Limits.getEmailInvocations();
        Test.stopTest();
        System.assertEquals(1, invocations, 'Email has not been sent');
    }
}

Gist Link

That wasn’t so bad was it? Now the send email method has 100% coverage and can be deployed.

Get Permission Set’s User email addresses

Have a need to get all the email addresses of users who are assigned a Permission Set?

It was something I ran into when needing to send emails from a Trigger.

Your use case is more than likley different, but here is a method to get a List<String> of email addresses by Permission Set.

To use the method, just pass in the API name of your PermSet:

Setup > Users > Permission Sets > [your permission set] > API Name in the top right. Ex: Expense_Approver_Final

// Get list of email addresses by Permission Set
public static List<String> getEmailByPermSet(String permsetname){
    List<Id> just_ids = new List<Id>();
    List<PermissionSetAssignment> users = [
        SELECT AssigneeId 
        FROM PermissionSetAssignment 
        WHERE PermissionSet.Name = :permsetname
    ];
    for(PermissionSetAssignment uid: users){
        just_ids.add(uid.AssigneeId);
    }

    List<String> just_email = new List<String>();
    List<User> email_list = [SELECT Email FROM User WHERE User.Id IN :just_ids];
    for(User usr: email_list){
        just_email.add(usr.Email);
    }

    return just_email;
}

Github Link