In this lesson and we're gonna kick off this section on email sending by well sending an email from
a..
J ust the first thing we're gonna do is explore these service.
We'll be using.
And then from there we will actually write a little bit of code to send our very first e-mail from our
node application.
And then throughout the section we'll actually set up emails that get sent as users do things like create
their account and delete their account.
But for the moment let's just see what it takes in terms of code and modules to actually get this done.
Now when it comes to picking a service for sending emails you have plenty of options available to you.
And the one that we're gonna be using in this course is called Send grid.
We can find it by heading over to send grid dot com inside of the browser and we're gonna have to create
an account and set it up before we'll be able to send emails from node j s.
Now one of the reasons I'm picking send grid is that there is a nice free tier so you can get started
integrating email into your app before you ever need to pay.
And it also doesn't require you to have a custom domain to setup other email services typically do even
to just test things out.
So with send grid we'll be able to start completely for free and there's no need to provide anything
like a credit card.
So if we head over to their pricing page real quick we can take a look at the plan that we'll be using
for the class.
It is the free plan.
We get 40000 emails for the first 30 days as a sort of trial period.
Then we can send 100 a day thereafter forever.
Now that might be OK for a real application but it seems unlikely if you were creating a real application
and you were charging your customers you would probably upgrade to this other plan for 15 bucks a month
where you could send a hundred thousand emails a month and that should be more than enough as you're
getting started.
So once again the free plan is going to allow us to send emails from no J.S. forever.
There's just a restriction after a certain point in time.
Let's go ahead and actually create an account down below on the pricing page we've selected that free
option.
And down here we have a try for free.
We're going to sign up for an account send grid is going to give us an API key and we'll be able to
use that from no J.S. to actually send those emails.
The setup process here can be a bit confusing at first solo work through it together.
We'll pick a user name down below a password
and we'll do the same thing down below for confirming
and then finally an email address.
Now from there we just confirm we're not a robot and agreed to those terms of service.
There's no need to have a custom domain or provide a credit card or anything like that right here we
have to provide just a little more information which I'll take a quick moment to go through
and for the Web site.
You can just say something like no cite yet down below you can just say you're the owner of your non-existent
company.
And then we just need to pick how many e-mails we plan to send.
We'll just do zero to one hundred thousand.
Next up how many employees.
Well obviously just us.
And then we can get started now from here it's offering up some suggestions as to how we can get started
with these service down below.
There are a few different ways we can send our first e-mail.
We can send it using their API.
We can integrate with popular applications like WordPress or we can build and send marketing campaigns
if we were creating an email list for our service maybe before it launches.
In this case to send one off emails from no Jay S. We want to use their web API.
So right here I'll click Start.
Now we have a web API and an S MTBE relay.
We'll be using their Web API because that's going to allow us to take advantage of these send grid NPM
module which we can use to send emails.
So down below I'm going to pick choose and from here it's going to walk us through a little wizard allowing
us to get set up.
We're going to integrate with node j as the first option available and then to make sure things are
actually working.
It's going to walk us through a what five step process to getting these send a grid service integrated
into the node app and we're actually going to work through this in Visual Studio code.
Well let's go ahead and work through this together the first step is to make sure we have node.
That's an easy one.
The next one is to create an API key.
So this is something we're going to provide in node j s when we send the e-mail mail and that will allow
send grid to associate the email we're sending with our account and we have to provide an API key.
You can't use send grid anonymously so down below we can pick a name for our first API key.
I'll go ahead and use something like task app or task manager app or anything that makes sense for you
right here we'll go ahead and create that key and down below.
It's going to show the key to us.
We want to take a moment to copy that to the clipboard as we're going to need it from inside of node
j s now to get started over in Visual Studio code.
We're going to create a new directory in the source folder.
And we're also going to create a new file.
So for the moment we can actually collapse all of the other directories in the source folder and we
can close all of the open EDS.
We're going to start with just a single file.
So right here in source we'll create a new folder I'm going to call this e mails and inside of the e
mails folder we'll create a single file which I'll call account dot J S so this will be the file that
contains all of the code for sending e-mails related to use your accounts.
So for things like signing up or deleting your account.
Now for the moment we're not going to worry about actually doing that.
We're just going to add a little code that sends a test e-mail.
Let's get started by just creating a variable to store that API key.
So right here a constant I can call it send.
Grid API key and we'll just set it equal to a string and write inside.
We'll paste the API key that was generated for us now from here.
We want to go ahead and actually install the NPM module for seven grid as we can use its methods to
send emails.
Now over inside of the instructions in the browser they're essentially doing the exact same thing.
They're walking you through the necessary steps to get all of this done.
Though we'll do this as part of the video.
Now the only major difference is step number three they're creating what's known as an environment variable
to store the API key.
Don't worry we're going to get to this in about three videos.
We'll learn how to get this out of our code so it never winds up on something like get hub.
But for now it's perfectly fine to leave it where it is.
We'll address that shortly.
So over inside a visual studio code we want to take a quick moment to install these seven grid NPM module
so we can actually use it to send emails.
So down below what we're going to do is use clear to clear the output and we'll be using NPM AI with
the following the module name is at send grid forward slash mail and the latest version that is apt
right here.
Six point three point one.
So let's take a quick moment to install that.
And up above online one of account dot J S we'll be requiring it.
So const s g mail which is the common variable name you'll see in the send grid documentation.
Its value comes from using require with that module send a grid forward slash mail and on s g mail which
is an object itself.
We have methods for actually sending emails.
Now before we can send an email we have to let these send grid mods you will know we want to work with
this API key and that's what's going to make sure that things work as expected we can do that using
s g mail dot set API key and we pass to this our API key which I have stored in my variable up above
that's sent grid API key.
Perfect.
Now that we have the API key set up when we send an email send grid will know it's associated with our
account and down below we do that using s g mail dot send.
This allows us to send an individual email and we pass to it an object.
And on this object we specify everything about the email who it's to who it's from the subject of the
email and the actual text content right here.
Let's go ahead and specify that information.
So too I'll send it to myself.
Andrew at Meade dot I.
Oh and I'll send it from that same account
next up we have the subject line for the email and I'll go ahead and say This is my first creation and
then below that we have the text for the email.
So the text is going to be something along the lines of let's just say I hope this one actually gets
to you.
Perfect.
Now in addition we can provide an h t AML property to set up a styled H T and L email if we wanted to
but we're not going to worry about that we'll just stick with basic text for the moment.
Now we can save account not J S and to actually send this email off all we need to do is run this script.
This is Andrew from the future with an important update since this lesson was originally added to the
class send grid has updated requiring an additional level of verification.
Now this might seem like overkill.
We've already done quite a bit of verification and it seems like overkill for a service that simply
sending emails well sending emails is very much a reputation based game which is why so much verification
is required.
You might think if I send an email to some email address that receiver should get the email but that's
simply not the case.
The reputation of the sender plays an important role in whether or not the email service will treat
that as spam.
So long story short if a bunch of people send spam from their send grid accounts that makes seven grid
look bad legitimate send grid users who are sending legitimate emails won't have their emails sent successfully
gmail will see that send grid is spam and it'll start blocking everything.
So that's why so much of verification is needed now.
We're not using send grid for spam.
We're using it to send a transactional emails to users who interacted with our applications.
So we're good to go.
We just need to go through this additional verification you can do that by scrolling down in the sidebar
and opening up settings.
From there you can open up something called sender authentication.
Now if you click on that you'll see what I see here.
Now I'm seeing two different ways to get started.
You should see three.
So I have authenticate your domain and brand your links right in between you should see a way to get
started with something called single sender verification.
So I'm not seeing that option just because my send grid account was created before that rule was put
into place.
You should be seeing that option right there and you'll want to click get started.
Now if you're not seeing that option that's OK.
You can pull up that page directly.
So right here in a second tab I've manually pulled up that page you can find it an app that send grid
dot com forward slash settings forward slash sender underscore off forward slash senders.
Now from here all you need to do is create a new sender down below.
So right here we can click Create new sender and it's just asking for some information information that
we already provided when we first created our account.
So down below it's some basic information.
But what I'd like to point out is the from email address and the reply to email address.
So when our code we call the mail method provided by Sen. Grant we pass to it an object and we've provided
those four properties.
We have two from text and subject the from value that you are using is where the email is getting sent
from.
You need to use that same value for the from email address and for reply to down below.
So make sure that these two fields match up with the Frome property you've set up in your source code.
So you'll go through the process of filling out this form down below you'll click Create.
And this is going to send an email to that account.
You'll just need to click the link inside of there to finish the single sender verification process.
Once you have that done it should be showing up right here and then you'll be ready to continue on with
the regular lesson.
So sorry about these changes the send grid service and other email providers they're always adding additional
layers of verification to stay on top of spam and keep their reputation pristine.
But once you work through that process you should be able to continue on with the regular video.
So let's go ahead and jump right back in so down below in the terminal what I'm going to do is use node
then source forward slash emails forward slash account dot J asked to send the e mail off right here
I run the script.
The script seems to work successfully and now we can go ahead and see if it actually worked via two
ways.
One we can use these send grid interface and two we can go ahead and actually open up our email accounts
so down below as you work through this little tutorial it eventually gets to the point where it asks
if you've sent a test email and it's going to verify that it actually went through that you were able
to correctly send the email with your account.
Go ahead and try to go through the verification process right here.
I am going to make sure that it actually did work and we can see that it did work.
So the email that we sent from no J.S. was received by the send grid server and sent off to the g mail
server which is where my email is hosted.
Now I'll go ahead and take a quick moment to pull that up.
So I'm going to open my email account right here.
It should be loading up in just a second and I would expect a couple of emails inside of there.
I have a couple greeting me to the send grid platform and then I have this email which I sent to myself
right here.
The subject line.
This is my first creation.
And if I crack it open I have the correct text as well.
Now this email might not have made it to your inbox.
It might have made its way to your spam folder and you'll also see this right here.
A really big warning letting you know that this email might be spam.
Now if you read the error message here it says this may be a spoofed message.
This message claims to have been sent from your account but Andrew Meade mail couldn't verify the actual
source and G mails right.
I just typed two addresses over here.
Now two should be able to be anyone but from should have to be an account that I actually own.
Now I do own that account but Gmail does not know that I was the one to actually call the send method
in node j s now to address this and to get send grid working reliably.
All you have to do is verify your email they sent you a welcome email to confirm your email address
and the next step would be to have a real domain you link with your account.
So if I'm creating an application I would buy a domain.
I actually do own Meade Dot.
I oh I would link that with my send grid account and that would make sure that other services like Yahoo
Mail outlook Gmail and others know that I actually have rights to send emails from addresses ending
in at Meade Dot.
Oh and we would no longer see those warnings.
You can learn how to link your custom domain to send grid in the documentation though buying a domain
is not a requirement for this course.
We'll still be able to send all of the emails necessary but for the moment they will have that warning.
Once you actually purchase a domain and link it once you're creating a real professional application
you plan on publishing all that will go away and you'll have a nice reliable email solution.
So now we have our first email sent and you should be seeing it whether it's in your inbox or your spam
folder.
And we were able to verify that everything with send grid worked as well.
In the next lesson we're going to start to send emails and based off of the user's interaction with
our application.
I'm excited to get to that.
So let's go ahead and jump right in to the next one.