# Send messages with Ruby

Source: https://developer.nylas.com/docs/v3/sdks/ruby/send-email/

This page explains how to use the Nylas Ruby SDK and Email API to send messages.

## Before you begin

Before you start, you must have done the following tasks:

- [Installed and set up the Nylas Ruby SDK](/docs/v3/sdks/ruby/).
- Authenticated one or more users.

## Create and send an email draft

This section walks through how to create and send an email draft with the Nylas Ruby SDK.

The following example creates a Draft object and assigns it a subject and some body text.

```ruby
request_body = {
  subject: 'From Nylas',
  body: 'This email was sent using the Nylas Email API. Visit https://nylas.com for details.',
  to: [{ name: 'My Nylas Friend', email: 'swag@example.com'}]
}

draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)
```

You can also add file attachments, message tracking features, and reply-to values. For more information about the data you can add to a draft, see the [Drafts references](/docs/reference/api/drafts/).

Next, add a recipient to the draft and send it.

Below is the full example showing how to draft and send a message.

```ruby
#!/usr/bin/env ruby
require 'nylas'

nylas = Nylas::Client.new(api_key: "<NYLAS_API_KEY>")

request_body = {
  subject: 'From Nylas',
  body: 'This email was sent using the Nylas Email API. Visit https://nylas.com for details.',
  to: [{ name: 'My Nylas Friend', email: 'swag@example.com'}]
}

draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)
draft, _ = nylas.drafts.send(identifier: "<NYLAS_GRANT_ID>", draft_id: "<DRAFT_ID>")
```

## Reply to a message

The first step to reply to a message is to find the thread you want to reply to. The example below gets the most recent message in a user's inbox by returning only the first thread.

```ruby
thread, _ = nylas.threads.find(identifier: "<NYLAS_GRANT_ID>",
    thread_id: "<THREAD_ID>")
```

Next, create a draft that has the same `thread_id` and `subject` as the thread you're replying to, and assign its recipients.

```ruby
request_body = {
  subject: thread[:subject],
  body: 'This is my reply',
  to: [{ name: thread[:participants][0][:name],
      email: thread[:participants][0][:email]}],
  reply_to_message_id: thread[:message_ids][0]
}

draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)
```

Below is the full example showing how to reply to a message.

```ruby
#!/usr/bin/env ruby
require 'nylas'

nylas = Nylas::Client.new(
    api_key: "<NYLAS_API_KEY>"
)

request_body = {
  subject: thread[:subject],
  body: 'This is my reply',
  to: [{ name: thread[:participants][0][:name],
      email: thread[:participants][0][:email]}],
  reply_to_message_id: thread[:message_ids][0]
}

draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)
draft, _ = nylas.drafts.send(identifier: "<NYLAS_GRANT_ID>", draft_id: "<DRAFT_ID>")
```

## Attach a file to a message

The [Attachments endpoint](/docs/reference/api/attachments/) allows you to create and modify files that you can attach to messages. The following examples show how to take a file that's saved locally and upload it to Nylas for use with the Email API.

```ruby
file = Nylas::FileUtils.attach_file_request_builder('attachment.pdf')
```

Next, create a draft and attach the file to it.

```ruby
request_body = {
  to: [{name: 'My Nylas Friend', email: 'swag@example.com'}],
  subject: "With Love, from Nylas",
  body: "This email was sent using the Nylas Email API. Visit https://nylas.com for details.",
  attachments: [file]
}

nylas.messages.send(identifier: ENV["NYLAS_GRANT_ID"], request_body: request_body)
```

Below is the full example showing how to attach a file to a message.

```ruby
#!/usr/bin/env ruby
require 'nylas'

nylas = Nylas::Client.new(api_key: "<NYLAS_API_KEY>")
file = Nylas::FileUtils.attach_file_request_builder('attachment.pdf')

request_body = {
  to: [{name: 'My Nylas Friend', email: 'swag@example.com'}],
  subject: "With Love, from Nylas",
  body: "This email was sent using the Nylas Email API. Visit https://nylas.com for details.",
  attachments: [file]
}

nylas.messages.send(identifier: ENV["NYLAS_GRANT_ID"], request_body: request_body)
```