> ## Documentation Index
> Fetch the complete documentation index at: https://developers.scoutworks.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Fix bounced email addresses

> A sheet of the addresses Scoutworks will not email, because mail bounced or the owner marked it as spam, so leaders can fix the contact in Scoutworks.

> [!NOTE]
>
> * **For:** section leaders and group admins
> * **You get:** a sheet of the addresses that bounced or marked an email as spam, refreshed each week
> * **Time:** about five minutes
> * **Scopes:** `communications:read` and `communications.people:read`

When an email to a parent bounces for good, or a parent marks one as spam, Scoutworks stops emailing that address. You do not need to do anything to stop the emails. But that family now misses every message, often without anyone noticing.

This recipe lists those addresses in a Google Sheet, refreshed each week. A leader can then ask the family for a new address and fix the contact in Scoutworks.

It uses Google Apps Script. It takes about five minutes.

## 1. Add the script

In your Google Sheet, choose **Extensions → Apps Script**. Delete what is there, paste the script below and choose **Save**.

```javascript Code.gs theme={null}
// Scoutworks → addresses that bounced. https://developers.scoutworks.app/recipes/clean-mailing-list

const BASE = 'https://api-demo.scoutworks.app/api/v1';

function blockedAddresses() {
  // The whole list each time: a bounce is lifted after 30 days without another.
  const blocked = readAll('/email-suppressions');

  const rows = [['Email', 'Why', 'Since', 'What to do']];
  blocked.forEach(function (s) {
    rows.push([
      safe(s.email),
      s.reason === 'bounce' ? 'Bounced' : 'Marked as spam or unsubscribed',
      s.since.slice(0, 10),
      s.reason === 'bounce'
        ? 'Ask the family for a new address, then update it in Scoutworks.'
        : 'Ask the family whether they still want emails. Do not just change the address.',
    ]);
  });

  const book = SpreadsheetApp.getActive();
  const sheet = book.getSheetByName('Not emailed') || book.insertSheet('Not emailed');
  sheet.clearContents();
  sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
  sheet.getRange(1, 1, 1, rows[0].length).setFontWeight('bold');
  sheet.setFrozenRows(1);
}

// Every item in a list, following nextCursor to the last page.
function readAll(path) {
  const items = [];
  let cursor = null;
  do {
    let url = BASE + path + (path.indexOf('?') === -1 ? '?' : '&') + 'limit=200';
    if (cursor) url += '&cursor=' + encodeURIComponent(cursor);
    const body = fetchScoutworks(url);
    items.push.apply(items, body.data);
    cursor = body.nextCursor;
  } while (cursor);
  return items;
}

// Calls the API. Waits and tries again if the rate limit is reached.
function fetchScoutworks(url) {
  const apiKey = PropertiesService.getScriptProperties().getProperty('SCOUTWORKS_API_KEY');
  if (!apiKey) throw new Error('Add SCOUTWORKS_API_KEY in Project Settings → Script Properties.');
  for (let attempt = 0; attempt < 3; attempt++) {
    const res = UrlFetchApp.fetch(url, {
      headers: { Authorization: 'Bearer ' + apiKey },
      muteHttpExceptions: true,
    });
    const body = JSON.parse(res.getContentText());
    if (res.getResponseCode() === 200) return body;
    if (res.getResponseCode() === 429) {
      Utilities.sleep((body.retryAfter || 30) * 1000);
      continue;
    }
    throw new Error(body.error.code + ': ' + body.error.message);
  }
  throw new Error('Scoutworks is still busy. Try again in a minute.');
}

// Stops an address that starts with = + - or @ from running as a formula.
function safe(value) {
  const text = String(value);
  return /^\s*[=+\-@]/.test(text) ? "'" + text : text;
}
```

## 2. Add your key

In the script editor, choose **Project Settings** (the cog), then **Script Properties → Add script property**. Property: `SCOUTWORKS_API_KEY`. Value: the [sandbox key](https://developers.scoutworks.app/guides/sandbox.md) to try it, or your own key. Choose **Save script properties**.

## 3. Run it, then refresh it every week

Choose **blockedAddresses** in the toolbar and press **Run**. Open the **Not emailed** tab.

To refresh it every Monday, choose **Triggers** (the clock), then **Add Trigger**. Function: `blockedAddresses`. Event source: **Time-driven**. Type: **Week timer**. Day: **Every Monday**. Choose **Save**.

## 4. Switch to your group

Change `api-demo.scoutworks.app` to `api.scoutworks.app`, and replace the sandbox key with your own. Your key needs `communications:read` and `communications.people:read`. The second scope is the one that opens this list, because every entry is a person's address.

> [!WARNING]
>
> Email addresses are personal data. Keep the sheet private to the leaders who fix contacts.

## What each reason means

| Why | What happened | What to do |
| - | - | - |
| **Bounced** | Mail to the address bounced for good: it does not exist, or the mailbox is closed. | Ask the family for a new address and update it in Scoutworks. Scoutworks tries the address again after 30 days without another bounce. |
| **Marked as spam or unsubscribed** | The owner marked a Scoutworks email as spam, or unsubscribed. | Talk to the family. They may have done it by mistake, or may want fewer emails. Scoutworks never lifts this block, so a changed address is the only way to email them again. Change it only if they ask. |

The list is for fixing records, not for sending. Scoutworks already leaves these addresses out of every email, so you never need to remove them from a send yourself.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.