npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@acryps/mail

v1.1.2

Published

Component based queued mail handler

Downloads

8

Readme

acryps mail

Component based queued mail handler

This package uses nodemailer.

When initializing a mailer you have to define a bunch of things right away.

Mail | TStoredMail, TStoredAddress :-- | :-- Sender address | Email address to send from Transport configuration | Nodemailer config for the mail transporter (It has no typings because nodemailer doesn't provide them in the first place) Convert to sendable mail | Convert TStoredMail into a sendable mail Create | Create TStoredMail and TStoredAddresses Mark as sent | Mark TStoredMail as sent Unsent queue | Optional initial unsent TStoredMail queue (fetch from db)

These are the minimum requirements for a functional mailer. Optionally DKIM can be added to sign the mails in the headers and send error can be handled.

The mailer also supports localization via polyfills. Defining <div>{'Hello'.german('Hallo')}</div> in the component automatically translates according to the preferred language passed in the send method. Currently only german translation is supported and the tag to pass into the send method is hardcoded to de. This obviously isn't convenient and will be fixed soon.

Example usage:

Setup Mailer

const mailer = new Mailer<Mail, Address>(
	'[email protected]', 

	// Transport configuration
	{
		host: 'smtp.host.com',
		port: 587,
		secure: false,
		auth: {
			user: '[email protected]',
			pass: 'securepassword1234'
		},
		tls: {
			rejectUnauthorized: false
		}
	}, 

	// Convert to sendable mail
	async model => {
		const recipients: string[] = await getRecipientEmails(model);

		return {
			subject: model.subject,
			text: model.text,
			html: model.html,
			recipients
		}
	},

	// Create
	async (addresses, mail) => {
		const model = new Mail();

		model.created = new Date();
		model.subject = mail.subject;
		model.text = mail.text;
		model.html = mail.html;

		await model.create();

		for (const address of addresses) {
			const mailAddress = new MailAddress();

			mailAddress.address = address;
			mailAddress.mail = model;

			await mailAddress.create();
		}

		return model;
	},
	
	// Mark as sent
	async model => {
		model.sent = new Date();
	
		await model.update();
	},

	// Unsent queue
	await db.mail.where(mail => mail.sent == null).toArray()
);

mailer.addDKIM(process.env.MAIL_DOMAIN, process.env.MAIL_DKIM_KEY);
mailer.onSendError = async (model, mail, error) => console.log(`Mail from ${mailer.sender} to ${mail.recipients} failed to send (id: ${model.id}):`, error);

Creating Mail Component

export class ExampleMail extends MailComponent {
	constructor(
		private fullname: string
	) {}

	render(child?: MailComponent): MailNode {
		return <html>
			<head>
				<meta charset="UTF-8" />
				<meta name="viewport" content="width=device-width,initial-scale=1" />
				<meta name="x-apple-disable-message-reformatting" />
			</head>
			<head>
				<style>...</style>
			</head>
			<body>
				<div class="line">Hello {this.fullname}</div>

				{child}

				<div class="line">Greetings</div>

				<div class="address">
					<div class="address-line">Name</div>
					<div class="address-line">Street</div>
					<div class="address-line">Place City</div>
				</div>

				<a href="mailto:[email protected]">[email protected]</a>
			</body>
			</html>;
	}
}

Sending Mail

// The passed address is of type TStoredAddress defined previously for the mailer.
mailer.send(new ExampleMail('Foo Bar'), address, 'de');

Sponsoring and support

This project is sponsored and supported by ACRYPS.