Skip to main content

Quick Tip : Stop emails from Sandbox to unwanted domains

Designing a robust automated system is challenging yet profoundly satisfying. I'm sure most can relate, it is bliss to watch the system automatically doing all the work, like clockwork (quite literally).

However, there can be some downsides as well. In our example, we wanted to ensure that in no way possible, an email is sent out to external recipients from a sandbox. Of course, the first step would be to mask the required data. However, there's still a possibility that real email addresses can make it into a sandbox.

So, we devised a solution to absolutely restrict such emails from a sandbox. The solution is actually quite simple.

This solution only targets email sent out manually by users in a sandbox. It does not address emails sent via apex, automation or third party tools

Step 1 - Identify the test domain

Identify a domain that you want to enable, to be able to send test emails from the sandbox. For example, if you are in XYZ Inc, you most likely want to send test emails only to the XYZ domain (

Step 2 - Identify Production Org Id

Get the organization Id of your production instance. To retrieve it, go to Setup> Settings > Company Settings > Company Information and make note of the organization id. (Note: This will be used to identify at run-time if the current instance is production or a sandbox)

Step 3 - Create REGEX code

Create a REGEX code to match the domain. Following are a few examples. 

(Note: This REGEX code is built to allow comma-separated email addresses, but may not be sufficient to handle all possible combinations of domain names. So, if you want to use a complicated domain name, you may need to tweak REGEX accordingly)

For the domain, REGEX will be:


For the domain, RGEX will be


Step 4 - Create a validation rule

Now, create a validation rule on the Email Message object, to throw an error, if the email address does not match the REGEX code. Before saving, you will need to:

  1. Replace Org id within sample validation rule, with your production instance's org id (as retrieved in STEP 2)
  2. Replace REGEX code within sample validation rule, with your domain specific REGEX (created in STEP 3)

$Organization.Id != '00D050000008ouS'
    !REGEX(ToAddress, "^[\\W]*([\\w+\\-.%]+@(mailinator\\.com){1,}[\\W]*,{1,}[\\W]*)*([\\w+\\-.%]+@(mailinator\\.com))[\\W]*$")
    !REGEX(CcAddress, "^[\\W]*([\\w+\\-.%]+@(mailinator\\.com){1,}[\\W]*,{1,}[\\W]*)*([\\w+\\-.%]+@(mailinator\\.com))[\\W]*$"))
    !REGEX(BccAddress, "^[\\W]*([\\w+\\-.%]+@(mailinator\\.com){1,}[\\W]*,{1,}[\\W]*)*([\\w+\\-.%]+@(mailinator\\.com))[\\W]*$"))

Final Solution

Learn how to restrict emails from sandbox to a specific domain
Final Validation Rule on Email Message object


This can be very effective to have absolute control over what domains can a sandbox send emails to, and avoid unnecessary hassles for you and your clients.


Popular posts from this blog

Quick Tips: Salesforce default Images

Well, I'm sure a lot of you still rely on using out of the box salesforce images for displaying quick icons within formula fields or even using them within your Visualforce pages. Lately, I realized that a lot of earlier resources are no longer accessible, so I tried to quickly extract all images from Salesforce CSS files and provide a quick reference here. Please note, I've referenced all images from SF servers directly, so if anything changes, the image should stop rendering here. As these images are completely controlled by Salesforce, and in case they change anything, it might lead to image not being accessible. Image path Image /img/samples/flag_green.gif /img/samples/flag_green.gif /img/samples/flag_red.gif /img/samples/color_red.gif /img/samples/color_yellow.gif /img/samples/color_green.gif /img/samples/light_green.gif /img/samples/light_yellow.gif /img/samples/light_red.gif /img/samples/stars_100.gif /img/samples/stars_200.gif /img/samples/stars_300.

Lightning: Generate PDF from Lightning components with in-memory data

I'm sure as everyone is diving into lightning components development, they are getting acquainted with the nuances of the Lightning components framework. As well as, its current limitations. Being a new framework, this is bound to happen. Although we have our users still using salesforce classic, we have started using lightning components framework our primary development platform and Visualforce is considered primarily for rendering lightning components within Classic Service console. Recently, while re-architecting a critical module, we encountered a problem wherein we needed to generate PDF from lightning components. Now, being Javascript intensive framework, it has limited room for such features (may be included in future roadmap). As of now, there is no native feature within the lightning framework to do so (at least I didn't find anything). Common Scenario - Create Visualforce page to retrieve data and generate PDF For scenarios where the data exist within Sa

Lightning: Generate PDF within Lightning Experience with Salesforce Data

Some time back I posted a solution to generate PDF from Lightning components using in-memory data. Post url: It was developed for a specific scenario, wherein we need to generate PDF where: User interface is Salesforce classic Initiated via Lightning Component Data doesn't exist within Salesforce and is completely in-memory As complex and tricky this situation was, we did end up finding a stable and equally tricky solution. However, I realize that there are still lack of solutions (or maybe my search skills are downgrading) to generate and automatically download PDF document from Lightning Experience, without using any lightning components, wherein data exists within Salesforce. You can use the earlier solution in that case, but it will be an overkill. There are various solutions available to generate PDF from javascript. But, I still think the plain old method of converting HTML to PDF (via