# How To: Add Email Recipients from Related Lists

Use `addToEmail` when a template should populate recipients automatically from queried records.

### Problem

You want to automatically add email addresses from a related list as document recipients.

### Solution

**Add Contacts as primary recipients:**

```
<!--{{!
<lineitemsSOQL>
<class>none</class>
<soql>SELECT Email FROM Contact WHERE AccountId = '{{!Account.Id}}' AND Email != null</soql>
<column addToEmail="To">Email</column>
</lineitemsSOQL>
}}-->
```

**Add Account Team members as CC:**

```
<!--{{!
<lineitemsSOQL>
<class>none</class>
<soql>SELECT User.Email FROM AccountTeamMember WHERE AccountId = '{{!Account.Id}}'</soql>
<column addToEmail="CC">User.Email</column>
</lineitemsSOQL>
}}-->
```

**Add Created By users as BCC:**

```
<!--{{!
<lineitemsSOQL>
<class>none</class>
<soql>SELECT CreatedBy.Email FROM Opportunity WHERE AccountId = '{{!Account.Id}}'</soql>
<column addToEmail="BCC">CreatedBy.Email</column>
</lineitemsSOQL>
}}-->
```

### Important Notes

* You must also add `addToEmail` as an Apex parameter to your S-Docs button URL.
* Only non-null email addresses are included.
* `<class>none</class>` prevents the email list from rendering in the document.
* This works with any object that has email fields.

### Complete Example

**Button URL parameter:**

```
&addToEmail=true
```

**Template code:**

```
<!--{{!
<lineitemsSOQL>
<class>none</class>
<soql>SELECT Email FROM Contact WHERE AccountId = '{{!Account.Id}}' AND Email != null AND MailingList__c = true</soql>
<column addToEmail="To">Email</column>
</lineitemsSOQL>
}}-->
```

### Tips

* Filter aggressively so only intended recipients are added.
* Combine with other SOQL filters for tighter control.
* Test with a small group first.
* Do not forget the button parameter.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.sdocs.com/sdocs/advanced-template-logic/template-attributes/template-attributes-how-to-guides/how-to-add-email-recipients-from-related-lists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
