Skip to main content

Mail

Sending emails is a common task for web applications. In this section, we will learn how to send emails using the inbuilt mail module of Framework X.

info

Framework X uses nodemailer for mailing with mjml for templating.

Installation

If not already installed, please install the dependencies below:

pnpm install nodemailer mjml

Usage

To send an email, you need to update .env file with the following variables:

MAILER_HOST="sandbox.smtp.mailtrap.io"
# 25 or 465 or 587 or 2525
MAILER_PORT=2525
MAILER_USER=
MAILER_PASS=
# true for 465, false for other ports
MAILER_SECURE=false
MAILER_FROM='"Fred Foo 👻" <[email protected]>'
info

Please note that the MAILER_FROM variable is used as the default sender for all emails sent by the application. You can override this by passing the from option to the sendMail method. See the example below.

Create Mail Template

You can create a new mail template by running the command below:

node x build:mail <template-name>

Example

node x build:mail order_created

This will create a new file order_created.mail.mjml in the mails directory.

Send Mail

const { sendMail } = require("@3rdplanet/x-core/mailer")

let mailOption = {
to: ["[email protected]", "[email protected]"],
subject: "Hello there"
}

sendMail("order_created", mailOption).then(() => {})

Send Mail with Dynamic Data

const {sendMail} = require("@3rdplanet/x-core/mailer")

let mailOption = {
to: ["[email protected]", "[email protected]"],
subject: "Hello there",
replacer : {
userName : "John Doe"
}
}

sendMail('order_created', mailOption).then(() => {})

// in your mjml file
<mj-text>Hello {{userName}}</mj-text>

Support Framework X

  • You can also support the project by following the organization and Star ⭐ the project on GitHub
  • Contribute by submitting issues and pull requests
  • Share the project with your friends and colleagues, any and all support is appreciated. 🙏
  • If you find this Framework useful, We will always appreciate a strong cup of coffee.