Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/acryps/mail
Basic tsx mailer
https://github.com/acryps/mail
Last synced: 5 days ago
JSON representation
Basic tsx mailer
- Host: GitHub
- URL: https://github.com/acryps/mail
- Owner: acryps
- Created: 2023-07-10T09:02:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-31T08:25:44.000Z (over 1 year ago)
- Last Synced: 2023-10-05T15:12:03.299Z (about 1 year ago)
- Language: TypeScript
- Size: 21.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
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).