Laravel adds a new email provider
Taylor Otwell added a Resend as an email provider to Laravel, which seemed pretty noteworthy to me! I walk through setting up Resend as your mail driver, creating and sending emails using Laravel's mailable classes, and discuss Resend's pricing options, making it a great choice for indie developers and enterprises alike. Let's take a look at what Resend does.
Everything I said
I like to keep an eye on the Laravel framework on GitHub. Yes, it's great to get updates from YouTubers. Please like. Please subscribe. It's great to get newsletters. It's sometimes great just to go to the source. So when I saw this commit here, this caught my eye. It's by Taylor, and that's not totally crazy. He still commits all the time. But this caught my eye because he's adding Resend as one of the ways to send email in Laravel, and I thought, Resend is for Next.js.
Well, not anymore. I'm gonna do a speed run of setting up Resend with Laravel, sending an email, and then at the end, we'll talk about their pricing. I've got a default Laravel application set up here. It's just totally stock, so if we do herd open, we'll see it in the browser. This is your standard Laravel starting screen. This is gonna be very simple. So let's check the docs. We're in the mail docs, and if we look for Resend down here, the Resend driver, we need to require their PHP package.
So let's hop over here, composer require resend/resend-php. That's gonna bring down their package, put it in our application, and then I bet we have to change just a few configuration items. So let's pop open the mail.php over here. And so that's in our config/mail.php. And you can see here we've got the default mailer is set to log, but let's scroll down a little bit to look at all of the configurations that are available to us.
You can see Resend is right there. So if we scroll down, here's SMTP. You can basically use any mailer that way. But if you keep going, you see that we have Resend down here. So instead of changing, instead of changing this env, I'm just gonna change it here. In fact, I'm just gonna say, let's just do resend.
So that sets up our mail driver to use Resend. Now, there's one more thing we need to do, and that is create an API key on the Resend site. All right, before we switch over to the Resend site, I have to warn you, it's dark mode only, so prepare yourself, cover your eyes if you must, but we're gonna be switching into dark mode. Here we go. Ready? Oh, it's so dark.
Okay, let's create an API key. We're just gonna call this Laravel Demo full, full. Yep, add, and oh, you can see the, you can see the API key. It's okay. I've already rolled it. You're welcome to try to use it. It's not gonna work. So I've got that API key. Honestly, dark mode, dark mode looks pretty good.
I'm not gonna lie. Switching back to light mode. Brace yourselves. Here we go. Now that I have that API key, we need to do one more thing, and that's hop over into Services, and you can see there are some services set up for you already. It looks like Resend is already set up for you. So we now need to go into the env file and set up the Resend key.
So if we hop over to env, we can scroll all the way down here and put RESEND_KEY equals that. Now, when you deploy your application, you'll need to make sure that you update your env file on your production server. I'm just doing it here locally. That should be, that should be plenty. Now, when you deploy this to your production server, you're gonna need to make sure you update your env there, hopefully with a different key than you're using locally.
Uh, but I'll leave that part up to you. I'm just doing it locally, so I'm just gonna put it in this .env. That's it. That's all it took. Resend is fully integrated. We're gonna keep going. I'm gonna show you how to make a mail message and how to send it, but at this point, it, it's just an exercise in programming. We have... We don't have to set anything else up. Let's make a mail message. We're gonna use php artisan make:mail, and I think you can probably just do that.
You can. Hello world. But I don't want to do that. I wanna remove that, clear this out, and we're gonna go, again, php artisan make:mail, and I'm gonna ask what is available. Markdown. That's what I want. I want a markdown email message, and then I'll have Laravel translate that into proper HTML for me.
So clearing this out again. We'll get rid of that. We'll say... What did we, what did we call it? Uh, Hello World Markdown equal... Where do you wanna put it? Mails? Mails? That doesn't make sense. Mail.hello. Let's do that. I think that should work. Uh, Hello World created. So now if we hop over here, we'll see a few things, hopefully.
We'll see Mail Hello World, and we have all of our stuff in here. Markdown is set to mail.hello, and if we come down to resources, views, mail, we have an old one from a previous mistake and Hello. So this is our, this is our, uh, markdown file here.
You can see we are using a few components here, so this should render out nicely, but we still get to write it with markdown. As I'm developing emails, I like to be able to look at them without sending them. I need to render them somewhere without having to send an email to see what it looks like. As you might have guessed, Laravel makes this really easy. So I'm gonna come over to the web.php. We're gonna get rid of that view, and instead, I'm going to return the actual mailable just straight to the browser, and Laravel is smart enough to pick up on it and say, "They're rendering a piece of email." So I'm
gonna say return new Hello World to the browser, and we can import or simplify that. Now, if we do check the browser, we see, hey, that's our email. That's the email that we're gonna send. This is the email that we wrote in markdown. It looks pretty great. So we can pass through data here, so you can pass data into your mail message, which is likely what you're gonna wanna do.
So usually, you'd pass in something like a user. In this case, you can pass in, just as a demo, we could pass in Hello, Aaron. And if we come over here, we could say, uh, this accepts some amount of args, or, you know, let's spice it up a little. Let's say that it is the name. But not only is it the name, it's the name that must be a string. Not only that, we can use property promotion.
So now this is a class level variable, which is going to pay off here in a second. Let's change the subject to say Hello World instead of just Hello World. I mean, for goodness sakes, let's have a little bit of excitement. Moving down here, you can see that, uh, we're returning that mail.hello markdown, and what you can do here is you can, you can just throw in some extra...
Data, some extra, some extra, um, variables. You need to wrap that 'cause that's not coming till 8.4. So you would need to wrap that, and you can throw in extra data down here. But we don't have to because we set this as a class level variable. So if we go back to web and we make this match, so we could say name is Aaron, and then in our markdown mailable, we can say, "Hello, name." And with any luck, we did this right,
hello, Aaron. So now we're passing through variables into the markdown mailable, and w- let's take one last look at this class, and you can see you can add attachments. It makes it very easy to add attachments. Unfortunately, we don't have anything interesting to attach, so we're gonna leave that blank. Now we've got our message. Now we've got our mail message fully built out, and can we just have, can we just have a moment, a, just a round of applause for Laravel and how easy it makes it to construct email messages like this?
And now we can just send it straight through Resend with the same ease. Hopping back into web PHP. Instead of returning it, I'm just gonna set this to a variable, and we're gonna say mail to hello@aaronfrancis.com. Please, y'all, please don't send me any emails.
Um, and we are gonna send the mailable that we just created. If I was doing this in a real application, and I was trying to send mail as a part of the request response life cycle instead of some background job, I would put this on the queue. So I would queue this message to be sent so that we don't waste any time in the request trying to send out an email.
Just put it on the queue. But for now, we're just gonna go ahead and send it. So this is going to send this message when we hit the route in the browser, and it worked. Well, it didn't break. We don't know that it worked. It didn't break. Um, it should have sent, and if we check over here into our... Hey, look at that.
Hello@aaronfrancis.com was delivered less than a minute ago. Please ignore aaron@example.com. I'm not gonna tell you what that was. But if we click in here, we'll see, hey, there's that email. There's that email that we were working on. So this dashboard does give you a nice overview of everything that you've sent. You can come in here and check your emails that you've sent.
You can check your metrics. You can check your domains. You can see I only have aaronfrancis.com set up, and it has been verified, which is why I'm allowed to send emails on my own behalf here. If we wanted a little more insight from Resend in our application, we could subscribe to webhooks. So if we look here on the dashboard, you'll see that there is a webhooks tab, and you can add a webhook so that events are fired back to your application, and we can receive those and consume them on
the Laravel side. So if I wanna know when somebody clicked or when an email bounced or anything like that, I can set up this webhook and keep track of those events on my side. There's also a logs tab here, and you can see this is where I went off the rails, and you won't see that in the video because I hadn't, I hadn't verified my domain. And so I got a, I got a little bit of an error, and that's a good thing 'cause I don't want people sending emails from aaronfrancis.com if they're not allowed to.
So I went ahead and verified my domain, and now we're all good. I told you it was gonna be really simple. Thanks to Laravel and Resend, the process of actually getting Resend set up took a few minutes, and then we spent some time building out a, a pretty easy email message that looks pretty good, honestly. So let's talk about Resend's pricing just for a second. There are two, um, types of pricing or two, um, things that you need to consider, and that's transactional versus marketing.
Transactional is, like, password resets and order shipping notification. Marketing is like, "Hey, buy my stuff." So looking at transactional, you can get 3,000 emails a month for free, um, only on one domain. So for the indie hackers, that's probably pretty good. Once you start getting into a little bit higher scale, you can come over here and scale up the emails up and down.
It stays, the, the pricing stays pretty competitive, um, and you can see some of this other stuff that you get, including, um, multi-region. And then when you get to enterprise, then you get dedicated IPs, SSO, all of that kind of stuff. And marketing emails have a pretty, pretty generous free tier as well, um, up to 1,000 contacts a month. And then if you need to move up, then you have the same slider again.
When I saw that Taylor added Resend to Laravel, I thought, "That's kind of a big deal. I think we should talk about it." So thanks to Zeno, who's the CEO over at Resend, very responsive. Thank you to him and the team for letting me make this video. If you are in the market for a new email provider or you're starting a new project and you need a generous free tier while you find your product market fit, check out Resend.
It's very easy to integrate, and I think you will like it. Until next time, see ya.