Socket
Book a DemoInstallSign in
Socket

@evokegroup/mailgun

Package Overview
Dependencies
Maintainers
4
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@evokegroup/mailgun

A lightweight library for sending email via Mailgun.

latest
Source
npmnpm
Version
2.0.2
Version published
Maintainers
4
Created
Source

@evokegroup/mailgun

A lightweight library for sending email via Mailgun.

Requires NodeJS 18+

Install

npm install @evokegroup/mailgun

MailgunClient

PropertyTypeDefaultDescription
apiKeystringThe Mailgun API key
domainstringThe sending domain
versionstringv3The API version

constructor()

ParameterTypeDefaultDescription
apiKeystringThe Mailgun API key
domainstringThe sending domain
versionstringv3The API version
import { MailgunClient } from '@evokegroup/mailgun';

const mg = new MailgunClient({ apiKey: '*****', domain: 'my.domain.com' });

Methods

sendAPI()

Sends email via the Mailgun API

ParameterTypeDefaultDescription
fromstring ¦ IMailAddress ¦ MailAddress
to(string ¦ IMailAddress ¦ MailAddress)[]
cc(string ¦ IMailAddress ¦ MailAddress)[]
bcc(string ¦ IMailAddress ¦ MailAddress)[]
subjectstring
htmlstring
textstring
testmodebooleanfalseSends the request to the Mailgun API but the message will not be sent.
argsobject{}Additional API parameters.

returns Promise<IWebResponse>

import { MailgunClient } from '@evokegroup/mailgun';

const mg = new MailgunClient({ apiKey: '*****', domain: 'my.domain.com' });

mg.sendAPI({
  to: ['john.doe@domain.com'],
  from: { name: 'First Last', address: 'first.last@domain.com' },
  subject: 'Hello World!',
  html: '<!DOCTYPE html>\r\n<html><head><title>World</title></head><body><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td><table cellpadding="0" cellspacing="0" border="0" width="450" align="center" bgcolor="#cccccc"><tr><td>HTML - Hello World!</td></tr></table></td></tr></table></body></html>',
  text: 'TEXT - Hello World!'
})
  .then((response) => {
    // do something
  })
  .catch((ex) => {
    // handle error
  });

sendMIME()

Sends mail via the Mailgun API MIME method.

ParameterTypeDefaultDescription
messageMimeMessage ¦ IMailgunMimeMessageThe MIME message
optsobject{}Additional options
opts.forceMultipartAlternativebooleanfalseForce the MIME message to be created using multipart/alternative
opts.testmodebooleanfalseSends the request to the Mailgun API but the message will not be sent.
opts.argsobject{}Additional API parameters.

returns Promise<IWebResponse>

Example using @evokegroup/mime

import { MailgunClient } from '@evokegroup/mailgun';
import { MimeMessage } from "@evokegroup/mime";

const mg = new MailgunClient({ apiKey: '*****', domain: 'my.domain.com' });

const message = new MimeMessage();
message.from = { name: 'First Last', address: 'first.last@domain.com' };
message.subject = 'Hello World!';
message.to.add('john.doe@domain.com');
message.setHTML('<!DOCTYPE html><html><head><title>World</title></head><body><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td><table cellpadding="0" cellspacing="0" border="0" width="450" align="center" bgcolor="#cccccc"><tr><td>HTML - Hello World!</td></tr></table></td></tr></table></body></html>');
message.setText('TEXT - Hello World!');

mg.sendMIME(message)
  .then((response) => {
    // do something
  })
  .catch((ex) => {
    // handle error
  });

Example using another means on MIME message creation

import { MailgunClient } from '@evokegroup/mailgun';

function createMimeMessage() {
  // Create a MIME message by some means and return it as a string
}

const mg = new MailgunClient({ apiKey: '*****', domain: 'my.domain.com' });

mg.sendMIME({
  to: ['john.doe@domain.com'], // mailgun requires the recipient list even if it's already in the MIME message
  message: createMimeMessage()
})
  .then((response) => {
    // do something
  })
  .catch((ex) => {
    // handle error
  });

static parseMessage()

Parses an incomming message.

ParameterTypeDefaultDescription
messagestring
allbooleanfalseParse all the data

returns Record<string, string> ¦ IMailgunMessage

IMailgunMessage

PropertyType
tostring ¦ IMailAddress
fromstring ¦ IMailAddress
subjectstring
htmlstring
textstring

IMailgunMimeMessage

PropertyType
to(string ¦ IMailAddress ¦ MailAddress)[]
messagestring

FAQs

Package last updated on 22 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts