Docs
const candymail = require('candymail')
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>",
"from": "[email protected]"
},
{
"trigger": "time",
"sendDelay": 3,
"subject": "w1e2",
"body": "Customizations are great",
"from": "[email protected]"
}
]
},
{
"name": "workflow2",
"description": "tell user about pro features 2",
"trigger_name": "proplan",
"emails": [
{
"trigger": "time",
"sendDelay": 1,
"subject": "w2e1",
"body": "Customizations are great",
"from": "[email protected]"
}
]
}
]
}
When initalizing Candymail, you need to provide:
- 1.
MAIL_USER
: Username for your SMTP auth - 2.
MAIL_PASSWORD
: Password for your SMTP auth - 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()
})
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]")
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]')
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}`)
})
Last modified 1yr ago