[Yandex Cloud documentation](../../index.md) > [Yandex Cloud Postbox](../index.md) > [Step-by-step guides](index.md) > Sending an email

# Sending an email

In Yandex Cloud Postbox, you can send an email:
* Using the [AWS CLI](#aws-cli).
* From a mail client via [SMTP](#smtp).
* Using the [AWS SDK](#aws-sdk).
* Using [cURL](#curl).

{% note info %}

To ensure the security of data transfer, Yandex Cloud Postbox supports TLS protocol versions 1.2 and 1.3.

{% endnote %}

## Getting started {#before-begin}

1. [Create](../../iam/operations/sa/create.md) a service account in the folder as the address. If you create the service account and address in different folders, you will get an error when attempting to send an email.
1. [Assign](../../iam/operations/sa/assign-role-for-sa.md) the `postbox.sender` [role](../security/index.md#postbox-sender) to the service account.
1. Create a key for the service account:

    * [API key](../../iam/operations/authentication/manage-api-keys.md#create-api-key) When creating an API key, set the scope to `yc.postbox.send`. Save the secret key you got in a secure location. You will not be able to view the secret key properties again after you close the window.

    * [Static access key](../../iam/operations/authentication/manage-access-keys.md#create-access-key). Save the ID and secret key to a secure location. You will not be able to view the secret key properties again after you close the window.

    * [IAM token](../../iam/operations/iam-token/create-for-sa.md). This authorization method is suitable for sending emails from [functions](../../functions/concepts/function.md) in Cloud Functions and [containers](../../serverless-containers/concepts/container.md) in Serverless Containers, as well as for Compute Cloud [VMs](../../compute/concepts/vm.md) linked to a service account. Select this method if you do not want to create and store static access keys.

        {% note warning %}

        Note that an IAM token is valid for up to 12 hours. If you need to provide authentication data in the mail client configuration file, use API key or password authentication.

        {% endnote %}

    Available combinations of authentication and emailing methods:

    ![arrow_right](../../_assets/console-icons/arrow-right.svg)<br>**Emailing method**<br>**Authentication method**<br>![arrow_down](../../_assets/console-icons/arrow-down.svg) | **AWS CLI** | **SMTP** | **AWS SDK** | **cURL**
    :---: | --- | --- | --- | ---
    |**API_key** | ![no](../../_assets/common/no.svg) | ![yes](../../_assets/common/yes.svg) | ![no](../../_assets/common/no.svg) | ![no](../../_assets/common/no.svg) ||
    **Static access key** | ![yes](../../_assets/common/yes.svg) | ![yes](../../_assets/common/yes.svg) | ![yes](../../_assets/common/yes.svg) | ![yes](../../_assets/common/yes.svg) ||
    **IAM_token** | ![no](../../_assets/common/no.svg) | ![yes](../../_assets/common/yes.svg) | ![no](../../_assets/common/no.svg) | ![yes](../../_assets/common/yes.svg)

## Sending an email {#send-email}

### AWS CLI {#aws-cli}

{% list tabs %}

- Static access key

    1. [Install](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) the AWS CLI.
    1. Set up the AWS CLI:
        1. Launch the interactive profile setup:
            ```
            aws configure
            ```
        1. Specify the previously obtained key ID of the `postbox-user` service account:
            ```
            AWS Access Key ID [****************ver_]: <service_account_key_ID>
            ```
        1. Specify the previously obtained secret key of the `postbox-user` service account:
            ```
            AWS Secret Access Key [****************w5lb]: <service_account_secret_key>
            ```
        1. Specify the ru-central1 default region name:
            ```
            Default region name [ru-central1]: ru-central1
            ```
        1. Specify `JSON` as the default format for output data:
            ```
            Default output format [None]: json
            ```
    
    1. Prepare two JSON files:
    
        * `destination.json`: File with a list of destination addresses:
    
            ```json
            {
              "ToAddresses": ["test@example.com"]
            }
            ```
    
        * `message.json`: File with the subject and content of the email:
    
           ```json
           {
             "Simple": {
               "Subject": {
                 "Data": "Test message",
                 "Charset": "UTF-8"
               },
               "Body": {
                 "Text": {
                   "Data": "Test message. Hello!",
                   "Charset": "UTF-8"
                 }
               }
             }
           }
           ```
    
    1. Send an email using the AWS CLI:
    
        ```bash
        aws sesv2 send-email \
          --from-email-address mail@example.com \
          --destination file://destination.json \
          --content file://message.json \
          --endpoint-url https://postbox.cloud.yandex.net
        ```
    
    1. Check the mailbox specified in `destination.json` for the email.

{% endlist %}

### SMTP {#smtp}

{% list tabs group=auth_keys %}

- API key {#api-key}

    1. In the SMTP server settings of your email client, specify the following parameters:
    
        #|
        || | **Email client supports STARTTLS** | **Email client supports SMTPS instead of STARTTLS** ||
        || **Server name** | `postbox.cloud.yandex.net` {align="center"} | > ||
        || **Port** | `587` | `465` ||
        || **Username** | ID of the created API key {align="center"} | > ||
        || **Password** | Secret part of the created API key {align="center"} | > ||
        |#
    
    1. Send an email using your email client and make sure the specified recipients receive it.

- Static access key {#static-key}

    1. Get a password using the previously created static access key of the service account. To do this, run the `generate.py` script. Use Python 3 or higher.
        ```
        python generate.py <service_account_secret_key>
        ```
    
        {% cut "generate.py" %}
    
        ```
        #!/usr/bin/env python3
    
        import hmac
        import hashlib
        import base64
        import argparse
        import sys
    
        # These values are required to calculate the signature. Do not change them.
        DATE = "20230926"
        SERVICE = "postbox"
        MESSAGE = "SendRawEmail"
        REGION = "ru-central1"
        TERMINAL = "aws4_request"
        VERSION = 0x04
    
    
        def sign(key, msg):
            return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
    
    
        def calculate_key(secret_access_key):
            signature = sign(("AWS4" + secret_access_key).encode("utf-8"), DATE)
            signature = sign(signature, REGION)
            signature = sign(signature, SERVICE)
            signature = sign(signature, TERMINAL)
            signature = sign(signature, MESSAGE)
            signature_and_version = bytes([VERSION]) + signature
            smtp_password = base64.b64encode(signature_and_version)
            return smtp_password.decode("utf-8")
    
    
        def main():
            if sys.version_info[0] < 3:
                raise Exception("Must be using Python 3")
    
            parser = argparse.ArgumentParser(
                description="Convert a Secret Access Key to an SMTP password."
            )
            parser.add_argument("secret", help="The Secret Access Key to convert.")
            args = parser.parse_args()
    
            print(calculate_key(args.secret))
    
    
        if __name__ == "__main__":
            main()
        ```
    
        {% endcut %}
    
    1. In the SMTP server settings of your email client, specify the following parameters:
    
        #|
        || | **Email client supports STARTTLS** | **Email client supports SMTPS instead of STARTTLS** ||
        || **Server name** | `postbox.cloud.yandex.net` {align="center"} | > ||
        || **Port** | `587` | `465` ||
        || **Username** | ID of the previously created static access key {align="center"} | > ||
        || **Password** | Password obtained in the previous step {align="center"} | > ||
        |#
    
    1. Send an email using your email client and make sure the specified recipients receive it.

- IAM token {#iam-token}

    1. In your SMTP mail client settings, specify the following parameters:
    
        #|
        || | **Email client supports STARTTLS** | **Email client supports SMTPS instead of STARTTLS** ||
        || **Server name** | `postbox.cloud.yandex.net` {align="center"} | > ||
        || **Port** | `587` | `465` ||
        || **Username** | `IAM_TOKEN` {align="center"} | > ||
        || **Password** | Service account IAM token {align="center"} | > ||
        |#
    
    1. Send an email using your email client and make sure the specified recipients receive it.

{% endlist %}

### AWS SDK {#aws-sdk}

You can send an email using the AWS SDK for .NET Core, Go, JavaScript, and Python. For more information, see these tutorials:

* [Sending emails using AWS SDK for .NET Core](../tutorials/send-emails-aws-sdk-csharp.md)
* [Sending emails using AWS SDK for Go](../tutorials/send-emails-aws-sdk-go.md)
* [Sending emails using the AWS SDK for JavaScript](../tutorials/send-emails-aws-sdk-js.md)
* [Sending emails using the AWS SDK for Python](../tutorials/send-emails-aws-sdk-python.md)

### cURL {#curl}

To send an email using [cURL](https://curl.se/), run this command:

{% list tabs %}

- Static access key

    ```bash
    curl \
        --request POST \
        --url 'https://postbox.cloud.yandex.net/v2/email/outbound-emails' \
        --header 'Content-Type: application/json' \
        --user "${KEY_ID}:${SECRET_KEY}" \
        --aws-sigv4 "aws:amz:ru-central1:ses" \
        --data-binary '@email.json'
    ```

- IAM token

    ```bash
    curl \
        --request POST \
        --url 'https://postbox.cloud.yandex.net/v2/email/outbound-emails' \
        --header 'Content-Type: application/json' \
        --header 'X-YaCloud-SubjectToken: <IAM_token>' \
        --data '{
            "FromEmailAddress": "<sender>",
            "Destination": {
                "ToAddresses": ["<recipient>"]
            },
            "Content": {
                "Simple": {
                    "Subject": {
                        "Data": "<subject>"
                    },
                    "Body": {
                        "Text": {
                            "Data": "<email text>"
                        }
                    }
                }
            }
        }'
    ```

{% endlist %}

You can provide the [request](../aws-compatible-api/api-ref/send-email.md) body in the command line argument or file.

{% cut "File example" %}

```json
{
    "FromEmailAddress": "<sender>",
    "Destination": {
        "ToAddresses": ["<recipient>"]
    },
    "Content": {
        "Simple": {
            "Subject": {
                "Data": "<subject>"
            },
            "Body": {
                "Text": {
                    "Data": "<email text>"
                }
            }
        }
    }
}
```

{% endcut %}

To use [AWS Signature Version 4](https://docs.amazonaws.cn/en_us/IAM/latest/UserGuide/reference_aws-signing.html) to sign a request, specify the `--aws-sigv4` parameter. To learn how to create a signature by yourself, see [Signing requests](../aws-compatible-api/signing-requests.md).