@maevia/mailer v0.3.0
About
A configurable tool for generating email parameters with an entity based api.
How to use
First create a new
Mail()
instance, providing any default values as an object.Modify the instance properties directly to update the underlying data.
Call
getConfig()
on the instance to get the formatted mail parameters.- By default the parameters returned are in the format required by
nodemailer
. - To override this implementation pass a custom
formatOutput
handler.
- By default the parameters returned are in the format required by
Example:
const mail = new Mail();
mail.to = 'test@domain.com';
mail.subject = 'Welcome to test';
mail.text = 'Message content here.';
const nodemailerParams = await mail.getConfig();
Setting default values
Default values can be provided to the Mail
's constructor directly or passed to
createMailFactory
that returns a function that creates pre-loaded instances when called.
const createMail = createMailFactory({
from: 'test@domain.com',
});
const mail: Mail = createMail();
console.log(mail.from); // test@domain.com
Handling email addresses
Internally, all email address are converted into a MailTargetDto
instance which stores
both the email and target name.
When accessing the targets directly, an array of MailTargetDto
's will be returned. To have
greater control of the returned targets call the instances getTargets
.
When the parameters are generated, the address is returned as a string if no name was provided.
Remove targets by calling the instances removeTargets
function.
Notes
- Duplicates are removed on entry, keeping the most relevant.
- Inputs are not validated on entry.