# Send an email

To send emails to other SAFE Network users, you need to know their email ID.

First, the app retrieves the encryption key associated with the appendable data that belongs to the recipient. Then, it encrypts the email using that encryption key and saves it as immutable data. Finally, it appends the immutable data that represents the email to the appendable data of the recipient.

#### Contents

![Compose Mail page](https://3287240797-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2iQcySPVDTOOrtj1Mi%2F-M2iQd_zdonbfes7H0XS%2F-M2iQeA5pI27k4elb0vC%2Fcompose-mail-page.png?generation=1584548196859312\&alt=media)

The [JSON](https://en.wikipedia.org/wiki/JSON) data for the above email would look like this:

```javascript
{
  "subject": "Test",
  "from": "example",
  "time": "Fri, 16 Sep 2016 10:49:44 GMT",
  "body": "123"
}
```

## Get the appendable data of the recipient

Before fetching the encryption key of the recipient, the app needs to obtain an appendable data handle.

### Get a data identifier handle

First, the app fetches a data identifier handle for the appendable data of the recipient.

#### [Get data ID handle](https://api.safedev.org/low-level-api/data-id/get-data-id-handle.html#for-appendable-data)

```
POST /data-id/appendable-data
```

**data\_id\_handle\_actions.js**

```javascript
export const getAppendableDataIdHandle = (token, name) => ({
  type: ACTION_TYPES.GET_STRUCTURED_DATA_ID_HANDLE,
  payload: {
    request: {
      method: 'post',
      url: '/data-id/appendable-data',
      headers: {
        'Authorization': token
      },
      data: {
        isPrivate: true,
        name
      }
    }
  }
});
```

The name of the appendable data is obtained by hashing the email ID of the recipient:

**app\_utils.js**

```javascript
export const hashEmailId = emailId => {
  return crypto.createHash('sha256').update(emailId).digest('base64');
};
```

### Get an appendable data handle

The app fetches an appendable data handle using the data identifier handle previously obtained.

#### [Get appendable data handle](https://api.safedev.org/low-level-api/appendable-data/get-appendable-data-handle.html)

```
GET /appendable-data/handle/:dataIdHandle
```

**appendable\_data\_actions.js**

```javascript
export const fetchAppendableDataHandle = (token, dataIdHandle) => { // id => appendable data id
  return {
    type: ACTION_TYPES.FETCH_APPENDABLE_DATA_HANDLER,
    payload: {
      request: {
        url: `/appendable-data/handle/${dataIdHandle}`,
        headers: {
          'Authorization': token,
          'Is-Private': true
        }
      }
    }
  };
};
```

### Drop the data identifier handle

The app drops the data identifier handle for the appendable data of the recipient.

#### [Drop data ID handle](https://api.safedev.org/low-level-api/data-id/drop-data-id-handle.html)

```
DELETE /data-id/:handleId
```

**data\_id\_handle\_actions.js**

```javascript
export const dropHandler = (token, handleId) => ({
  type: ACTION_TYPES.DROP_HANDLER,
  payload: {
    request: {
      method: 'delete',
      url: `/data-id/${handleId}`,
      headers: {
        'Authorization': token
      }
    }
  }
});
```

## Get the encryption key of the recipient

After the appendable data handle is successfully obtained, the app fetches an handle for the public encryption key of the recipient. By encrypting your email using that encryption key, only the recipient will be able to read it. This is known as asymmetric encryption.

#### [Get encryption key handle](https://api.safedev.org/low-level-api/appendable-data/encryption-key/get-encryption-key-handle.html)

```
GET /appendable-data/encrypt-key/:handleId
```

**appendable\_data\_actions.js**

```javascript
export const getEncryptedKey = (token, handleId) => ({
  type: ACTION_TYPES.GET_ENCRYPTED_KEY,
  payload: {
    request: {
      url: `/appendable-data/encrypt-key/${handleId}`,
      headers: {
        'Authorization': token
      }
    }
  }
});
```

### Get a cipher options handle

The app fetches a cipher options handle for asymmetric encryption using the encryption key handle of the recipient.

#### [Get cipher options handle](https://api.safedev.org/low-level-api/cipher-options/get-cipher-options-handle.html)

```
GET /cipher-opts/:encType/:keyHandle?
```

**cipher-opts\_actions.js**

```javascript
export const getCipherOptsHandle = (token, encType, keyHandle='') => ({
  type: ACTION_TYPES.GET_CIPHER_OPTS_HANDLE,
  payload: {
    request: {
      url: `/cipher-opts/${encType}/${keyHandle}`,
      headers: {
        'Authorization': token,
      }
    }
  }
});
```

### Drop the encryption key handle

The app drops the encryption key handle of the recipient.

#### [Drop encryption key handle](https://api.safedev.org/low-level-api/appendable-data/encryption-key/drop-encryption-key-handle.html)

```
DELETE /appendable-data/encrypt-key/:handleId
```

**appendable\_data\_actions.js**

```javascript
export const deleteEncryptedKey = (token, handleId) => ({
  type: ACTION_TYPES.DELETE_ENCRYPTED_KEY,
  payload: {
    request: {
      method: 'delete',
      url: `/appendable-data/encrypt-key/${handleId}`,
      headers: {
        'Authorization': token
      }
    }
  }
});
```

## Create the email

The app creates an email using the cipher handle that contains the encryption key of the recipient.

### Get an immutable data writer handle

First, the app fetches an immutable data writer handle.

#### [Get immutable data writer handle](https://api.safedev.org/low-level-api/immutable-data/get-immutable-data-handle.html#get-immutable-data-writer-handle)

```
GET /immutable-data/writer
```

**immutable\_data\_actions.js**

```javascript
export const createImmutableDataWriterHandle = (token) => ({
  type: ACTION_TYPES.CREATE_IMMUT_WRITER_HANDLE,
  payload: {
    request: {
      url: `/immutable-data/writer`,
      headers: {
        'Authorization': token
      }
    }
  }
});
```

### Write immutable data

The app stores the email as immutable data using the immutable data writer handle.

#### [Write immutable data](https://api.safedev.org/low-level-api/immutable-data/write-immutable-data.html)

```
POST /immutable-data/:handleId
```

**immutable\_data\_actions.js**

```javascript
export const writeImmutableData = (token, handleId, data) => {
  const dataByteArray = new Uint8Array(new Buffer(JSON.stringify(data)));
  return {
    type: ACTION_TYPES.WRITE_IMMUT_DATA,
    payload: {
      request: {
        method: 'post',
        url: `/immutable-data/${handleId}`,
        headers: {
          'content-type': 'text/plain',
          'Authorization': token
        },
        data: dataByteArray
      }
    }
  };
};
```

### Close the immutable data writer

The app encrypts the data map of the email using the cipher handle that contains the encryption key handle of the recipient. The data map is stored as immutable data on the SAFE Network.

#### [Close immutable data writer](https://api.safedev.org/low-level-api/immutable-data/close-immutable-data-writer.html)

```
PUT /immutable-data/:handleId/:cipherOptsHandle
```

**immutable\_data\_actions.js**

```javascript
export const putImmutableData = (token, handleId, cipherOptsHandle) => ({
  type: ACTION_TYPES.PUT_IMMUT_DATA,
  payload: {
    request: {
      method: 'put',
      url: `/immutable-data/${handleId}/${cipherOptsHandle}`,
      headers: {
        'Authorization': token,
      }
    }
  }
});
```

Once the write operation is successful, the API returns a data identifier handle for the data map of the email.

### Drop the immutable data writer handle

The app drops the immutable data writer handle.

#### [Drop immutable data writer handle](https://api.safedev.org/low-level-api/immutable-data/drop-immutable-data-handle.html#drop-immutable-data-writer-handle)

```
DELETE /immutable-data/writer/:handleId
```

**immutable\_data\_actions.js**

```javascript
export const closeImmutableDataWriter = (token, handleId) => ({
  type: ACTION_TYPES.CLOSE_IMMUT_DATA_WRITER,
  payload: {
    request: {
      method: 'delete',
      url: `/immutable-data/writer/${handleId}`,
      headers: {
        'Authorization': token,
      }
    }
  }
});
```

## Append the email to the appendable data

The app adds the data identifier handle representing your email to the appendable data of the recipient.

#### [Append data](https://api.safedev.org/low-level-api/appendable-data/append-data.html)

```
PUT /appendable-data/:handleId/:dataIdHandle
```

**appendable\_data\_actions.js**

```javascript
export const appendAppendableData = (token, handleId, dataIdHandle) => ({
  type: ACTION_TYPES.APPEND_APPENDABLE_DATA,
  payload: {
    request: {
      method: 'put',
      url: `/appendable-data/${handleId}/${dataIdHandle}`,
      headers: {
        'Authorization': token
      }
    }
  }
});
```

### Drop the appendable data handle

After your email is successfully appended to the appendable data of the recipient, the app drops the appendable data handle.

#### [Drop appendable data handle](https://api.safedev.org/low-level-api/appendable-data/drop-appendable-data-handle.html)

```
DELETE /appendable-data/handle/:handleId
```

**appendable\_data\_actions.js**

```javascript
export const dropAppendableDataHandle = (token, handleId) => ({
  type: ACTION_TYPES.DROP_APPENDABLE_DATA_HANDLE,
  payload: {
    request: {
      method: 'delete',
      url: `/appendable-data/handle/${handleId}`,
      headers: {
        'Authorization': token
      }
    }
  }
});
```
