# Sending an Email

This section guides you through the process of sending an email using the Mailzeet API. The Mailzeet API allows you to send transactional emails reliably and efficiently. To get started, you'll need to provide specific details about the email, including sender information, recipient details, subject, template ID, and customizable parameters.&#x20;

This guide will walk you through the steps to collect these details, format your request, and understand the response.

### Step 1: Collect the Email Details

To send an email, you need to gather the following required information:

* **Sender**: Information about the sender of the email.
* **Recipients**: A list of recipients to whom the email will be sent.
* **Subject**: The subject line of the email.
* **Template ID**: The ID of the email template to use, if applicable. You can find the templates on the Mailzeet dashboard, on the **Templates** page.

<figure><img src="https://3578008564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPfyFF9CqlMll3LpAoLlY%2Fuploads%2FVhqGSY0u9eXInrs8eF8p%2Fscreenshot-app.mailzeet.com-2024.07.14-13_51_01.png?alt=media&#x26;token=d2d0641a-0c00-4eb3-9214-2a5935f3b528" alt=""><figcaption><p>Mailzeet templates</p></figcaption></figure>

* **Params**: Customizable variables used in the email template.

#### Fields used for the requests

Below is a detailed table of the fields you need to or can include in your request:

<table><thead><tr><th width="254">Field Name</th><th width="116">Type</th><th>Required</th><th>Description</th></tr></thead><tbody><tr><td><code>sender</code></td><td>object</td><td>no</td><td>Details about the sender. Not required if <code>template_id</code> is present and template has default sender set.</td></tr><tr><td><code>sender.email</code></td><td>string</td><td>no</td><td>Must be an authorized email address for your SMTP server provided on Mailzeet.</td></tr><tr><td><code>sender.name</code></td><td>string</td><td>no</td><td>Name of the sender.</td></tr><tr><td><code>recipients</code></td><td>object[]</td><td>yes</td><td>A list of recipient details. Minimum 1, maximum 50.</td></tr><tr><td><code>recipients.email</code></td><td>string</td><td>yes</td><td>Email address of the recipient.</td></tr><tr><td><code>recipients.name</code></td><td>string</td><td>no</td><td>Name of the recipient. May not contain <code>;</code> or <code>,</code>.</td></tr><tr><td><code>cc</code></td><td>object[]</td><td>no</td><td>List of CC recipients. Maximum 10.</td></tr><tr><td><code>cc.email</code></td><td>string</td><td>yes</td><td>Email address of the CC recipient.</td></tr><tr><td><code>cc.name</code></td><td>string</td><td>no</td><td>Name of the CC recipient. May not contain <code>;</code> or <code>,</code>.</td></tr><tr><td><code>bcc</code></td><td>object[]</td><td>no</td><td>List of BCC recipients. Maximum 10.</td></tr><tr><td><code>bcc.email</code></td><td>string</td><td>yes</td><td>Email address of the BCC recipient.</td></tr><tr><td><code>bcc.name</code></td><td>string</td><td>no</td><td>Name of the BCC recipient.</td></tr><tr><td><code>reply_to.email</code></td><td>string</td><td>no</td><td>Reply-to email address.</td></tr><tr><td><code>reply_to.name</code></td><td>string</td><td>no</td><td>Name for the reply-to email address.</td></tr><tr><td><code>subject</code></td><td>string</td><td>yes *</td><td>Subject of the email. Max 255 characters. Not required if <code>template_id</code> is present and template has default subject set.</td></tr><tr><td><code>text</code></td><td>string</td><td>yes *</td><td>Email represented in a text (text/plain) format. Max size of 1 MB. * Only required if there's no <code>html</code> or <code>template_id</code> present.</td></tr><tr><td><code>html</code></td><td>string</td><td>yes *</td><td>Email represented in HTML (text/html) format. Max size of 2 MB. * Only required if there's no <code>text</code> or <code>template_id</code> present.</td></tr><tr><td><code>template_id</code></td><td>string</td><td>yes *</td><td>Template ID to use for the email. * Only required if there's no <code>text</code> or <code>html</code> present.</td></tr><tr><td><code>params</code></td><td>object</td><td>no</td><td>Allows using personalization in <code>{{var}}</code> syntax. Can be used in the subject, html, text fields, and templates.</td></tr></tbody></table>

### Step 2: Send the Request

Once you have collected the necessary details, you can send an email using the Mailzeet API by making a POST request to the following endpoint: `POST https://api.mailzeet.com/v1/mails`

#### Example Request Body

```json
{
    "sender": {
        "email": "hello@mailzeet.com",
        "name": "Mailzeet"
    },
    "recipients": [
        {
            "email": "john@mailzeet.com",
            "name": "John Doe"
        }
    ],
    "subject": "Hello from {company}!",
    "text": "This is just a friendly hello from your friends at {company}.",
    "html": "<b>This is just a friendly hello from your friends at {company}.</b>",
    "template_id": "xxxxxx",
    "params": {
        "company": "Mailzeet"
    }
}
```

### Example Response

After sending the request, you will receive a response from the Mailzeet API. The response will confirm whether the email was sent successfully or if there were any errors.

```json
{
    "message": "Email sent successfully.",
    "data": {
        "id": "email-id-12345",
        "status": "sent"
    },
    "errors": null
}
```

This response includes a message indicating the success of the operation, a data object with the email ID and status, and an errors field which will be null if there are no errors.

By following these steps, you can successfully send emails using the Mailzeet API. If you encounter any issues, refer to the response for error details and adjust your request accordingly.

Here are some examples of the same request in some popular languages.

{% tabs %}
{% tab title="cURL" %}

```bash
curl -X POST https://api.mailzeet.com/v1/mails \
-H "Content-Type: application/json" \
-d '{
    "sender": {
        "email": "hello@mailzeet.com",
        "name": "Mailzeet"
    },
    "recipients": [
        {
            "email": "john@mailzeet.com",
            "name": "John Doe"
        }
    ],
    "subject": "Hello from {company}!",
    "text": "This is just a friendly hello from your friends at {company}.",
    "html": "<b>This is just a friendly hello from your friends at {company}.</b>",
    "template_id": "xxxxxx",
    "params": {
        "company": "Mailzeet"
    }
}'

```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
const fetch = require('node-fetch');

const url = "https://api.mailzeet.com/v1/mails";
const payload = {
    sender: {
        email: "hello@mailzeet.com",
        name: "Mailzeet"
    },
    recipients: [
        {
            email: "john@mailzeet.com",
            name: "John Doe"
        }
    ],
    subject: "Hello from {company}!",
    text: "This is just a friendly hello from your friends at {company}.",
    html: "<b>This is just a friendly hello from your friends at {company}.</b>",
    template_id: "xxxxxx",
    params: {
        company: "Mailzeet"
    }
};

const headers = {
    "Content-Type": "application/json"
};

fetch(url, {
    method: "POST",
    headers: headers,
    body: JSON.stringify(payload)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

```

{% endtab %}

{% tab title="Python" %}

```python
import requests

url = "https://api.mailzeet.com/v1/mails"
payload = {
    "sender": {
        "email": "hello@mailzeet.com",
        "name": "Mailzeet"
    },
    "recipients": [
        {
            "email": "john@mailzeet.com",
            "name": "John Doe"
        }
    ],
    "subject": "Hello from {company}!",
    "text": "This is just a friendly hello from your friends at {company}.",
    "html": "<b>This is just a friendly hello from your friends at {company}.</b>",
    "template_id": "xxxxxx",
    "params": {
        "company": "Mailzeet"
    }
}

headers = {
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())

```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

func main() {
    url := "https://api.mailzeet.com/v1/mails"
    payload := map[string]interface{}{
        "sender": map[string]string{
            "email": "hello@mailzeet.com",
            "name": "Mailzeet",
        },
        "recipients": []map[string]string{
            {
                "email": "john@mailzeet.com",
                "name": "John Doe",
            },
        },
        "subject":    "Hello from {company}!",
        "text":       "This is just a friendly hello from your friends at {company}.",
        "html":       "<b>This is just a friendly hello from your friends at {company}.</b>",
        "template_id": "xxxxxx",
        "params": map[string]string{
            "company": "Mailzeet",
        },
    }

    jsonData, err := json.Marshal(payload)
    if err != nil {
        fmt.Println("Error marshaling JSON:", err)
        return
    }

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }

    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return
    }
    defer resp.Body.Close()

    var result map[string]interface{}
    json.NewDecoder(resp.Body).Decode(&result)
    fmt.Println(result)
}

```

{% endtab %}

{% tab title="Java" %}

```java
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class MailzeetEmailSender {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://api.mailzeet.com/v1/mails");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");

            String payload = "{"
                    + "\"sender\": {\"email\": \"hello@mailzeet.com\", \"name\": \"Mailzeet\"},"
                    + "\"recipients\": [{\"email\": \"john@mailzeet.com\", \"name\": \"John Doe\"}],"
                    + "\"subject\": \"Hello from {company}!\","
                    + "\"text\": \"This is just a friendly hello from your friends at {company}.\","
                    + "\"html\": \"<b>This is just a friendly hello from your friends at {company}.</b>\","
                    + "\"template_id\": \"xxxxxx\","
                    + "\"params\": {\"company\": \"Mailzeet\"}"
                    + "}";

            byte[] input = payload.getBytes(StandardCharsets.UTF_8);
            OutputStream os = conn.getOutputStream();
            os.write(input, 0, input.length);

            int responseCode = conn.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            // Further processing can be added here to read the response
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

```

{% endtab %}
{% endtabs %}
