How to Include an Unsubscribe Link, Revised

2/15/20: Instructions and set up worksheet updated for Spring ’20.

1/9/20: Download the set-up worksheet to use with the instructions. 

12/16/19 Download the app now on the AppExchange! 

Allow Recipients to Unsubscribe from All Emails

Follow this tutorial to include an unsubscribe link in promotional emails sent from Salesforce. This post gives you some reasons for considering this feature.

When the recipient clicks to unsubscribe, a flow will look for all contacts and leads who have this as their preferred email address (if you’re in NPSP) or in the Email field. All contacts or leads who meet that requirement will be marked “Email Opt Out.”  The email address owner will receive one confirmation email immediately.

Please try this in your sandbox, developer org or Trailhead playground first! 

The difficulty level for this tutorial is medium because of all the steps. You should know how to modify a page layout and a profile, create or modify an email template, and use a sandbox. You will install a package that contains Flow and Process Builder, but you don’t need to know how to use them.

Create a site 

  • (Setup -> User Interface -> Sites and Domains -> Sites)
  • Create a new Site.
  • If you don’t have a Site domain, you’ll need to set one up. You may want to use the same domain as your “My” domain. Keep in mind: if I make my site domain “BaileyBrothers,” then the unsubscribe confirmation email will appear from “BaileyBrothers Site Guest User.” I will look into how to modify that label.
  • Site Name & Label: Unsubscribe
  • Site Contact: lookup your own user record here.
  • Active Site Home Page: find the visualforce page Unsubscribe.
  • A default web address does not need to be specified.
  • Save and Activate.
  • Copy and save the Site URL for use in a later step.

site url

  • You may need to click the Site label to go back into the Site again.

Public Access Settings

  • Click Public Access Settings.
  • Allow “Read” on the Contact object and on the Email field on Contacts.
  • Allow “Read” on the Lead object and on the Email field on Lead.
  • Allow Read, Create, Edit on custom object Unsubscribe. Give Edit access to all editable fields.
  • Give access to the visualforce page Unsubscribe.
  • If you are using a sandbox or Trailhead playground that has the Nonprofit Success Pack installed, activate this Workflow: Contact_Preferred_Email__c. If not on NPSP, ignore this step.

Sharing Settings

  • Enable the External Sharing model.
  • Internal Lead sharing must be set to Public, which will allow external lead sharing to be public.
  • Default External Access for Accounts needs to be Public (Read only or Read/Write) and Contacts need to be controlled by parent or Public (Read only or Read/Write).

Sharing Rules in Spring ’20

New in Spring ’20 security is locked down for Site Guest Users. On your Sharing Settings page, look for a checkbox “Secure guest user record access.” If that’s checked, you’ll need to add two sharing rules. Here’s a video.

Update variables in the flows

  • The variables in Flow called Unsubscribe Part 1.
    • OrganizationName – otherwise defaults to “our organization”
    • SenderAddress – otherwise defaults to This is the email address that the unsubscribe confirmation email comes from.
    • Follow the gif below to update “our organization” to the name of your actual organization. Look for SenderAddress in the Manager tab as well.


  • Click “Save As” and Close.
  • Activate this latest version of the Flow.

Required: One Time Set Up of Error Recipient Email Address(es)

This package contains a Custom Metadata Type to store the email addresses of recipients of the error handling message. You’ll need to create a record.

  1. Open Custom Metadata Types from Setup.
  2. Click Manage Records. Manage records to the left of Error Email Recipients
  3. Click New.
  4. Label and name the record “Error email list” or anything else you want. It doesn’t matter to this process.
  5. In Comma Separated Email List, type the list of anyone who should be alerted when a Flow fault occurs. Separate the emails with commas.Image of a custom metadata type record called Error Email Recipients. The record has one field Comma separated Email List that has a list of three emails.
  6. Click Save.

Set Up Email Templates

The package contains a folder of classic email templates called Unsubscribe.

Classic Email Templates

    • Unsubscribe Contact HTML Template
    • Unsubscribe Link VF Contacts Template
    • Unsubscribe Link VF Leads Template

Open the email templates and find the instructions for modifying the unsubscribe link to work for your site. The HTML template does not work for email addresses that include special characters like plus signs, so I’d recommend using the Visualforce (VF) email templates if you can.

Lightning Email Templates

Lightning email templates are not able to be included in packages at this time, so you’ll need to create your own.

  • Search for Email Templates or Lightning Email Templates in your AppLauncher.
  • Name it whatever you like.
  • In Related Entity Type choose Contact.
  • Click on the button Source and paste in this HTML
<a href="{{{Recipient.Id}}}&Email={{{Recipient.Email}}}">Click here to unsubscribe from all emails from our organization./a>
  • Modify the HTML of the link above by changing this part: to the Site URL you saved earlier.
  • Change “our organization” to the name of your organization.
  • Write the rest of the email, including a subject and save.

Test it out

  • In your sandbox, make sure Email Deliverability is turned on.
    • Go to setup. Search for “Deliverability.”
    • Set the access level to “All email.”
    • Be very careful if you are using a sandbox with real data – do you have any automated processes that are now going to start firing off emails like crazy? Remember to turn off email deliverability when you’re done testing.
  • Optional: Edit your Contact page layout.
    • Add the Email Opt Out field.
    • Add the Unsubscribe related list. You may want to add other fields here like Unsubscribe Email, Created Date, Status
  • Create a contact with your email address.
  • TEST IT!
    • Send an email to that contact (using the template you created) to test it. Make sure the contact has “Email Opt Out” unchecked.
    • When you click the link it should look like this.


  • The next page should look like this.


  • Look for that confirmation email.
  • Open the contact record and confirm the following:
    • Email opt out is now checked
  • There is a new related Unsubscribe record and Status = “Unsubscribed.”


Package Contains

  • Flows
    • Unsubscribe Part 1
    • Unsubscribe Contacts
    • UnsubscribeLeads
  • Process Builder
    • UnsubscribePB
  • Custom Object
    • Unsubscribe
      • Custom Fields
        • Contact__c
        • Lead__c
        • Unsubscribe_Email__c
        • Status__c
  • Visualforce Page
    • Unsubscribe
  • Email Template Folder “Unsubscribe”
      • Unsubscribe Contact HTML Template
      • Unsubscribe Link VF Contacts
      • Unsubscribe Link VF Leads

How It Works Technically

  1. Email recipient clicks unsubscribe link in email.
  2. This launches screen Flow Unsubscribe Part 1
    1. Which is embedded in Visualforce Page Unsubscribe
    2. Which is the main page of a Site (not included in package)
    3. Which is which is set to be visible to external users.
  1. If the Flow errors out, it looks up a Custom Metadata Type record Error Email Recipient and sends an email to the email addresses listed there (most likely your admin).
    1. If the flow runs successfully, it sends a confirmation email to the person who unsubscribed.
  1. The flow creates a new Unsubscribe record, a child record of the Contact. The external user has permission to create this kind of custom record but cannot create or modify standard objects.
  2. The record creation launches Process Builder UnsubscribePB.
    1. Why Process Builder instead of staying in the same Flow? Because by breaking it up to have a Process run in the middle, the External User is not technically modifying the contact or lead objects. External Users cannot modify standard objects. The Process Builder is checking the email opt out box on contact and lead.
  3. The process launches the Unsubscribe Contacts flow which finds all contact records with this email address as their preferred email and marks them Email Opt Out.
  4. The process also launches the Unsubscribe Leads If the whole process works successfully, the Unsubscribe record is marked as Status = “Unsubscribed”. If there is an error, Status = “Error” and an alert will be sent to people set as error recipients.

Revision notes

December 18, 2019

Here are instructions on how to remove the confirmation email from the Flow.

v.1.11 December 16, 2019

Now on AppExchange!

V.1.11 November 26, 2019

I added a missing step to the instructions and fixed some errors in the email templates.

V. 1.10 October 9, 2019

I have revised this one to submit for security review with the AppExchange. With security improvements to Sites in Winter ’20, I’ll need to add additional improvements very soon.

Now includes Leads as well as Contacts. Includes Custom Metadata instead of hardcoding emails in the Flow.

More resources


Leave a Reply