> For the complete documentation index, see [llms.txt](https://docs.planetcrust.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.planetcrust.com/human-connections/google/gmail/query-find-or-send-email.md).

# Query: Find or Send Email

<p align="center"><a class="button primary" data-icon="circle-caret-right">Try @Human</a><a class="button primary" data-icon="check">Buy @Human Subscriptions</a><a class="button primary" data-icon="comments">Join @Human Community</a></p>

<figure><img src="/files/5kJqaCa4hNci9Yb597Jb" alt="" width="188"><figcaption></figcaption></figure>

<p align="center">The Find or Send Email query searches for an existing email in Gmail or sends a new one if it doesn't exist.</p>

{% embed url="<https://www.youtube.com/watch?v=CyuUuaBSZzA>" %}

#### Example User Story

I want to check if I’ve already emailed a lead about our new product, or send a follow-up if I haven’t.

#### Fields

| Field Name | Field Type | Required |
| ---------- | ---------- | -------- |
| query      | String     | No       |
| to         | String     | Yes      |
| subject    | String     | Yes      |
| body       | String     | Yes      |

### Related Connections

[Gmail](/human-connections/google/gmail.md)

### Related Automations

Example link

### Code (Apache v2.0): Operation

```
{
  "handle": "find-or-send-email",
  "state": "inDev",
  "meta": {
    "short": "Find or Send Email",
    "description": "Find an email in Gmail or send a new email if not found."
  },
  "input": [
    {
      "name": "query",
      "type": "String",
      "required": false,
      "meta": {
        "description": "Search query to find the email."
      }
    },
    {
      "name": "to",
      "type": "String",
      "required": true,
      "meta": {
        "description": "Recipient email address."
      }
    },
    {
      "name": "subject",
      "type": "String",
      "required": true,
      "meta": {
        "description": "Subject of the email."
      }
    },
    {
      "name": "body",
      "type": "String",
      "required": true,
      "meta": {
        "description": "Body content of the email."
      }
    }
  ],
  "output": [
    {
      "name": "emailFound",
      "type": "Boolean",
      "selector": ["found"],
      "meta": {
        "description": "Indicates if an email was found."
      }
    },
    {
      "name": "emailId",
      "type": "String",
      "selector": ["id"],
      "meta": {
        "description": "ID of the found or sent email."
      }
    }
  ],
  "steps": [
    {
      "type": "http",
      "http": {
        "method": "GET",
        "path": "/gmail/v1/users/me/messages",
        "queryParams": {
          "q": "{{query}}"
        }
      }
    },
    {
      "type": "taq",
      "taq": {
        "condition": "{{not .found}}",
        "steps": [
          {
            "type": "http",
            "http": {
              "method": "POST",
              "path": "/gmail/v1/users/me/messages/send",
              "bodyTemplate": {
                "value": "{\"raw\": \"{{base64Encode .body}}\"}",
                "placeholders": [
                  {
                    "name": "body",
                    "type": "String",
                    "description": "Base64 encoded email body.",
                    "required": true
                  }
                ]
              },
              "headers": {
                "Content-Type": "application/json"
              }
            }
          }
        ]
      }
    }
  ]
}

```

<p align="center"><a class="button primary" data-icon="square-caret-right">Try @Human</a><a class="button primary" data-icon="check">Buy @Human Subscription</a><a class="button primary" data-icon="comments">Join @Human Community</a></p>


---

# 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 dynamically by asking a question.

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

```
GET https://docs.planetcrust.com/human-connections/google/gmail/query-find-or-send-email.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.
