> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codenullapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sending Emails with Attachments in Your Application (frontend)

>  Send emails to any recipient with a custom subject and message.

### Features

* Send emails to any recipient with a custom subject and message.
* Attach files as `Blob` or `File` objects.
* Authorization token support for secure API calls.

### Function Overview

The `sendEmailAsync` function sends an email using a POST request to the email service endpoint. It supports the following parameters:

#### Parameters

| Parameter     | Type               | Required | Description                                                        |
| ------------- | ------------------ | -------- | ------------------------------------------------------------------ |
| `to`          | `string`           | Yes      | Recipient's email address.                                         |
| `subject`     | `string`           | Yes      | Subject of the email.                                              |
| `message`     | `string`           | Yes      | Body of the email.                                                 |
| `from`        | `string`           | No       | Sender's email address. Defaults to the configured sender address. |
| `attachments` | `Array` of objects | No       | List of attachments to include in the email.                       |

**Attachment Object**

Each attachment in the `attachments` array should have the following properties:

| Property   | Type             | Required | Description                          |
| ---------- | ---------------- | -------- | ------------------------------------ |
| `filename` | `string`         | Yes      | The name of the file to be attached. |
| `file`     | `Blob` or `File` | Yes      | The binary content of the file.      |

***

### Usage Example

#### 1. Sending an Email Without Attachments

```javascript theme={null}
helpers.sendEmailAsync({
  to: 'recipient@example.com',
  subject: 'Hello!',
  message: 'This is a simple email without attachments.',
})
  .then((response) => {
    console.log('Email sent successfully:', response);
  })
  .catch((error) => {
    console.error('Error sending email:', error);
  });
```

#### 2. Sending an Email with Attachments using Blob file

```javascript theme={null}
// example file
const file = new Blob(['This is the content of the file.'], { type: 'text/plain' });
  
helpers.sendEmailAsync({
  to: 'recipient@example.com',
  subject: 'Hello!',
  message: 'This is a simple email without attachments.',
   attachments: [
        {
          filename: 'example.txt',
          file, // Binary content of the file
        },
      ],
})
  .then((response) => {
    console.log('Email sent successfully:', response);
  })
  .catch((error) => {
    console.error('Error sending email:', error);
  });
```

#### 3. Sending an Email with Attachments using Binary Content

```javascript theme={null}
// example file
 // Create binary content (e.g., a Uint8Array or Blob)
  const binaryContent = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]); // "Hello World"
  const file = new Blob([binaryContent], { type: 'application/octet-stream' });
  
helpers.sendEmailAsync({
  to: 'recipient@example.com',
  subject: 'Hello!',
  message: 'This is a simple email without attachments.',
   attachments: [
        {
          filename: 'example.txt',
          file, // The binary file
        },
      ],
})
  .then((response) => {
    console.log('Email sent successfully:', response);
  })
  .catch((error) => {
    console.error('Error sending email:', error);
  });
```

## Documentation for Sending Emails with Base64 Attachments (backend, hooks)

This guide explains how to use the `sendEmail` function to send emails with PDF attachments in base64 format.

```javascript theme={null}

  const doc = new jsPDF();
  doc.text('Hello world!', 10, 10);
  const pdfCreated = doc.output('datauristring');
  
   const archivoPDF = {
        filename: `filename.pdf`,
        content: pdfCreated.split('base64,')[1],
        encoding: 'base64'
    };
    const optionsEmail = {       
        to: `email@email.com`,
        subject: 'subject',
        message: 'message',
        attachments: [archivoPDF]
    }
    await sendEmail(optionsEmail);

    export { sendEmail };
```
