1

I have a feature that sends emails to final user after an action is completed.

It is done by calling the oncomplete function on a commandButton on a ApexPage.

The testClass for the feature works fine, the same code via Developer console, too.

But, when I try it by using the live feature in a ApexPage, the email never arrives and no exception is thrown.

yes, the "Access level" Deliverability is set to "All email".

The fragment is like so:

EmailTemplate template = templateList[0];
Messaging.SingleEmailMessage  mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{objContact.Email});
String htmlBody = template.HtmlValue;
String subject  = template.Subject;
htmlBody        = htmlBody.replaceAll('#CONTACT_NAME#', objContact.Name);
mail.setHtmlBody(htmlBody); 
mail.setReplyTo('[email protected]');
mail.setSenderDisplayName('MyDisplayName'); 
mail.setSubject(subject);
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {mail};
Messaging.SendEmailResult[] results     = Messaging.sendEmail(messages); 

What could be the issue here?

if(results[0].success)
{
  //always true, but no email comes
}  

UPDATE

When I access the page in a internal approach like so:

https://domain-from-dev-sandboxc.visualforce.com/apex/VisualPageName

it works.

It sounds like a permition...which one?

UPDATE 2

By looking at email logs, what did I find?

Unauthenticated email from force.com is not accepted due to domain's\r\n550-5.7.26 DMARC policy. Please contact the administrator of force.com domain\r\n550-5.7.26 if this was a legitimate mail. Please visit\r\n550-5.7.26 https://support.google.com/mail/answer/2451690 to learn about the\r\n550 5.7.26 DMARC initiative.

5
  • What is returned in SendEmailResult's?
    – kurunve
    Commented Nov 29, 2021 at 17:47
  • I have added the code snippet that returns true, but no email comes Commented Nov 29, 2021 at 18:25
  • 2
    Pull email logs and go from there
    – identigral
    Commented Nov 29, 2021 at 18:31
  • You'd need access to every field the template pulls.
    – Adrian Larson
    Commented Nov 29, 2021 at 19:44
  • Regarding the access to fields, I have provided it to every field that template pulls, but no email arrives. Is it supposed to be something related to the type of license of the page? It is set as Guest user license. Related to the log? I could not find anything wrong. The Werid is that the debug shows that the email is sent. Commented Nov 30, 2021 at 0:06

1 Answer 1

0

I've found the solution.

You should configure your salesforce sandbox to send email through Email Relay. I realized it after checking the emails logs. Over there, the email is not sent. Google, for example, rejected them. All of them.

Go to Setup -> Email Relays and add your own configuration. After, add Setup -> Email Domain Filter.

Important to check the headers of the emails to see that in fact, you have set up it right. After defining relays, the emails will not be sent by Salesforce Admin user anymore. They will be sent by the email provider that you set up.

More details can be found here.

https://www.youtube.com/watch?v=GVEe-zMrslw

https://help.salesforce.com/s/articleView?id=sf.emailadmin_email_relay_overview.htm&type=5

https://help.salesforce.com/s/articleView?id=sf.email_relay.htm&type=5

https://help.salesforce.com/s/articleView?id=sf.emailadmin_email_relay_considerations.htm&type=5

https://help.salesforce.com/s/articleView?id=000316016&type=1

https://help.salesforce.com/s/articleView?id=000340131&type=1

https://help.salesforce.com/s/articleView?id=000321501&type=1

You must log in to answer this question.

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