Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/acryps/mail

Basic tsx mailer
https://github.com/acryps/mail

Last synced: 5 days ago
JSON representation

Basic tsx mailer

Awesome Lists containing this project

README

        

# acryps mail
Component based queued mail handler

> This package uses [nodemailer](https://npmjs.com/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 `

{'Hello'.german('Hallo')}
` 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(
'[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






...


Hello {this.fullname}

{child}

Greetings


Name

Street

Place City

[email protected]

;
}
}
```

### 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](https://acryps.com).