New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mailtrap

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mailtrap - npm Package Compare versions

Comparing version 3.1.1 to 3.2.0-beta.0

2

dist/index.d.ts
import { Mail, SendResponse } from "./lib/types";
type MailtrapClientConfig = {
declare type MailtrapClientConfig = {
endpoint?: string;

@@ -4,0 +4,0 @@ token: string;

@@ -28,2 +28,38 @@ "use strict";

};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -34,10 +70,10 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

exports.MailtrapClient = void 0;
const http = __importStar(require("http"));
const https = __importStar(require("https"));
const axios_1 = __importDefault(require("axios"));
const encodeMailBuffers_1 = __importDefault(require("./lib/encodeMailBuffers"));
const MailtrapError_1 = __importDefault(require("./lib/MailtrapError"));
const MAILTRAP_ENDPOINT = "https://send.api.mailtrap.io";
class MailtrapClient {
constructor({ endpoint = MAILTRAP_ENDPOINT, token }) {
var http = __importStar(require("http"));
var https = __importStar(require("https"));
var axios_1 = __importStar(require("axios"));
var encodeMailBuffers_1 = __importDefault(require("./lib/encodeMailBuffers"));
var MAILTRAP_ENDPOINT = "https://send.api.mailtrap.io";
var MailtrapClient = /** @class */ (function () {
function MailtrapClient(_a) {
var _b = _a.endpoint, endpoint = _b === void 0 ? MAILTRAP_ENDPOINT : _b, token = _a.token;
this.axios = axios_1.default.create({

@@ -48,5 +84,4 @@ httpAgent: new http.Agent({ keepAlive: true }),

headers: {
Authorization: `Bearer ${token}`,
Authorization: "Bearer ".concat(token),
Connection: "keep-alive",
"User-Agent": "mailtrap-nodejs (https://github.com/railsware/mailtrap-nodejs)",
},

@@ -57,26 +92,36 @@ maxRedirects: 0,

}
async send(mail) {
const preparedMail = (0, encodeMailBuffers_1.default)(mail);
try {
const axiosResponse = await this.axios.post("/api/send", preparedMail);
return axiosResponse.data;
}
catch (err) {
if (axios_1.default.isAxiosError(err)) {
const serverErrors = err.response?.data &&
typeof err.response.data === "object" &&
"errors" in err.response.data &&
err.response.data.errors instanceof Array
? err.response.data.errors
: undefined;
const message = serverErrors ? serverErrors.join(", ") : err.message;
// @ts-expect-error weird typing around Error class, but it's tested to work
throw new MailtrapError_1.default(message, { cause: err });
}
// should not happen, but otherwise rethrow error as is
throw err;
}
}
}
MailtrapClient.prototype.send = function (mail) {
var _a, _b;
return __awaiter(this, void 0, void 0, function () {
var preparedMail, axiosResponse, err_1, unwrappedError;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
preparedMail = (0, encodeMailBuffers_1.default)(mail);
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.axios.post("/api/send", preparedMail)];
case 2:
axiosResponse = _c.sent();
return [2 /*return*/, axiosResponse.data];
case 3:
err_1 = _c.sent();
if (err_1 instanceof axios_1.AxiosError) {
unwrappedError = (_b = (_a = err_1.response) === null || _a === void 0 ? void 0 : _a.data) !== null && _b !== void 0 ? _b : {
success: false,
error: ["connection failed"],
};
throw unwrappedError;
}
// should not happen, but otherwise rethrow error as is
throw err_1;
case 4: return [2 /*return*/];
}
});
});
};
return MailtrapClient;
}());
exports.MailtrapClient = MailtrapClient;
__exportStar(require("./lib/types"), exports);
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
function encodeMailBuffers(mail) {
const preparedMail = { ...mail };
var preparedMail = __assign({}, mail);
if ("text" in preparedMail && preparedMail.text instanceof Buffer) {

@@ -12,8 +23,5 @@ preparedMail.text = preparedMail.text.toString();

if (preparedMail.attachments) {
preparedMail.attachments = preparedMail.attachments.map((attachment) => {
preparedMail.attachments = preparedMail.attachments.map(function (attachment) {
if (attachment.content instanceof Buffer) {
return {
...attachment,
content: attachment.content.toString("base64"),
};
return __assign(__assign({}, attachment), { content: attachment.content.toString("base64") });
}

@@ -20,0 +28,0 @@ return attachment;

/// <reference types="node" />
export type Mail = {
export declare type Mail = {
from: Address;

@@ -11,21 +11,21 @@ to: Address[];

} & (MailContent | MailFromTemplateContent);
type MailContent = {
declare type MailContent = {
subject: string;
category?: string;
} & (TextMailContent | HTMLMailContent);
type MailFromTemplateContent = {
declare type MailFromTemplateContent = {
template_uuid: string;
template_variables?: Record<string, string | number | boolean>;
};
type TextMailContent = {
declare type TextMailContent = {
text: string | Buffer;
};
type HTMLMailContent = {
declare type HTMLMailContent = {
html: string | Buffer;
};
export type Address = {
export declare type Address = {
name?: string;
email: string;
};
export type Attachment = {
export declare type Attachment = {
filename: string;

@@ -37,7 +37,7 @@ type: string;

};
export type SendResponse = {
export declare type SendResponse = {
success: true;
message_ids: string[];
};
export type SendError = {
export declare type SendError = {
success: false;

@@ -44,0 +44,0 @@ error: string[];

{
"name": "mailtrap",
"version": "3.1.1",
"description": "Official mailtrap.io API client",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"scripts": {
"prepublish": "rm -rf dist && tsc --project tsconfig.build.json",
"lint:eslint": "yarn run eslint . --ext .js,.ts",
"lint:tsc": "tsc -p . --noEmit --incremental false",
"lint": "yarn lint:eslint && yarn lint:tsc",
"test": "jest"
},
"repository": "https://github.com/railsware/mailtrap-nodejs",
"version": "3.2.0-beta.0",
"author": "Railsware Products Studio LLC",
"license": "MIT",
"dependencies": {

@@ -28,3 +14,5 @@ "axios": ">=0.27"

"@jest/globals": "^29.3.1",
"@types/jest": "^29.5.3",
"@types/node": "^18.15.11",
"@types/nodemailer": "^6.4.9",
"@typescript-eslint/eslint-plugin": "^5.57.1",

@@ -44,3 +32,29 @@ "@typescript-eslint/parser": "^5.57.1",

"typescript": "^5.0.3"
}
},
"engines": {
"node": ">=16.20.1",
"yarn": ">=1.22.17"
},
"files": [
"dist/**/*"
],
"license": "MIT",
"main": "dist/index.js",
"peerDependencies": {
"nodemailer": "^6.9.4"
},
"peerDependenciesMeta": {
"nodemailer": {
"optional": true
}
},
"repository": "https://github.com/railsware/mailtrap-nodejs",
"scripts": {
"lint": "yarn lint:eslint && yarn lint:tsc",
"lint:eslint": "yarn run eslint . --ext .js,.ts",
"lint:tsc": "tsc -p . --noEmit --incremental false",
"prepublish": "rm -rf dist && tsc --project tsconfig.build.json",
"test": "jest"
},
"types": "dist/index.d.ts"
}

@@ -30,11 +30,14 @@ ![TypeScript](https://badgen.net/badge/icon/TypeScript/?icon=typescript&label) [![test](https://github.com/railsware/mailtrap-nodejs/actions/workflows/test.yml/badge.svg)](https://github.com/railsware/mailtrap-nodejs/actions/workflows/test.yml)

```js
const { MailtrapClient } = require("mailtrap");
```ts
import { MailtrapClient } from "mailtrap"
// For this example to work, you need to set up a sending domain,
// and obtain a token that is authorized to send from the domain
const TOKEN = "your-api-token";
const SENDER_EMAIL = "sender@yourdomain.com";
const RECIPIENT_EMAIL = "recipient@email.com";
/**
* For this example to work, you need to set up a sending domain,
* and obtain a token that is authorized to send from the domain.
*/
const TOKEN = "<YOUR-TOKEN-HERE>";
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";
const client = new MailtrapClient({ token: TOKEN });

@@ -51,3 +54,4 @@

})
.then(console.log, console.error);
.then(console.log)
.catch(console.error);
```

@@ -57,13 +61,17 @@

```js
const fs = require("fs");
const path = require("path");
const { MailtrapClient } = require("mailtrap");
```ts
import fs from "node:fs"
import path from "node:path"
// For this example to work, you need to set up a sending domain,
// and obtain a token that is authorized to send from the domain
const TOKEN = "your-api-token";
const SENDER_EMAIL = "sender@yourdomain.com";
const RECIPIENT_EMAIL = "recipient@email.com";
import { MailtrapClient } from "mailtrap"
/**
* For this example to work, you need to set up a sending domain,
* and obtain a token that is authorized to send from the domain.
*/
const TOKEN = "<YOUR-TOKEN-HERE>";
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";
const client = new MailtrapClient({ token: TOKEN });

@@ -117,3 +125,4 @@

})
.then(console.log, console.error);
.then(console.log)
.catch(console.error)
```

@@ -123,18 +132,19 @@

```js
const { MailtrapClient } = require("mailtrap");
```ts
import { MailtrapClient } from "mailtrap"
// For this example to work, you need to set up a sending domain,
// and obtain a token that is authorized to send from the domain
const TOKEN = "your-api-token";
const SENDER_EMAIL = "sender@yourdomain.com";
const RECIPIENT_EMAIL = "recipient@email.com";
/**
* For this example to work, you need to set up a sending domain,
* and obtain a token that is authorized to send from the domain.
*/
const TOKEN = "<YOUR-TOKEN-HERE>";
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";
const client = new MailtrapClient({ token: TOKEN });
const sender = { name: "Mailtrap Test", email: SENDER_EMAIL };
client
.send({
from: sender,
from: { name: "Mailtrap Test", email: SENDER_EMAIL },
to: [{ email: RECIPIENT_EMAIL }],

@@ -146,5 +156,74 @@ template_uuid: "813e39db-c74a-4830-b037-0e6ba8b1fe88",

})
.then(console.log, console.error);
.then(console.log)
.catch(console.error);
```
### Nodemailer Transport
***[Nodemailer](https://www.npmjs.com/package/nodemailer) needed as a dependency.***
You can provide Mailtrap specific keys like `category`, `customVariables`, `templateUuid` and `templateVariables`.
```ts
import { readFileSync } from "fs";
import Nodemailer from "nodemailer";
import { MailtrapTransport } from "mailtrap"
/**
* For this example to work, you need to set up a sending domain,
* and obtain a token that is authorized to send from the domain.
*/
const TOKEN = "<YOUR-TOKEN-HERE>"
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";
const transport = Nodemailer.createTransport(MailtrapTransport({
token: TOKEN
}))
transport.sendMail({
text: "Welcome to Mailtrap Sending!",
to: {
address: RECIPIENT_EMAIL,
name: "John Doe"
},
from: {
address: SENDER_EMAIL,
name: "Mailtrap Test"
},
subject: "Hello from Mailtrap!",
html: `
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body style="font-family: sans-serif;">
<div style="display: block; margin: auto; max-width: 600px;" class="main">
<h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">Congrats for sending test email with Mailtrap!</h1>
<p>Inspect it using the tabs you see above and learn how this email can be improved.</p>
<img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;">
<p>Now send your email using our fake SMTP server and integration of your choice!</p>
<p>Good luck! Hope it works.</p>
</div>
<!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
<style>
.main { background-color: white; }
a:hover { border-left-width: 1em; min-height: 2em; }
</style>
</body>
</html>
`,
attachments: [
{
filename: "welcome.png",
content: readFileSync("./welcome.png"),
},
],
}).then(console.log)
.catch(console.error)
```
## Development

@@ -151,0 +230,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc