[SalesForce] Platform Event subscriber not sending email to gmail domain

I am trying to send an email to the user who initiated the event in the case of an error during event processing. In the subscriber I have code similar to the following:

public void onAfterInsert(Map<Id, my_event__c> events) {
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    email.setToAddresses(new string[]{'a@b.com'});
    email.setSaveAsActivity(false);
    email.setSubject('testing in subscriber...');
    email.setPlainTextBody('testing');

    Messaging.SendEmailResult[] emailResults = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });


}

If the toAddress is a yahoo address, everything goes through just fine. If the email is a Gmail address (personal or corporate), it queues up to send just fine. However, it never gets delivered. I found this message in the Salesforce email logs:

"12/08/2017 19:50:47","37/C0-04567-71DEA2A5",P,"xxx@gmail.com","rsandoval=00df40000002czjeaq__8h38x9n2wlectxvw@nbughxf8745q.5b-vhtuam.cs52.bnc.sandbox.salesforce.com","173.194.205.27",1447,005f40000011pYR,"",0,"0.556447","reading confirmation",,,"550-5.7.1 [136.147.62.237 11] Our system has detected that this message is\r\n550-5.7.1 not RFC 5322 compliant:\r\n550-5.7.1 'From' header has non compliant domain name.\r\n550-5.7.1 To reduce the amount of spam sent to Gmail, this message has been\r\n550-5.7.1 blocked. Please visit\r\n550-5.7.1 https://support.google.com/mail/?p=RfcMessageNonCompliant\r\n550 5.7.1 and review RFC 5322 specifications for more information. u54si363262qte.264 – gsmtp"

I can send just fine with the exact same code to the same Gmail address in a normal trigger. In the log, it looks like this. Same exact sender as the failed message, but different remote host:

"12/12/2017 00:03:44","6D/72-04565-0EC1F2A5",R,"xxx@gmail.com","rsandoval=00df40000002czjeaq__s2xv7jhs06ggwtcp@rzt152b8j5n.5b-vhtuam.cs52.bnc.sandbox.salesforce.com","10.247.217.116",1263,005f40000011pYR,"",,,,,,

What would be different in the domain name when running as the Automated Process user? Is there a way around this so email can be sent to any domain?

Best Answer

We had a similar issue pertaining to email. What we found was that emails generated from Workflow were not reaching email addresses (specifically on the Gmail domain). Using the Email Log Files we determined that the sending user for emails sent by the Automated Process user was "rsandoval@ourorgid" with no ".com" appended. This meant that Gmail would ignore it and the email wasn't being delivered.

For this scenario the answer was to update the Email Alert's setting for "From Email Address" to use "Default Workflow User's email address" instead of "Current User's Email Address".

What is particularly interesting is that in each Org that uses the Automated Process user, the associated email is "Rsandoval@ourorgId". I'm guessing that "RSandoval" was the engineer that created the Automated Process global user when it was first created? Weird.

Related Topic