1

Requirement: Take input (Name, Phone) from user into LWC modal and on click of submit button fire email, using Apex Messaging class, if user is Community Guest User.

Current Issue: When logged in as administrator or any other profile, email is getting fired and receiver is able to receive the email. But in case the user is not logged in on the community site no error is generated and nor does the receiver receives the email.

Methods tried till now:

  1. Have assigned access level to "all mails" Setup --> Deliverability --> Access Level --> "All mails"
  2. Used "Without Sharing" in the apex class.
  3. Profile(Community Guest User) --> System Permissions --> Checkbox checked: Send Email, Mass Emailenter image description here

3 Answers 3

0

Messaging class not accessible by guest user profile. Once I was facing the same problem for customer community guest users. the alternative I did is, called auto launch flow from apex and send the email from flow.

0

You can set organization-Wide Addresses and use them when you want to send the email.

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

Example: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_single.htm#apex_Messaging_SingleEmailMessage_setOrgWideEmailAddressId

Please try as suggested, it should work

Site Guest User can not send email in force.com site in salesforce

-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
  • Same issue as in your other answer with this exact text. Making a guest user an org-wide address seems like a terrible idea, and there's no explanation here about how that would resolve the issue (if it resolves the issue at all).
    – Derek F
    Commented Jan 23, 2023 at 21:05
  • Hey @DerekF I have updated the answer. Hope this helps. Thanks. Commented Jan 24, 2023 at 20:46

You must log in to answer this question.

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