Back to jobs

Email drip campaign using Resend

Trigger: eventTrigger

Sends four emails in a drip campaign over 30 days, triggered by an event. Emails built using React & Typescript

Integrations:

/src/resendDripCampaign

Marketing

1
import { TriggerClient, TriggerContext, eventTrigger } from "@trigger.dev/sdk";
2
import {
3
FirstEmail,
4
MonthLaterEmail,
5
SecondEmail,
6
ThirdEmail,
7
} from "./mocks/reactEmails";
8
import { Resend } from "@trigger.dev/resend";
9
import { z } from "zod";
10
11
const client = new TriggerClient({ id: "jobs-showcase" });
12
13
const resend = new Resend({
14
id: "resend",
15
apiKey: process.env.RESEND_API_KEY!,
16
});
17
18
// This job sends a drip campaign using Resend
19
client.defineJob({
20
id: "resend-drip-campaign",
21
name: "Resend: email drip campaign",
22
version: "1.0.0",
23
trigger: eventTrigger({
24
name: "send.drip.campaign",
25
schema: z.object({
26
to: z.string(),
27
// The 'from' email address must be a verified domain in your Resend account.
28
from: z.string(),
29
name: z.string(),
30
}),
31
}),
32
integrations: {
33
resend,
34
},
35
run: async (payload, io, ctx) => {
36
const email1 = {
37
text: `Hi there, welcome to our community! This is the first email we send you to help you get started.`,
38
};
39
40
// Email 1, triggered by an event
41
await io.resend.sendEmail("email-1", {
42
to: payload.to,
43
from: payload.from,
44
subject: `Thanks for joining Acme Inc`,
45
text: email1.text,
46
react: <FirstEmail name={payload.name} text={email1.text} />,
47
});
48
49
await io.wait("wait-1-day", delay(60 * 60 * 24 * 1, ctx));
50
51
const email2 = {
52
text: `Hi there, welcome to our community! This is the second email we send you to help you get started.`,
53
};
54
55
// Email 2, triggered after a day
56
await io.resend.sendEmail("email-2", {
57
to: payload.to,
58
from: payload.from,
59
subject: `Here are some tips to get started`,
60
text: email2.text,
61
react: <SecondEmail name={payload.name} text={email2.text} />,
62
});
63
64
await io.wait("wait-4-days", delay(60 * 60 * 24 * 4, ctx));
65
66
const email3 = {
67
text: `Hi there, welcome to our community! This is the third email we send you to help you get started.`,
68
};
69
70
// Email 3, triggered after 5 days
71
await io.resend.sendEmail("email-3", {
72
to: payload.to,
73
from: payload.from,
74
subject: `Do you have any questions?`,
75
text: email3.text,
76
//email 1, triggered by an event
77
react: <ThirdEmail name={payload.name} text={email3.text} />,
78
});
79
80
await io.wait("wait-26-days", delay(60 * 60 * 24 * 26, ctx));
81
82
const email4 = {
83
text: `This is the fourth email designed to re-engage your users after a month.`,
84
};
85
86
// Email 4, triggered after 30 days
87
await io.resend.sendEmail("email-4", {
88
to: payload.to,
89
from: payload.from,
90
subject: `How are you getting on with Acme Inc.?`,
91
text: email4.text,
92
//email 1, triggered by an event
93
react: <MonthLaterEmail name={payload.name} text={email4.text} />,
94
});
95
},
96
});
97
98
function delay(seconds: number, context: TriggerContext) {
99
if (context.environment.type === "DEVELOPMENT" || context.run.isTest) {
100
return 10;
101
}
102
return seconds;
103
}
104
105
// These lines can be removed if you don't want to use express
106
import { createExpressServer } from "@trigger.dev/express";
107
createExpressServer(client);