> For the complete documentation index, see [llms.txt](https://glyue.docs.sandboxbanking.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://glyue.docs.sandboxbanking.com/tutorials/building-an-email-integration/5.-sending-the-email.md).

# 5. Sending the Email

Now we’re ready for the final step. We’re going to take each of those story responses and collate them into an email body and then send it off to our recipient.

#### Service Request: `send_email` <a href="#step6-theemail-servicerequest-send_email" id="step6-theemail-servicerequest-send_email"></a>

Navigate to the **Build** page, go to the Service Requests for our integration, and add a new row. Enter the following values:

* **Sequence:** `200`
* **System:** `EMAIL_SMTP`
* **Service Name:** `n/a`
* **Formula Variable:** `send_email`
* **Before Prepare Request Hook:** copy and paste the code below.\
  This is the most complicated Python code we have shown to the reader thus far. Basically, we are creating a standard message for each story we received, and writing it to a file. We will later read this file for the body of the email.

  ```
  from datetime import datetime

  stories = [sr.response.payload for sr in get_story_by_id]
  stories.sort(key=lambda story: story.score, reverse=True)

  query_ct = 500 if not input.payload.limitQuery else input.payload.limitQuery
  email_limit = 50 if not input.payload.limitEmail else input.payload.limitEmail

  with open_glyuefile("email_body.txt", "w") as file:
      file.write(f"Here are the top {email_limit} of the latest {query_ct} articles from Hacker news:\n\n")
      for story in stories[:email_limit]:
          time = datetime.fromtimestamp(story.time).strftime("%m/%d/%Y, %H:%M")
          file.writelines([
              f"{story.title}\n",
              f"{story.url}\n",
              f"Posted by [{story.by}] on {time} ({story.score} points, {story.descendants} comments)\n",
              f"Discussion: https://news.ycombinator.com/item?id={story.id}\n\n"
          ])
  ```

#### Field Mappings for SR: `send_email` <a href="#step6-theemail-fieldmappingsforsr-send_email" id="step6-theemail-fieldmappingsforsr-send_email"></a>

Right-click the newly-created SR and go to Field Mappings. Add 4 rows with these values and then save.

* First row
  * **Field:** `from_address`
  * **Value:** `"email@address.net"` (use the same email as was entered for the Email SMTP Adapter Config, and don’t forget the quotes)
  * **Value Type:** `str`
* Second row
  * **Field:** `to_addresses`
  * **Value:** `[input.payload.email]`
  * **Value Type:** `list`
* Third row
  * **Field:** `subject`
  * **Value:** `"Top HackerNews Stories"`
  * **Value Type:** `str`
* Fourth row
  * **Field:** `body`
  * **Value:** `open_glyuefile("email_body.txt").read()`
  * **Value Type:** `str`

{% hint style="warning" %}
Don’t forget to save!
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://glyue.docs.sandboxbanking.com/tutorials/building-an-email-integration/5.-sending-the-email.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
