0

I'm trying to send an auto response without using Email-To-Case.

I can execute the bulk of the code below successfully using execute anonymous. However when I add it to the class below no email is received. The debug log says the email invocation count is 1. Email deliverability settings are set to All Email.

Why is the email is not being received? I have checked the spam etc in my inbox with no success.

global class AutoResponseTest implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
        Messaging.InboundEnvelope envelope) {

    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    // Get inbound email details and create temporary Contact for use with targetObjectId
    Contact contact = new Contact();
    contact.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
    contact.LastName = email.fromname.substring(email.fromname.indexOf(' '));
    contact.Email = envelope.fromAddress;
    insert contact;

    system.debug(contact);

    // Setup Email Message
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();  
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    List<String> sendTo = new List<String>();
    sendTo.add(contact.Email);
    mail.setToAddresses(sendTo);  
    mail.setReplyTo('[email protected]');
    mail.setSenderDisplayName('xxxxx HELP');  
      mail.settargetObjectId(contact.id);    
    Id emailTemplate = [select id from EmailTemplate where DeveloperName = 'xxxxx_AUTO_REPLY' LIMIT 1].Id;
      mail.setTemplateId(emailTemplate);
    mails.add(mail);
    Messaging.sendEmail(mails);

    delete contact; 

    return result;
  }
}
3
  • 1
    You should check the List<Messaging.SendEmailResult> returned from Messaging.SendEmail(). If there was an error on the salesforce side, you'll get the text and might be able to do something to fix it. Documentation Dec 29, 2017 at 14:27
  • 1
    When you ran code in execute anonymous, did you delete the contact also? What happens if you comment out the delete contact in your class? Dec 29, 2017 at 22:32
  • Thanks both for your input. I tried both of you comments but they did not give me any more clues. However, when I downloaded the email logs I found that there were errors. When I looked at the sender address I noticed that the system was trying to send the email using the system users email adress. This was a problem because I was using a sandbox, so the email address had been skewed. Once I changed it to my address, it went through fine.
    – Jay
    Jan 3, 2018 at 14:08

1 Answer 1

0

More info can be found by going to Setup > Email Log Files. Here I realised that since I was using a sandbox, I needed to replace the system users email address with a valid (non skewed) one.

You must log in to answer this question.

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