From 53b601e150bec50d05eb55d59df88a74cfbe9f35 Mon Sep 17 00:00:00 2001 From: diego tonini Date: Fri, 11 Aug 2023 11:14:19 +0200 Subject: [PATCH] feat: extends send test options to all nodemailer transporter --- README.md | 74 +++++++++++++++++++++++++--------- playground/marilena.config.mjs | 23 ++++++----- src/lib/send-test-email.ts | 19 ++------- src/types.ts | 12 ++---- 4 files changed, 77 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 8bdc78b..3a0805c 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ Under the hood a default configuration will be loaded but a file `marilena.confi | mjmlParsingOptions | | options passed to mjml render. See: [mjml options](https://www.npmjs.com/package/mjml) | | htmlVersion | | function of type `(emailName: string, locale: string) => string`. If set, this function allow to customize the output html filename. The function must return file name `es: ${emailName}-${locale}.html` | index.html | | textVersion | | function of type `(emailName: string, locale: string) => string`. If set, this function allow to generate text version of email stripping all html. The function must return file name `es: ${emailName}-${locale}-text-version.txt` | -| sendTestOptions | | option in case you want to send the email to some account for testing. Setting this should add `send email` button during development: Read below for some use cases | +| sendTestOptions | | option in case you want to send the email to some account for testing. Setting this should add `send-email` button during development: Read below for some use cases | ## About templateOptions @@ -162,26 +162,63 @@ templateOptions: { --- -## About templateOptions +## About sendTestOptions + +This option provides a fast way to test email sending an email to real account. You shoud pass also `createTransport` function that return a `Transporter`. +See [nodemailer tutorial](https://nodemailer.com/smtp/) -This option provides a fast way to test email sending an email to real account. For now this is possible using `aws-ses` provider. In this case you shoul have a valid Amazon SES account. Setting this options will add a small form in the email page development. Example: +Example `marilena.config.mjs` to work with Aws SES: ```js -sendTestOptions: { - provider: "aws-ses", // only aws-ses is supported - to: "diego.tonini93@gmail.com", // this is only a default. During development you can set different address - from: "noreply@custom_domain.com", // only valid and registered alias are working with your associated Aws Ses accout - // this field must return an object of type aws.SES. Below is only a basic working example - ses: () => - new aws.SES({ - apiVersion: "2010-12-01", // should be the same - region: "us-east-1", - credentials: { - accessKeyId: "...", - secretAccessKey: "...", - }, - }), +import * as aws from "@aws-sdk/client-ses"; +import nodemailer from "nodemailer"; + +export default { + ...config, + sendTestOptions: { + to: "diego.tonini93@gmail.com", + from: "noreply@custom-domain.com", // this is not random email, but should be registered in you provider + createTransport: () => + nodemailer.createTransport({ + SES: { + ses: new aws.SES({ + apiVersion: "2010-12-01", + region: "us-east-1", + credentials: { + accessKeyId: "...", + secretAccessKey: "...", + }, + }), + aws, + }, + }), }, +}; +``` + +Example `marilena.config.mjs` to work with forwardemail or other custom setting; + +```js +import nodemailer from "nodemailer"; + +export default { + ...config, + sendTestOptions: { + to: "diego.tonini93@gmail.com", + from: "noreply@custom-domain.com", // this is not random email, but should be registered in you provider + createTransport: () => + nodemailer.createTransport({ + host: "smtp.forwardemail.net", + port: 465, + secure: true, + auth: { + // TODO: replace `user` and `pass` values from + user: "REPLACE-WITH-YOUR-ALIAS@YOURDOMAIN.COM", + pass: "REPLACE-WITH-YOUR-GENERATED-PASSWORD", + }, + }), + }, +}; ``` ## Use css @@ -209,13 +246,12 @@ If you want to add a css file import in `mj-include` tag. Path start from root d - [x] load varibles from yaml/json format - [x] load common variables - [x] pass option to MJML render -- [x] easy way to send a real email (AWS Ses) +- [x] send test email (nodemailer, aws ses) ## 🏗️ Roadmap (PRs are welcome 😀) - [ ] liquid, ejs, nunjucks, mustache, dot - [ ] config in typescript -- [ ] extends send test email to custom provider - [ ] fast-refresh on config change - [ ] snaphost test for each email out of the box - [ ] refactor to esm instead common js diff --git a/playground/marilena.config.mjs b/playground/marilena.config.mjs index 7b2e99a..ec8bbb6 100644 --- a/playground/marilena.config.mjs +++ b/playground/marilena.config.mjs @@ -1,5 +1,6 @@ import path from "path"; import * as aws from "@aws-sdk/client-ses"; +import nodemailer from "nodemailer"; /** * this is of config file. Is used only inside playground */ @@ -20,16 +21,20 @@ export default { keepComments: false, }, sendTestOptions: { - provider: "aws-ses", to: "diego.tonini93@gmail.com", - from: "noreply@custom_domain.com", // only valid and registered alias are working with SES - ses: () => - new aws.SES({ - apiVersion: "2010-12-01", - region: "us-east-1", - credentials: { - accessKeyId: "...", - secretAccessKey: "...", + from: "noreply@conio.com", // only valid and registered alias are working with SES + createTransport: () => + nodemailer.createTransport({ + SES: { + ses: new aws.SES({ + apiVersion: "2010-12-01", + region: "us-east-1", + credentials: { + accessKeyId: "...", + secretAccessKey: "...", + }, + }), + aws, }, }), }, diff --git a/src/lib/send-test-email.ts b/src/lib/send-test-email.ts index e6186f0..ab76137 100644 --- a/src/lib/send-test-email.ts +++ b/src/lib/send-test-email.ts @@ -1,22 +1,11 @@ -import nodemailer from "nodemailer"; -import * as aws from "@aws-sdk/client-ses"; import { SendTestOptions } from "../types"; export function sendTestEmail(options: SendTestOptions, html: string) { - if (options.provider !== "aws-ses") { - throw new Error( - `provider ${options.provider} not supported for send a test email`, - ); - } + const { from, to, createTransport } = options; - // create Nodemailer SES transporter - let transporter = nodemailer.createTransport({ - SES: { ses: options.ses(), aws }, - }); - - return transporter.sendMail({ - from: options.from, - to: options.to, + return createTransport().sendMail({ + from, + to, subject: "Test", html, }); diff --git a/src/types.ts b/src/types.ts index fff85c6..f974d4a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ import { MJMLParsingOptions } from "mjml-core"; -import * as aws from "@aws-sdk/client-ses"; +import { Transporter } from "nodemailer"; export interface UserConfig { inputFolder?: string; // default input outputFolder?: string; // default output @@ -21,12 +21,8 @@ export type SupportedEngine = "eta" | "handlebars"; export type CoreConfig = UserConfig & Required>; -type BaseSendTestOptions = { +export interface SendTestOptions { from: string; to: string; -}; - -export type SendTestOptions = BaseSendTestOptions & { - provider: "aws-ses"; - ses: () => aws.SES; -}; + createTransport: () => Transporter; +}