Adding Subscribers

Emails and Clients - Part Two

Summary

Coming soon.

Transcript

Alright, so to actually send emails with SES, we need a few dependencies. I'm just going to copy-paste them into the terminal here. So you can either copy them off the screen or follow along as we decode this and your IDE or your LSPs should pick them up and you can add them to the Go mod file. So let's just get all of this in. Hopefully we have everything now. Then we're going to create a new package called Clients. Inside of Clients, we're going to create a new file called aws.ses in package Clients. Great. We need two things to begin with. We need some const here. We need to set the charset, which is going to be utf8 and an AWS region. I'm based out of Europe, so I'm going to be using Central1, but pick the one that is closest to you or the one that your AWS account lives in or is specifically the one where you have the SES set up in. All right. Next, we're going to create a default sender email, so const defaultSender. I'm going to use noreply at myblog.com. This needs to be a registered domain that you have with SES. There's a bunch of guides on how to set it up. It's a little bit out of the scope of this course, but it's not that difficult and there's a ton of guides on YouTube on how you can set it up. So pick something that you have specified in your SES account and then have something like noreply. So if you don't provide a sender, this is what AWS will use to send your email. Then we're going to create a struct here called SimpleEmailService that has one dependencies or one field. It's a client, which is a pointer to SES, SES. Go. Let's just create a new function so we can quickly initialize it. There we go. It takes in nothing and returns the SimpleService. Now, we're going to be using a bunch of these SDKs from AWS. So to get the credentials that we need to interact with our SES account, we're going to create a variable called crits that uses the credentials. Yes, a new credentials. And in here, not in here first, we actually need to say conf and then create an AWS config where we need a specific AWS. There we go. And here we need to set the region, which we already specified, but we need to use this AWS specific type. So for a string, we need to use an AWS string where we can now pass the AWS region and credentials. It's going to be our crits variable. All right. With that, we can now create a new session. So we say session, new session, pass the conf. And if there's an error, in this case, we want to panic because this is a setup function. So this will get called before the application starts to run. And I made a mistake here because it's not new credentials, it's new ENV credentials. So you would need to expose a key and a secret key in your environmental variables that this one would then pick up and use to authenticate your session or your sending. Right. Finally, return symbol email service. We just pass this ses. And we do not just pass this ses because we need to say new. We need to say, of course, SES new and then pass the session. There we go. Now, in terms of sending email, we are just going to be creating a single method for now that is going to be called send. What you don't want to do is if you have a new domain and just start to send a ton of emails, you want to go slow. So if you anticipate to send a bunch of emails, it would be a good thing to batch them. So just be aware that you'd be a little bit careful with the new domain that you're sending from that you probably should warm it up, like send slowly in the beginning, and then you can start to scale up the amount of emails you send. But this will be handled outside of this client. Right. So enough talking, let's get on to the method here. I'm going to create a send that's going to return an error. And this send method would take in the context. Context, it will take in a recipient, recipient, recipient, sender. It will have the HTML payload and it will have the text payload. And finally, it will have a subject that's also going to be a string. Cool. So we can do a little check in the beginning here, say from is equal to the default sender. And if the sender is an empty string, is not an empty string, of course. Oh, there we go. It's not an empty string. Then we're going to say from equal to sender. And then we are going to create an input variable here of type SES sent email input. Now I'm going to have the LSP fill out this struct. So we're not going to need the bound sender. We're not going to need the bound sender RN. No. And I also imported the wrong one. So everything is wrong here. Sent email input. There we go. And now we get the right one. So we want to have destination, we want to have message, and we want to have a source. Yes, we can tag and we can do a lot of other stuff, but we only need to begin with these three fields. So we're going to have two. That is going to be a slice of string pointers, where it's just going to be the recipient, right? So AWS, we're going to have AWS string with the recipient. And you can send multiple ones here. If you wanted to, we're just going to have one. And then we want to go into our message here, where we are going to say the body is going to be an SES body that takes in the HTML. And this is an SES content. You could also have a text, right? That is also going to be an SES content. And in here, it's literally just past our. So if we want to fill out content, the char set we already have, AWS string char set, and the data is just going to be AWS string one more time, and then HTML. And we cannot say HTML is undefined because it's HTML payload. All right, so copy paste it, this go into text, and then just update the HTML payload to be text payload. Great. Then we need a subject, which is, again, an SES content. You will find that working with AWS SDKs can be interesting. The char set, same as before. The data, let's just grab the data here, is going to be the subject that we passed to the method. And this is pretty much our input. Now, we are still lacking a source, right? So we need AWS string to be the from variable. And now we have everything. Then we can call sclient send. Actually, send email. There we go. Text in the input. And it returns as SES email output that we're not really going to need. We're only interested in the error that we're going to return if there are any. If not, we're just going to return nil. And this is pretty much how our send method is going to look like, really, really straightforward. Once you have this set up, if you need to, quote unquote, load balance this sending, you could do it in a background queue or you can do it in an in-memory queue. Lots of different options here. But for the purpose of this client, it only needs to send. If you were to send bulk emails, that's the same email to different recipients, you could technically handle the load balancing in here as well. But our send method is just going to be focused on one thing, and that is sending a singular email. Right. So that was quite a lot. And we're almost done. We just need to update our controller's dependencies. So we have this controller struct here where we want to add an email client from clients, simple email service. And again, we pass the dependencies into the new function. So we can say email client. There we go. Finally, we have our main.go file that is now complaining about our missing dependencies as expected. So here, let's just create an SES client from clients. New simple email service that we can then pass to the controller.new function. And now we are basically ready to accept subscribers. Of course, you need to set up your SES account or take this implementation and switch it out for whatever email provider you want to use. It's only the meat of the send function you really need to adapt to your provider. However, this is it for emails and clients. In the next episode, we are going to continue with testing and verifying that our subscription and verification flow is actually working the way we expect it to.

Early Access

$95 $65 USD

Get access

Invoices and receipts available for easy company reimbursement