1

I'm writing a somewhat generic class for my organization. The goal is to allow to send email using apex with a one line call.

This is working nice but I would like to offer some logs when we have a success.

My code:

public static void sendEmail(List<WrapperGenericEmail> listEmailWrapper){

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

    for(WrapperGenericEmail wrpGE : listEmailWrapper){
        allmsg.add(createMail(wrpGE));
    }

    try {
        Messaging.SendEmailResult[] resultsMail = Messaging.sendEmail(allmsg,false);
        list<Log__c> listLogs = new List<Log__c>();
        for(Messaging.SendEmailResult res : resultsMail){
            if (res.success) { // I would like to log each success inside my custom object Log__c }

Basically I have a custom wrapper and i can make a list of it and give it to the method above. It creates the emails and send them.

I would like to know if there is any way I could identify the success emails? The Documentation gives a 'getError()' which i beleave I can use to identify my errors but I would like to identify my success too.

There is also an IsSuccess() method but I would like to identify the mail which is successful (Body send, template, or anything specific to the email sent)

It's not super important but it would be nice to have. Thanks

1 Answer 1

1

The documentation you linked to also says ther's a isSuccess() method which will let you know that an email was sent (not delivered) successfully.

isSuccess() Indicates whether the email was successfully submitted for delivery (true) or not (false). Even if isSuccess is true, it does not mean the intended recipients received the email, as there could have been a problem with the email address or it could have bounced or been blocked by a spam blocker.

The SendEmailResult array should be in the same order as the list of your wrapper class, so by looping using the index you could reference the wrapper:

    for(Integer i = 0; i < resultsMail.size(); i++){
         Messaging.SendEmailResult res = resultsMail[i];
    
        if (res.isSuccess()) {
            WrapperGenericEmail wrapper = listEmailWrapper[i];
           Log__c logEntry = new Log__c();
           logEntry.SomeField__c = wrapper.someProperty;
        }
    }
3
  • Hello, Thanks for the answer, the thing is that I would like to identify the email which is sent successfully. The isSuccess() method doesn't seems to give any information about the email that was sent. I edited my question. Nov 2, 2022 at 9:54
  • @JulienSouchi Have made an edit that will hopefully help!
    – Matt Lacey
    Nov 2, 2022 at 10:15
  • Oh, i did not think about that. I will give it a try. Thanks! Nov 2, 2022 at 10:20

You must log in to answer this question.

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