Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snippet: Example e-mail sending class #4

Open
trasherdk opened this issue May 1, 2022 · 2 comments
Open

Snippet: Example e-mail sending class #4

trasherdk opened this issue May 1, 2022 · 2 comments

Comments

@trasherdk
Copy link
Owner

Code to test relaying mass email through primary mail server.

import type { SMTPConnectionOptions } from 'emailjs';
import { Message, SMTPClient } from 'emailjs';
import PQueue from 'p-queue';

export type AddressList = {
    from: string;
    to: string[];
};

/** Internal configuration used by emailjs to send mail, see https://www.npmjs.com/package/emailjs for all options. */
export type EmailConnectionConfig = Partial<SMTPConnectionOptions>;

export default class Emailer {
    /** stop sending mail after this many items are in the queue */
    private readonly mailQueueMaxSize = 512;

    /** The minimum amount of time between sending each email. */
    private readonly globalMailThrottle = 750;

    // throttle mail sending using a promise queue
    private readonly mailQueue = new PQueue({ concurrency: 1, interval: this.globalMailThrottle, intervalCap: 1 });

    private client: SMTPClient;

    constructor(options: EmailConnectionConfig) {
        this.client = new SMTPClient(options);
    }

    public async send(addresses: AddressList, subject: string, text: string) {
        if (!addresses) {
            console.warn(`Not sending email. Address list looks falsey.`);
            return;
        }

        const from = addresses.from;

        if (!from) {
            console.warn(`Not sending email. No from address is configured.`);
            return;
        }

        if (addresses.to.length === 0) {
            console.warn(`Not sending email. No addresses are configured to send.`);
            return;
        }

        const to = addresses.to.join(', ');
        if (this.mailQueue.size >= this.mailQueueMaxSize) {
            console.error(
                `Dropping outgoing email. There are currently too many items in the email queue. (Queue size= ${this.mailQueue.size}, maximum=${this.mailQueueMaxSize})`
            );
            return;
        }

        await new Promise<void>(resolve => {
            const mailTask = async () => {
                try {
                    console.log(`Sending email to ${to}...`);
                    await this.client.sendAsync(
                        new Message({
                            from,
                            to,
                            subject,
                            text,
                            'content-type': text.startsWith('<!DOCTYPE html') ? 'text/html; charset="UTF-8"' : undefined,
                        })
                    );
                    console.log(`Sending email to ${to}  [SENT]`);
                    console.log(`There are`, this.mailQueue.size, 'items left in the mail queue');
                    resolve();
                } catch (err) {
                    console.error('Error sending email!', `(Subject: "${subject}")`);
                    console.error(err);

                    console.log('Putting this email to the back of the send queue...');
                    void this.mailQueue.add(mailTask);
                }
            };
            void this.mailQueue.add(mailTask);
        });
    }
}

Source: eleith#315 (comment)

@ecker00
Copy link

ecker00 commented Dec 5, 2022

This seems like quite a nice wrapper, maybe a good example implementation to include in the main repo?

@trasherdk
Copy link
Owner Author

@ecker00 I got the example from the main repo, linked abowe as Source eleith#315 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants