Does Messaging.SendEmail() not work in a Platform Event trigger context?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 “[email protected]” 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.

Method 2

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[]{'<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="563b2f133b373f3a17323224332525163b2f12393b373f387835393b">[email protected]</a>'};
    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('<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="126761776052767d7f737b7c3c717d7f">[email protected]</a>');
       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
   }  
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x