# Method: generateEmail

#### Why?

Use `generateEmail` when you need to leverage the advanced styling, conditional logic, and related-list capabilities of S-Docs within an automated email workflow. This method allows you to bypass the limitations of standard Salesforce Email Templates while maintaining full control over the distribution via Apex.

#### What: Technical Signatures

The SDK provides three overloaded versions of this method depending on your required output.

**1. Generate HTML Body Only**

Best for when you only need the compiled content to pop into a custom UI or a standard Apex mailer.

Java

```
public static String generateEmail(Id templateId, Id recordId)
```

* Returns: `String` (The compiled HTML body).

**2. Generate Serialized Email Object (with Routing)**

Best for when you want S-Docs to handle the "To," "CC," and "Subject" logic defined in the template.

Java

```
public static String generateEmail(Id templateId, Id recordId, SDOC.EmailOptions options)
```

* Returns: `String` (A JSON-serialized `Messaging.SingleEmailMessage` object).

**3. Generate with Existing Attachments**

Best for sending an email that includes previously generated S-Doc files as attachments.

Java

```
public static String generateEmail(Id templateId, Id recordId, List<Id> sdocIdsToAttach, Boolean enforceSharingRules)
```

* Returns: `String` (A JSON-serialized `Messaging.SingleEmailMessage` object).

***

#### Solve: Implementation Patterns

**Scenario A: Simple HTML Injection**

Use this to grab the S-Docs content and send it using standard Salesforce Apex.

Java

```
String htmlBody = SDOC.DocumentSDK.generateEmail(emailTemplateId, oppId);

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(userContactId);
mail.setHtmlBody(htmlBody);
mail.setSubject('Your Custom Quote');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
```

**Scenario B: Advanced Routing with EmailOptions**

Use this to define CC addresses and specific S-Doc attachments programmatically.

Java

```
SDOC.EmailOptions options = new SDOC.EmailOptions();
options.toAddresses = new List<String>{'client@example.com'};
options.ccAddresses = new List<String>{'rep@company.com'};
options.sdocIdsToAttach = new List<Id>{'a00xxxxxxxxxxxx'}; // ID of an existing S-Doc record

String serializedMail = SDOC.DocumentSDK.generateEmail(templateId, recordId, options);

// Note: To send this, you must deserialize it back to a SingleEmailMessage object
```

***

#### Supporting Class: SDOC.EmailOptions

| **Property**      | **Type**       | **Description**                                           |
| ----------------- | -------------- | --------------------------------------------------------- |
| `toAddresses`     | `List<String>` | List of primary recipients.                               |
| `ccAddresses`     | `List<String>` | List of CC recipients.                                    |
| `emailSubject`    | `String`       | Overrides the subject line defined in the template.       |
| `sdocIdsToAttach` | `List<Id>`     | IDs of `SDOC__SDoc__c` records to include as attachments. |
| `replyToEmail`    | `String`       | Sets the "Reply-To" header.                               |

***

#### ⚠️ Constraints & Limitations

* Trigger Context: Direct invocation within a Trigger is prohibited. Use a Queueable or `@future` method.
* Email Limits: Sending emails via the SDK counts toward your daily Salesforce Org email limits.
* Attachment Logic: When attaching S-Docs, ensure the running user has "Read" access to the `SDOC__SDoc__c` records and their related `ContentVersion` files.
* Sharing Rules: If `enforceSharingRules` is set to `true`, the SDK will respect Salesforce private sharing models; if the user cannot see the record, the email generation will fail.


---

# 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/developer-hub/document-generation-workflows/apex-and-programmatic-automation/software-development-kit/document-generation/method-generateemail.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.
