# email

## getLastEmail

Retrieves last unseen email.

\*\* Usage example:\*\*

```javascript
email.init('[YOUR_EMAIL]@gmail.com', 'password', 'imap.gmail.com', 993, true, 3000);
var mail = email.getLastEmail(60, 'email subject', 5000);
log.info(mail);

if(r.attachments && r.attachments.length > 0){
   const fs = require('fs');
   r.attachments.map((attachment) => {
   	let fileDescriptor;
   	try{
   		fileDescriptor = fs.openSync(attachment.filename, 'w');
   	} catch(e) {
   		throw 'could not open file: ' + e;
   	}
   	
   	try{
   		fs.writeFileSync(fileDescriptor, attachment.data);
   	} catch(e) {
   		throw 'error writing file: ' + e;
   	}
   	
   	fs.closeSync(fileDescriptor);
   });
}
```

**Parameters:**

| Name           | Type              | Description                                                                 |
| -------------- | ----------------- | --------------------------------------------------------------------------- |
| `sinceMinutes` | `Number`          | Search for emails received since the specified amount of minutes into past. |
| `subject`      | `String`\|`Regex` | Return email matching the specified subject.                                |
| `timeout`      | `Number`          | Timeout (in milliseconds) for waiting for the message to arrive.            |

**Returns:**

`Object` - Email body, text, textAsHtml, attachments if any, and TO, FROM, SUBJECT, DATE headers.

## init

Set email connection details.

**Parameters:**

| Name          | Type      | Description                                                                                                               |
| ------------- | --------- | ------------------------------------------------------------------------------------------------------------------------- |
| `user`        | `String`  | Username (e.g. '<your@email.address>').                                                                                   |
| `password`    | `String`  | Password.                                                                                                                 |
| `host`        | `String`  | Host name (e.g. 'imap.gmail.com').                                                                                        |
| `port`        | `Number`  | Port number (e.g. 993).                                                                                                   |
| `tls`         | `Boolean` | true to use TLS, false otherwise.                                                                                         |
| `authTimeout` | `Number`  | `optional` Authentication timeout in milliseconds.                                                                        |
| `enableSNI`   | `Boolean` | `optional` Enable sending SNI when establishing the connection. This is required for some mail servers. Default is false. |

## send

Send email using SMTP.

**Parameters:**

| Name      | Type              | Description                              |
| --------- | ----------------- | ---------------------------------------- |
| `to`      | `String`\|`Array` | Receiver's email address.                |
| `subject` | `String`          | Email subject.                           |
| `content` | `String`          | Email body as a plain text.              |
| `html`    | `String`          | `optional` Optional, Email body as HTML. |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.oxygenhq.org/download-installation-start/modules/module-email.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
