# Send a Test Marketing Email

## Operation overview

```json
{"path":"https://api.sendgrid.com/v3/marketing/test/send_email","method":"post","servers":[{"url":"https://api.sendgrid.com","description":"The Twilio SendGrid v3 API"}]}
```

**This endpoint allows you to send a test marketing email to a list of email addresses**.

Before sending a marketing message, you can test it using this endpoint. You may specify up to **10 contacts** in the `emails` request body field. You must also specify a `template_id` and include either a `from_address` or `sender_id`. You can manage your templates with the [Twilio SendGrid App](https://mc.sendgrid.com/dynamic-templates) or the [Transactional Templates API](/docs/sendgrid/api-reference/transactional-templates).

> Please note that this endpoint works with Dynamic Transactional Templates only. Legacy Transactional Templates will not be delivered.

For more information about managing Dynamic Transactional Templates, see [How to Send Email with Dynamic Transactional Templates](/docs/sendgrid/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/).

You can also test your Single Sends in the [Twilio SendGrid Marketing Campaigns UI](https://mc.sendgrid.com/single-sends).

## Operation details

### Authentication

API Key

### Headers

```json
[{"in":"header","name":"Authorization","required":true,"default":"Bearer <<YOUR_API_KEY_HERE>>","schema":{"type":"string"}}]
```

### Request body

```json
{"schema":{"type":"object","required":["template_id","emails"],"example":{"template_id":"f8f77db8-b9fa-4b3c-9ee8-de3d582016b8","version_id_override":"7734f757-8eb8-4d22-b7f0-779a72f32986","sender_id":6060664,"custom_unsubscribe_url":"https://example.com/unsubscribe","suppression_group_id":21865513,"emails":["janedoe@example.com","tiramisu@example.com","bundt@example.com"]},"properties":{"template_id":{"type":"string","format":"uuid","description":"The ID of the template that you would like to use. If you use a template that contains a subject and content (either text or HTML), then those values specified at the personalizations or message level will not be used."},"version_id_override":{"type":"string","format":"uuid","description":" You can override the active template with an alternative template version by passing the version ID in this field. If this field is blank, the active template version will be used."},"sender_id":{"type":"integer","description":"This ID must belong to a verified sender. Alternatively, you may supply a `from_address` email."},"custom_unsubscribe_url":{"type":"string","description":"A custom unsubscribe URL."},"suppression_group_id":{"type":"integer"},"emails":{"type":"array","uniqueItems":true,"minItems":1,"maxItems":10,"description":"An array of email addresses you want to send the test message to.","items":{"type":"string","format":"email"}},"from_address":{"type":"string","description":"You can either specify this address or specify a verified sender ID.","format":"email"}}},"encodingType":"application/json"}
```

### Responses

```json
[{"responseCode":"202","schema":{"description":"","content":{"application/json":{"schema":{"type":"object"}}}}},{"responseCode":"400","schema":{"description":"","content":{"application/json":{"schema":{"type":"object","example":{"errors":[{"field":"field_name","message":"error message"}]},"refName":"ErrorResponse","modelName":"ErrorResponse","properties":{"errors":{"type":"array","items":{"type":"object","properties":{"message":{"type":"string","description":"An error message."},"field":{"description":"When applicable, this property value will be the field that generated the error.","nullable":true,"type":"string"},"help":{"type":"object","description":"When applicable, this property value will be helper text or a link to documentation to help you troubleshoot the error."}}}},"id":{"type":"string","description":"When applicable, this property value will be an error ID."}}}}},"refName":"#/components/responses/TestSend400","modelName":"__components_responses_TestSend400"}}]
```

Send a Test Marketing Email

```js
const client = require("@sendgrid/client");
client.setApiKey(process.env.SENDGRID_API_KEY);

const data = {
  template_id: "f8f77db8-b9fa-4b3c-9ee8-de3d582016b8",
  version_id_override: "7734f757-8eb8-4d22-b7f0-779a72f32986",
  sender_id: 6060664,
  custom_unsubscribe_url: "https://example.com/unsubscribe",
  suppression_group_id: 21865513,
  emails: ["janedoe@example.com", "tiramisu@example.com", "bundt@example.com"],
};

const request = {
  url: `/v3/marketing/test/send_email`,
  method: "POST",
  body: data,
};

client
  .request(request)
  .then(([response, body]) => {
    console.log(response.statusCode);
    console.log(response.body);
  })
  .catch((error) => {
    console.error(error);
  });
```

```python
import os
from sendgrid import SendGridAPIClient


sg = SendGridAPIClient(os.environ.get("SENDGRID_API_KEY"))

data = {
    "template_id": "f8f77db8-b9fa-4b3c-9ee8-de3d582016b8",
    "version_id_override": "7734f757-8eb8-4d22-b7f0-779a72f32986",
    "sender_id": 6060664,
    "custom_unsubscribe_url": "https://example.com/unsubscribe",
    "suppression_group_id": 21865513,
    "emails": [
        "janedoe@example.com",
        "tiramisu@example.com",
        "bundt@example.com",
    ],
}

response = sg.client.marketing.test.send_email.post(request_body=data)

print(response.status_code)
print(response.body)
print(response.headers)
```

```csharp
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SendGrid;

public class Program {
    public static async Task Main() {
        string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
        var client = new SendGridClient(apiKey);

        var data =
            @"{
            ""template_id"": ""f8f77db8-b9fa-4b3c-9ee8-de3d582016b8"",
            ""version_id_override"": ""7734f757-8eb8-4d22-b7f0-779a72f32986"",
            ""sender_id"": 6060664,
            ""custom_unsubscribe_url"": ""https://example.com/unsubscribe"",
            ""suppression_group_id"": 21865513,
            ""emails"": [
                ""janedoe@example.com"",
                ""tiramisu@example.com"",
                ""bundt@example.com""
            ]
        }";

        var response = await client.RequestAsync(
            method: SendGridClient.Method.POST,
            urlPath: "marketing/test/send_email",
            requestBody: data);

        Console.WriteLine(response.StatusCode);
        Console.WriteLine(response.Body.ReadAsStringAsync().Result);
        Console.WriteLine(response.Headers.ToString());
    }
}
```

```java
import com.sendgrid.*;
import java.io.IOException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Arrays;

public class Example {
    public static void main(String[] args) throws IOException {
        try {
            SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
            Request request = new Request();
            request.setMethod(Method.POST);
            request.setEndpoint("/marketing/test/send_email");
            request.setBody(new JSONObject(new HashMap<String, Object>() {
                {
                    put("template_id", "f8f77db8-b9fa-4b3c-9ee8-de3d582016b8");
                    put("version_id_override", "7734f757-8eb8-4d22-b7f0-779a72f32986");
                    put("sender_id", 6060664);
                    put("custom_unsubscribe_url", "https://example.com/unsubscribe");
                    put("suppression_group_id", 21865513);
                    put("emails", Arrays.asList("janedoe@example.com", "tiramisu@example.com", "bundt@example.com"));
                }
            }).toString());
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
        } catch (IOException ex) {
            throw ex;
        }
    }
}
```

```go
package main

import (
	"fmt"
	"github.com/sendgrid/sendgrid-go"
	"os"
)

func main() {
	apiKey := os.Getenv("SENDGRID_API_KEY")
	host := "https://api.sendgrid.com"
	request := sendgrid.GetRequest(apiKey, "/v3/marketing/test/send_email", host)
	request.Method = "POST"
	request.Body = []byte(`{
  "template_id": "f8f77db8-b9fa-4b3c-9ee8-de3d582016b8",
  "version_id_override": "7734f757-8eb8-4d22-b7f0-779a72f32986",
  "sender_id": 6060664,
  "custom_unsubscribe_url": "https://example.com/unsubscribe",
  "suppression_group_id": 21865513,
  "emails": [
    "janedoe@example.com",
    "tiramisu@example.com",
    "bundt@example.com"
  ]
}`)
	response, err := sendgrid.API(request)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}
```

```php
<?php
// Uncomment the next line if you're using a dependency loader (such as Composer) (recommended)
// require 'vendor/autoload.php';

// Uncomment next line if you're not using a dependency loader (such as Composer)
// require_once '<PATH TO>/sendgrid-php.php';

$apiKey = getenv("SENDGRID_API_KEY");
$sg = new \SendGrid($apiKey);
$request_body = json_decode('{
    "template_id": "f8f77db8-b9fa-4b3c-9ee8-de3d582016b8",
    "version_id_override": "7734f757-8eb8-4d22-b7f0-779a72f32986",
    "sender_id": 6060664,
    "custom_unsubscribe_url": "https://example.com/unsubscribe",
    "suppression_group_id": 21865513,
    "emails": [
        "janedoe@example.com",
        "tiramisu@example.com",
        "bundt@example.com"
    ]
}');

try {
    $response = $sg->client
        ->marketing()
        ->test()
        ->send_email()
        ->post($request_body);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $ex) {
    echo "Caught exception: " . $ex->getMessage();
}
```

```ruby
require 'sendgrid-ruby'
include SendGrid

sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
data = JSON.parse('{
  "template_id": "f8f77db8-b9fa-4b3c-9ee8-de3d582016b8",
  "version_id_override": "7734f757-8eb8-4d22-b7f0-779a72f32986",
  "sender_id": 6060664,
  "custom_unsubscribe_url": "https://example.com/unsubscribe",
  "suppression_group_id": 21865513,
  "emails": [
    "janedoe@example.com",
    "tiramisu@example.com",
    "bundt@example.com"
  ]
}')

response = sg.client.marketing.test.send_email.post(request_body: data)
puts response.status_code
puts response.headers
puts response.body
```

```bash
curl -X POST "https://api.sendgrid.com/v3/marketing/test/send_email" \
--header "Authorization: Bearer $SENDGRID_API_KEY" \
--header "Content-Type: application/json" \
--data '{"template_id": "f8f77db8-b9fa-4b3c-9ee8-de3d582016b8", "version_id_override": "7734f757-8eb8-4d22-b7f0-779a72f32986", "sender_id": 6060664, "custom_unsubscribe_url": "https://example.com/unsubscribe", "suppression_group_id": 21865513, "emails": ["janedoe@example.com", "tiramisu@example.com", "bundt@example.com"]}'
```
