5

I have below apex code under without sharing apex class to send email

Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        semail.setToAddresses(new String[] {'[email protected]'});
        semail.setSubject('Message from ');
        semail.setPlainTextBody('emailMessage');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail}, true);

The above works fine from the executed anonymous window and I receive the email immediately. But when the same code is executed by a guest site user I don't receive any email.

in logs, I am getting isSuccess() to true from Messaging.SendEmailResult but still don't receive any email

the guest user profile has below permission assigned but still don't get any email.

enter image description here Could someone please help, what am I missing?

tried assigning list email permission but it throw below error

enter image description here

Deliverability settings set to all emails

enter image description here

6
  • why are you assigning the list email permission? Do they have the general send email permission or am I misunderstanding the context in which your code above runs? Jul 6, 2021 at 18:39
  • Could not find send email permission, that's why I tried with the above permission.
    – gs650x
    Jul 6, 2021 at 18:40
  • 1
    your screenshot shows you editing a permission set. Have you taken a look at the actual guest profile where send email would be? You can find this through the following help doc for experiences Jul 6, 2021 at 21:36
  • I am sorry, I am not able to find any permission related to email on profile.
    – gs650x
    Jul 7, 2021 at 3:31
  • I have already assigned send email permission to the profile but still no luck, I have updated my question again.
    – gs650x
    Jul 7, 2021 at 7:13

3 Answers 3

2

Please check your Email Deliverability, if its All Emails

Then go to your community -> WorkSpace->Administrative-> Email and verify the email under Emails section as shown in Screenshot.

enter image description here

1
0

one of the solution is check the guest user email id , it should be authenticated . Change admin email id for guest user then it will work

-2

Since we all know that the apex code runs in the current user's context.

So when you try to run the Send Email code from the anonymous window, then the code runs from the System Admin Profile. So here, you will receive the email from the email address assigned to that System Admin User. Since these users have a valid email address, so you are able to receive mail from that user's email address.

But, when the Send Email code triggers from the Guest User's context, then you will not be able to receive email because the guest users are automatically generated when creating a site/community in salesforce and these users have auto-generated invalid email addresses.

So, our goal is to provide a valid email address while sending emails from Guest User's context.

There are 2 approaches to do this.

Approach 1:- Change the Guest User's Email to a valid email address.

  1. Since you cannot find the guest user just by navigating Setup --> Users, so you need to go to the Developer Console --> Query Editor and run the query as follows:-

    Select Id, FirstName, LastName from User Where UserType = 'Guest'
    
  2. Copy the Id of your guest user and open it into your org.

  3. Then, Edit the user, and provide any valid email address you like for that guest user.

enter image description here

For more help, you can refer to the following link:-

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

The problem with this method is that it will work only for that particular guest user's context for which we have provided the valid email address.

Suppose, your org has a large amount of guest users then to make it work for each and every guest user's context, we have to set the valid email address for all the guest users which I believe is a time-consuming and inefficient process. for this, we will use our second approach.

Approach 2:- Using Org-Wide Email Address.

Instead of defining the Valid Email addresses for all the guest users, why don't we define the email address at one place and use for all the guest user right? For this, we will use Org-Wide Email Address

To learn more about the Org-Wide Email Address please refer to the link:- https://documentation.conga.com/clm/latest/setting-up-organization-wide-email-address-152570545.html

Follow these steps:-

  1. Go to Setup --> Organization-Wide Addresses and add any email address you like and verify it.

  2. In the apex code, add the following:-

     List<OrgWideEmailAddress> lstEmailAddress=[select Id from OrgWideEmailAddress WHERE Address=['your org wide email address']];
    
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     mail.setTemplateId(et.Id);
     mail.setOrgWideEmailAddressId(lstEmailAddress[0].Id);
     mail.setToAddresses(new List<String>{'receiever email address'});
     mail.setTargetObjectId(UserInfo.getUserId());
     mail.setWhatId(objRegistration.Id);
     mail.setSaveAsActivity(false);
     mail.setUseSignature(false);
     List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
     allmsg.add(mail); 
    
     Messaging.sendEmail(allmsg,false); 
    

Hope this helps. Thanks !!

2
  • 1
    Setting an org-wide address for a guest user seems ill-advised (even worse if there are multiple guest users that would need this treatment). Even if it weren't though, this answer doesn't go into sufficient detail to explain why this would fix the issue presented in the question.
    – Derek F
    Jan 23, 2023 at 21:03
  • Hey !! @DerekF I have updated the answer. Hope this helps. Thanks. !! Jan 24, 2023 at 20:43

You must log in to answer this question.

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