8

I can't figure out why an email isn't being sent. I've got a Platform Event trigger which ultimately is supposed to send an email. Most of the code isn't directly relevant, but here's the surrounding code, called by the trigger handler (cut down for demonstration):

public static void sendEmails() {
    System.debug(Limits.getEmailInvocations());

    Messaging.SingleEmailMessage sem = new Messaging.SingleEmailMessage();
    sem.toAddresses = new List<String>{'myEmailAddress'};
    sem.subject = 'testsubject';
    sem.htmlBody = '<p>test Body</p>';
    Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{sem});

    System.debug(Limits.getEmailInvocations());
}

Debug:

20:22:26.0 (273270760)|USER_DEBUG|[47]|DEBUG|0

20:22:26.0 (346734155)|USER_DEBUG|[55]|DEBUG|1

Yet no email is received (not in Spam either). It's a sandbox, with Deliverability set to "All Email". No exceptions are thrown and I've tried my personal & work email addresses. When executing the Email send code above anonymously, it arrives in my inbox just fine.

EDIT: After further investigation, I've found the following in our server logs:

Our system has detected that this message is 550-5.7.1 not RFC 5322 compliant: 550-5.7.1 'From' header has non compliant domain name. 550-5.7.1 To reduce the amount of spam sent to Gmail, this message has been 550-5.7.1 blocked.

So, now my question is...given that this is an Automated Process user and thus cannot be set to use an Org-Wide Email Address (there's no profile to enable on the address), how can I resolve this?

5
  • 2
    Does the behavior change if you use an Organization-Wide Email Address? Platform Event triggers don't have a standard User context to be the "From".
    – David Reed
    Jun 16, 2018 at 17:18
  • @DavidReed I suspect we're getting warmer with this. Unfortunately, specifying an Org-Wide Email Address now throws: "INSUFFICIENT_ACCESS_OR_READONLY, Not profiled to access this Org-wide Email Address", but the running user is "Automated Process" and cannot be set on the Org-Wide Address.
    – Mike
    Jun 16, 2018 at 17:38
  • Removing the Org-wide address (so no specified sender), I have just checked the SendEmailResult and it's showing success. All debugging is showing a successful send, but nothing is received. So, I checked the Email Log and it's showing a ton of Mail Event = "P", e.g. "Permanent Failure". Is that a bounce?
    – Mike
    Jun 16, 2018 at 17:54
  • Okay, I dug into our server logs and found: "Our system has detected that this message is 550-5.7.1 not RFC 5322 compliant: 550-5.7.1 &#39;From&#39; header has non compliant domain name. 550-5.7.1 To reduce the amount of spam sent to Gmail, this message has been 550-5.7.1 blocked." Updating my original post with revised question.
    – Mike
    Jun 16, 2018 at 18:00
  • I am not an expert on email protocols, but poking around on other Stack Exchange sites suggests that message can mean that the mail server isn't sending a FROM header at all.
    – David Reed
    Jun 16, 2018 at 21:09

2 Answers 2

6

Limitation

Sadly, there is no apex email support from platform events. Per the documentation:

Sending an email message from a platform event trigger using the Messaging.SingleEmailMessage class is not supported. The email can’t be sent because the sender is the Automated Process entity, which has no email address.

Workaround

You can, however, send emails via email alert where the From Email Address is NOT "Current User's Email Address" but rather some other organization-wide email or default workflow user.

The reason, as outlined in this answer, "emails sent by the Automated Process user was "autoproc@YOURORGID" with no ".com" appended. This meant that Gmail would ignore it and the email wasn't being delivered."

In Apex, you might be able to use the SingleEmailMessage.setOrgWideEmailAddressId method to set the org-wide email and thereby the email be sent from a user other than the automated process user.

5
  • our experience is the emails are sent by rsandoval@YOURorgId where we suspect rsandoval is some internal SFDC name associated with our org when org was set up - we have never had rsandoval as a User in our org.
    – cropredy
    Jul 2, 2019 at 16:10
  • 1
    @cropredy thanks, in the sandbox I tested today the email was trying to be sent from "autoproc@ORGID". The prefix may vary. Seems consistent the suffix is the org id without a domain. Either way, sending from an org-wide email or contextually any user other than autoproc seems to work :)
    – Doug Ayers
    Jul 2, 2019 at 16:21
  • @DougAyers Is the suggestion then to do a callout to the Actions API since Apex cannot invoke an Email Alert directly? I just tried an Email Alert from an Event Process Builder and nothing sent, even if set up with an Org-Wide sender.
    – Mike
    Jul 2, 2019 at 18:49
  • @Mike You could do a callout to Actions API, but no, that's not my suggestion. I was successfully able to have my platform event trigger do DML on a record to cause Process Builder to fire and send an email alert. We did find out that Visualforce email templates weren't supported -- the autoproc user doesn't have access to them, so try a "custom" or other template type.
    – Doug Ayers
    Jul 2, 2019 at 22:32
  • Confirmed you can send emails from apex using setOrgWideEmailAddressId. I think you must enable all profiles to use the ORG wide in order for it to work tho
    – NSjonas
    Sep 1, 2020 at 20:49
-1

It appears to me that your issues are caused by not following the documentation in the SingleEmailMessage Methods where one basically "sets" the values of the attributes you have in your code. I'll add that the limits class is best used to reserve email capacity and to determine whether sufficient capacity exists to send your message. Your code should look more like what's below:

public static void sendEmails() {
    integer maxCount = Limits.getLimitEmailInvocations(); // emails available to send  
    String[] toAddresses = new String[]{'[email protected]'};
    integer count = 1; // number of single emails you intend to send
    // note that in your code, you did not have a valid email address
    if(maxCount - count > 0 ) {
       Messaging.reserveSingleEmailCapacity(count);
       // this reserves the capacity for your emails if available
       Messaging.SingleEmailMessage sem = new Messaging.SingleEmailMessage();
       sem.setToAddresses(toAddresses);
       // context user is autmatically set as the sender unless you specify otherwise
       sem.subject('testsubject');
       sem.setReplyTo('[email protected]');
       sem.setSenderDisplayName('MyName');
       sem.setBccSender(false);
       sem.setUseSignature(false);        
       sem.sethtmlBody('<p>test Body</p>');
       Messaging.sendEmail(new Messaging.SingleEmailMessage[]{sem});

       System.debug(count - Limits.getEmailInvocations());
       // the above should always be equal to zero which you can assert if you wish
   }  
}
1
  • 1
    As mentioned, the code works fine anonymously, but to confirm, I’ve also tried using the set() methods with the same outcome. This is also written here for demonstration, it’s not my actual method. The limits check is here only to demonstrate the system claiming to have invoked the sendEmail method, it’s not actually in my method. Finally, this is a platform event trigger in its own transaction, and therefore impossible to have hit the sendEmail limit at this point. In any case, none of these things explain why an email wouldn’t be delivered.
    – Mike
    Jun 16, 2018 at 16:05

You must log in to answer this question.

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