Gathering detailed insights and metrics for postal-js
Gathering detailed insights and metrics for postal-js
Gathering detailed insights and metrics for postal-js
Gathering detailed insights and metrics for postal-js
postal-codes-js
Postal Codes
alive-postal-code
Improved version of postal-code-js
barcode-js
A node addon to perform barcode recognition and analysis
nepal-js
A JavaScript Object for all the location in Nepal. It contains all the regions,zones,provinces,districts,city and postal/zip codes
npm install postal-js
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
5
A simple libray for sending emails with the postal api or accessing the informations of a message.
Install the library with your favourite package manager:
1npm install postal-js 2pnpm add postal-js 3bun add postal-js
Then import it in your code and fill out the required informations to connect to the api:
1import Postal from "postal-js"; 2 3export const postal = new Postal({ 4 key: "YOUR_API_KEY", 5 url: "https://postal.example.com", 6});
You can use the exported class in your project to send emails.
To send an email, you need to provide the following information:
to
: The email address of the recipient.from
: The email address of the sender.subject
: The subject of the email.html_body
or plain_body
: The HTML body of the email.There are also some optional parameters you can provide:
reply_to
: The email address to use as the reply-to address.attachments
: An array of attachments to include in the email.headers
: An object of headers to include in the email.tag
sender
bounce
cc
bcc
1const msg = await postal.sendMessage({ 2 to: "user@example.com", 3 from: "me@example.com", 4 subject: "Hello", 5 html_body: "<h1>Hello World</h1>", 6});
If you ran this code, you will receive a response like this:
1console.log(msg); /* => { 2 success: boolean, 3 flags: Record<string, any>, 4 time: number 5 status: "success" | "error", 6 data: { 7 // error fields 8 message?: string, 9 code?: string, 10 11 // success fields 12 messages?: Record<string, { 13 id: Number, 14 token: string 15 }>, 16 messages_id?: string, 17 }, 18 error: string 19} */
If you want to send a raw email, an RFC2822 formatted email, encoded as a base64 string is also possible.
[!TIP] When you should using an raw email? Raw emails are useful when you want to send emails that are not RFC2822 formatted. For example, if you want to send an email with a subject that contains a colon, you will need to use a raw email.
This is an example for a RFC2822 formatted email:
From: sender@example.com
To: recipient@example.com
Subject: Hey!
Date: Mon, 28 Aug 2023 12:34:56 +0000
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
And this is an example for a base64 encoded RFC2822 formatted email:
RnJvbTogc2VuZGVyQGV4YW1wbGUuY29tClRvOiByZWNpcGllbnRAZXhhbXBsZS5jb20KU3ViamVjdDogSGV5IQpEYXRlOiBNb24sIDI4IEF1ZyAyMDIzIDEyOjM0OjU2ICswMDAwCk1JTUUtVmVyc2lvbjogMS4wCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD1VVEYtOA==
You can give following informations to this function:
rcpt_to
: The email address of the recipient.mail_from
the adress of the sender.bounce
a boolean indicating if the email should be bounced.data
the base64 encoded RFC2822 formatted email.You are now able to send your email with this function:
1const msg = await postal.sendRawEmail({ 2 to: "user@example.com", 3 from: "me@example.com", 4 subject: "Hey!", 5 raw_email: 6 "RnJvbTogc2VuZGVyQGV4YW1wbGUuY29tClRvOiByZWNpcGllbnRAZXhhbXBsZS5jb20KU3ViamVjdDogSGV5IQpEYXRlOiBNb24sIDI4IEF1ZyAyMDIzIDEyOjM0OjU2ICswMDAwCk1JTUUtVmVyc2lvbjogMS4wCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD1VVEYtOA==", 7});
The response is the same as the previous example.
You can also get the details of an message with this library. This is useful if you want to access certain informations about the message, like the body or subject.
1const msg = await postal.getMessageDetails({ 2 id: "MESSAGE_ID", 3});
You can get your message id from the response of the sendMessage
or sendRawEmail
function. You can find the id in the id
field of the response.
This is an example to get the id of a message:
1console.log(msg); /* => { 2 success: boolean, 3 flags: Record<string, any>, 4 time: number 5 status: "success" | "error", 6 data: { 7 // error fields 8 message?: string, 9 code?: string, 10 11 // success fields 12 messages?: Record<string, { 13 id: Number, <== This is the id of the message 14 token: string 15 }>, 16 messages_id?: string, <== NOT THE ID !!! 17 }, 18 error: string 19} */
You can also select what informations you want to receive from this function. For example, if you only want to get the subject of the message, you can do this:
1const msg = await postal.getMessageDetails({ 2 id: "MESSAGE_ID", 3 expansions: ["subject"], 4});
These are all the possible expansions:
"status" | "details" | "inspection" | "plain_body" | "html_body" | "attachments" | "headers" | "raw_message" | "activity_entries"
If you want to get all of the informations you can simply do this:
1const msg = await postal.getMessageDetails({ 2 id: "MESSAGE_ID", 3 expansions: true, 4});
A response will look like this:
1console.log(msg); /* => { 2 success: boolean, 3 flags: Record<string, any>, 4 time: number 5 status: "success" | "error", 6 data: { 7 id: number, 8 9 // error fields 10 message?: string, 11 code?: string, 12 13 // fields which are only avaiable is the status is success 14 status?: { 15 status: string; 16 last_delivery_attempt: number; 17 held: boolean; 18 hold_expiry: number | null; 19 }; 20 details?: { 21 rcpt_to: string; 22 mail_from: string; 23 subject: string; 24 message_id: string; 25 timestamp: number; 26 direction: string; 27 size: string; 28 bounce: boolean; 29 bounce_for_id: number; 30 tag: string | null; 31 received_with_ssl: boolean; 32 }; 33 inspection?: { 34 inspected: boolean; 35 spam: boolean; 36 spam_score: number; 37 threat: boolean; 38 threat_details: string | null; 39 }; 40 plain_body?: string; 41 html_body?: string | null; 42 attachments?: any[]; 43 headers?: { 44 received: string[]; 45 date: string[]; 46 from: string[]; 47 to: string[]; 48 "message-id": string[]; 49 subject: string[]; 50 "mime-version": string[]; 51 "content-type": string[]; 52 "content-transfer-encoding": string[]; 53 "dkim-signature": string[]; 54 "x-postal-msgid": string[]; 55 }; 56 raw_message?: string; 57 activity_entries?: { 58 loads: any[]; 59 clicks: any[]; 60 }; 61 }, 62 error: string
Based on the selection of the expansions, you will receive different fields in the response. This example of an response contains all fields.
You can also receive the deliveries of a message. This is useful if you want to know when a message was delivered.
1const msg = await postal.getMessageDeliveries({ 2 id: "MESSAGE_ID", 3});
A response will look like this:
1console.log(msg); /* => { 2 success: boolean, 3 flags: Record<string, any>, 4 time: number 5 status: "success" | "error", 6 data: { 7 id: number; 8 9 // error fields 10 message?: string, 11 code?: string, 12 13 // fields which are only avaiable is the status is success 14 status?: string; 15 details?: string; 16 output?: string; 17 sent_with_ssl?: boolean; 18 log_id?: string; 19 time?: number; 20 timestamp?: number; 21 }[], 22 error: string 23}
Please note that the data
field is an array of objects. Each object represents a delivery of the message.
The Project is licensed under the MIT License.
No vulnerabilities found.
No security vulnerabilities found.