0.2.1 • Published 3 years ago

ds2-mailman v0.2.1

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
3 years ago

README DS2-MAILMAN

This is a REST API for sending emails by using templates. Mainly to be used in DS2. For information about each endpoint see each controller's documentation.

Usage

Email module

Add templates

For full description of how to add new email templates, see the [Templater] documentation.

Read Emails

To read emails you can use the following snippet:

import { ImapSimpleOptions } from 'imap-simple'
import { Condition, ImapClient, MailRouter } from './receiver'

const settings: ImapSimpleOptions = {
	imap: {
		user: process.env.MAIL_USER,
		password: process.env.MAIL_PW,
		host: process.env.MAIL_HOST,
		port: 993,
		tls: true,
		authTimeout: 3000,
		tlsOptions: {
			rejectUnauthorized: false,
		},
	},
}

const run = async () => {
	const client = new ImapClient(settings)

	const emails = await client.search([Condition.UNSEEN], {
		bodies: [''],
	})

	for (const email of emails) {
		console.log(email.subject)
	}
}

Send emails

To use the [Sender] module you can use the following snippet:

import { Sender } from './mail'
import { TemplateType } from '@/types'
import ical, { ICalAttendeeStatus, ICalAttendeeType } from 'ical-generator'

const run = async () => {
	// Create an (optional) calendar invitation:
	const cal = ical()
	cal.createEvent({
		description: 'Event description',
		summary: 'Event Title',
		end: '2021-05-13T11:58:16.966Z',
		start: '2021-05-13T13:58:16.966Z',
		location: 'https://www.youtube.com/',
		url: 'https://www.youtube.com/',
		categories: [{ name: 'someCategory' }],
		organizer: {
			name: 'Ankeborgs vårdcentral',
			email: 'info@minso.se',
		},
		attendees: [
			{
				type: ICalAttendeeType.INDIVIDUAL,
				email: 'robin@minso.se',
				name: 'mästaren',
				status: ICalAttendeeStatus.DELEGATED,
			},
		],
	})

	const sender = new Sender()

	// This will throw if you configured Sender wrong:
	await sender.verifyConnection()

	// Send the actual email:
	await sender.send(
		{
			from: '"Jesus" noreply@minso.se',
			sender: 'noreply@minso.se',
			replyTo: 'nihad@minso.se',
			to: 'tommy@minso.se',
			cc: ['robin.bauhn@minso.se', 'xxxxxxxx@minso.se'],
			subject: 'Ett nytt mail',
			attachments: [{ path: 'path/to/image.png' }],
			icalEvent: { content: cal.toString() },
		},
		{
			template: TemplateType.Test,
			values: { items: [{ name: 'kalle' }, { name: 'anna' }] },
		}
	)
}