Links

Docs

Import the library

const candymail = require('candymail')

Creating your first workflow

Workflow is a sequence of emails with a trigger name. You can create any number of workflows.
Here is an example candymail.automation.json :
{
"workflows": [
{
"name": "workflow1",
"description": "tell user about pro features",
"trigger_name": "proplan",
"emails": [
{
"trigger": "time",
"sendDelay": 0,
"subject": "w1e1",
"body": "<h1>Send automated messages with Candymail</h1><p>Now with HTML support</p><a href='https://saasbase.dev/candymail'>Learn more here</a>",
},
{
"trigger": "time",
"sendDelay": 3,
"subject": "w1e2",
"body": "Customizations are great",
}
]
},
{
"name": "workflow2",
"description": "tell user about pro features 2",
"trigger_name": "proplan",
"emails": [
{
"trigger": "time",
"sendDelay": 1,
"subject": "w2e1",
"body": "Customizations are great",
}
]
}
]
}

Initialize setup

When initalizing Candymail, you need to provide:
  1. 1.
    MAIL_USER: Username for your SMTP auth
  2. 2.
    MAIL_PASSWORD: Password for your SMTP auth
  3. 3.
    HOSTING_URL: The domain the app is hosted on. Typically you would want this to be - https://myapp.com but for development, you could do: http://localhost:3000 as well.
candymail
.init(automation.workflows, {
mail: {
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASSWORD,
},
tls: {
rejectUnauthorized: true,
},
},
hosting: { url: process.env.HOSTING_URL },
db: { reset: true },
debug: { trace: true },
})
.then(() => {
candymail.start()
})

Trigger a workflow

When an event occurs, you can trigger a workflow to start the email sequence like below. The email is the recipient's email to whom the emails will be sent.
candymail.runWorkflow('workflow1', "[email protected]")

Unsubscribe

Manually unsubscribe a user by email

This will immediately unsubscribe the user and they will not receive any more messages, even the ones waiting in the queue.
candymail.unsubscribeUser('[email protected]')

Unsubscribe via endpoint

Every email is sent out with an /[email protected] link. If you're running an API server, you can unsubscribe the user automatically like so:
app.get('/unsubscribe', (req, res) => {
const { email } = req.query
candymail.unsubscribeUser(email)
res.send(`Sent a unsubscribe request for ${email}`)
})