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

feat: change aws send test option with more customizable one for all providers (ses included) #48

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 55 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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: "[email protected]", // 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: "[email protected]",
from: "[email protected]", // 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: "[email protected]",
from: "[email protected]", // 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 <https://forwardemail.net>
user: "[email protected]",
pass: "REPLACE-WITH-YOUR-GENERATED-PASSWORD",
},
}),
},
};
```

## Use css
Expand Down Expand Up @@ -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
23 changes: 14 additions & 9 deletions playground/marilena.config.mjs
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -20,16 +21,20 @@ export default {
keepComments: false,
},
sendTestOptions: {
provider: "aws-ses",
to: "[email protected]",
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: "[email protected]", // 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,
},
}),
},
Expand Down
19 changes: 4 additions & 15 deletions src/lib/send-test-email.ts
Original file line number Diff line number Diff line change
@@ -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,
});
Expand Down
12 changes: 4 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,12 +21,8 @@ export type SupportedEngine = "eta" | "handlebars";
export type CoreConfig = UserConfig &
Required<Pick<UserConfig, "inputFolder" | "outputFolder" | "locales">>;

type BaseSendTestOptions = {
export interface SendTestOptions {
from: string;
to: string;
};

export type SendTestOptions = BaseSendTestOptions & {
provider: "aws-ses";
ses: () => aws.SES;
};
createTransport: () => Transporter;
}