@joystick.js/node

email.send()

How to send emails in Joystick using the email.send() method.

To send an email, use the email.send() method, available via the named export email from @joystick.js/node.

Example Usage

/api/messages/setters.js

import joystick, { email } from '@joystick.js/node';

const setters = {
  send_message: {
    input: {
      user_id: {
        type: 'string',
        required: true,
      },
      message: {
        type: 'string', 
        required: true,
      },
    },
    set: async (input = {}) => {
      const message_id = joystick.id();

      await process.databases.mongodb.collection('messages').insertOne({
        _id: message_id,
        recipient: input?.user_id,
        message: input?.message,
      });

      const recipient_user = await process.databases.mongodb.collection('users').findOne({
        _id: input?.user_id,
      });

      await email.send({
        to: recipient_user?.emailAddress,
        from: 'notifications@app.com',
        subject: `You received a new message from ${context?.user?.username}!`,
        base: 'user_messages',
        template: 'new_message',
        preheader: `${context?.user?.username} says "${input?.message}"`,
        props: {
          message_id,
          sender: context?.user?.username,
        },
        attachments: [
          { filename: 'message.txt', content: input?.message },
        ],
      });
    },
  },
};

export default setters;

In the example above, we create a setter for sending messages between users. After saving the message to the database, we retrieve the recipient’s email and use email.send() to notify them.

The to, from, subject, and template fields are required when calling email.send().

API

Definition

email.send(options: object) => Promise

Parameters

options object required
The options object defining the email to send.
to string required
The recipient address for the email being sent.
from string required
The sender address for the email being sent.
subject string required
The subject for the email being sent.
base string
The name of the base template relative to the /email directory. The .html suffix is assumed.
template string required
The name of the email template relative to the /email directory. The .js suffix is assumed.
preheader string
Text shown as the preview line in a recipient’s inbox.
props object
Key/value pairs passed as props to the email template.
attachments array[object]
An array of attachment objects, following the Nodemailer attachments specification.