Lesson Description

The "Triggering Emails" Lesson is part of the full, Build a Fullstack Next.js App, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates connecting page view milestones to sending celebration emails with Resend, scheduling them in the background. He covers setting thresholds, handling promises, testing delivery, and briefly discusses email deliverability and IP reputation.

Preview

Transcript from the "Triggering Emails" Lesson

[00:00:00]
>> Brian Holt: Obviously, you'd like a more interesting email than this. This is probably not going to get you much engagement and probably just going to get you blocked, but this is not a marketing class and you shouldn't take marketing from me. So let's go make this work from adding it to page views so that we can actually send an email. We are going to go to app, actions, page views, and here up at the top, we're going to say import send celebration email from @email/celebration email.

[00:00:38]
You're going to decide all the milestones that you want to send an email at, so I'm going to say like 10. Milestones equals 10, 50, 100, and then I did 10,000. So when someone gets 10 emails or 10 page views, they get an email when they get 50, 100, so on. Okay, under new value declaration, so here we're going to say if milestones includes new value, then send celebration email with article ID.

[00:01:32]
And we have to send them a new value, but new value is probably going to come out, does it come out as a number? Oh, it does. So I had the unary operator here in my notes, but I don't think you actually need it, because new value actually will end up coming back from Redis as a number. Now this is something to await, right? It's a promise. But we don't need to wait on this.

[00:02:02]
This is all just getting kind of scheduled in the background. So this doesn't need a plus either, so in that case it doesn't matter. It's already there. We'll leave it. So technically what we could do here is we could say await celebration email. But because this is going to happen in the background, we don't actually really care, we can just say, blah, and this will schedule it, this will still happen, right, providing your program doesn't crash or end or something like that, and it doesn't block this, which is nice.

[00:02:52]
Okay. So let's go take a look. We're going to go to our app. Is my app still running? My app is not running. Npm run dev. I'm going to open my app here, localhost 3000, and let's find one from test user. This unknown here would be for test user because I haven't actually set up their name. This has two page views. Let's open my Resend dashboard as well. Resend.

[00:03:34]
All right, so we should see one here above this that says, you know, less than four days ago, once this gets to 10. By the way, not the best way to count page views because I'm counting the same person multiple times. You should definitely just count one person, but that's fine. All right, so I think this should try and send an email. Let's see, did my dashboard say that it tried to send one?

[00:04:03]
Yeah, sent blah a celebration email for getting 10 on article four. So Resend says that it did try and send it. It's probably going to come back as, yeah, it's sent, but it's going to bounce, right, because everything at example.com bounces. So that's expected. It's expected that that's going to bounce. But if you send it to your personal email address, it should say delivered.

[00:04:40]
Cool. Email. This was like, again, I'm just tickled. This used to be so much harder to do. Cool. Any questions so far on email? Perfect kind of stuff for sending transactional emails like this. You can definitely use Resend for marketing emails as well, but this is nice for stuff like this. Render library that we're about to use it. We haven't used it yet.

[00:05:11]
Yeah, yeah, I'm sure you're about to have a valid question. So yeah, we have not used React email yet. We were just using the dumb HTML version here. Someone said their email got delivered to junk. It wouldn't surprise me, yeah. So deliverability of an email is an entire course unto itself, and I loathe it so much, so please don't ask me. God, I don't even want to get too much into it, but like, when you run an email service like this, you generally have IPs, and IPs have reputation, and if I don't know how they run it at Resend, my guess is they have an excellent group of IPs and they have a crappy group of IPs, and they probably put most of us, like free tier users that are trying stuff out, in the crappy IPs because we're going to send a bunch of emails that are going to bounce.

[00:06:02]
And every time that you send an email that bounces, it lowers the reputation of your IP, and every time that you get caught for fraud, it lowers the reputation of your IP, and like reputation is like, does Gmail respect your IP, does Hotmail, does all of these various different email services. And they probably reserve those excellent IPs that have high deliverability rates for their highly paying customers.

[00:06:26]
That's how I would manage it if I was running Resend, and I imagine that's how they have to run it as well. So in other words, I'm not surprised that a bunch of the emails that we send here will get bounced to junk mail. Or maybe stated differently is like if you started paying for this, it's extremely likely that it'll get delivered. I will say that I was messing around with the, you can see here on my Resend dashboard, I sent a bunch of emails to myself.

[00:00:00]
These all got delivered. So it's not always true. Okay. So this is probably going to say bounced by now. Nope, still doesn't know. Email's a very slow thing.

Learn Straight from the Experts Who Shape the Modern Web

  • 250+
    In-depth Courses
  • Industry Leading Experts
  • 24
    Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now