So now we want to look at how we can make our emails a little prettier, and I'm not going to put a
great deal of effort into actually making it pretty because I'll leave that up to you.
But I'll show you how you can do it quite easily.
You may recall that for our public facing website, we're using the bootstrap framework to format things,
and that makes things ever so easy.
But there are more found more frameworks out there based on cascading style sheets.
And one of them is called Foundation, which is produced by the ZRP Foundation.
And the site you're looking at right now is is getting foundations, emails, dot, HTML.
And this is a subset of a sixth framework called Foundation, which is intended to be used exclusively
for email.
So go there and then click get started.
And then we want to download this and I'm going to download I'm not going to use the SAS version because
I don't want to get into actually writing SACE right now, which is a way of managing access.
Instead, just download the access version, so download the starter kit.
And when you do that, you'll get a zip file.
And I'm going to show you that I have the zip file already and I've expanded it.
So get it and extract it, then open it up.
And inside of that, you'll see in this year's success folder the actual access that's used.
But what I want to look at right now are the templates.
And I'm going to look at some of these.
Just preview them.
There's a preview of the template that's called Basic, which looks like it's ideal for my purposes.
But there are others.
You can go through the whole list.
There's the responsive emails, which is very nice.
There's Elijah Bailey.
There's all of these things.
So the one I want to work with is the right here.
Let's get back up and find the right one again.
It's not this one.
It is this one dripped HTML.
That looks like a nice format for me to use in this course.
So what I'm going to do is open up dripped on HTML in my favorite text editor, go away in my favorite
text editor, so I'll put it up in sublime text.
You can use any text editor.
You want to try that again.
Drip.
There it is.
So this is just HTML with a bunch of inline access.
So this is just the way we've been doing earlier in the course.
This is just an HTML file and it has a whole bunch of success which is suitable for making emails and
making responsive emails.
So I'm going to copy this whole thing and then I'm going to go back to my editor, my goaland in my
case, and I might create a new folder at the root level of my project and I will call that directory
email templates.
And inside of that, I'll create a new file, which I will call basic HTML, and I'm going to paste
the contents of that HTML file I just copied into this file.
And this will be a template that we can use to send email.
So what I need to do is to get rid of the things that I don't want.
So let's go back and preview that again.
I don't want this responsive email heading like this.
Maybe I want to have the title there, but I definitely don't want this 15 sections, 567 minutes.
I don't want this.
I don't want the button.
And I'll fix this to what I want it to be at the bottom of my message.
So we need to fix this this template, get rid of the things that we don't want.
So what I'm going to do here is scroll all the way down past the access and there's a lot of it.
But don't worry about that and get down to the part that I actually don't want.
So somewhere here we have.
There it is.
I don't want this part, the 15 sections and 567 minutes, I'm going to delete that and I'll just put
the name of my bed and breakfast here.
So the name of my bed and breakfast, I can't remember what it is.
Let's go find out.
Localhost.
Localhost 80.
Hopefully that's running and it is and I call this Fort Smith bed and breakfast, so I'll just put Fort
Smith in there instead of Fort Smith, and then I'm going to come down here and find this section.
I don't want this in here.
Instead, what I want in this is going to be some content and I need a placeholder.
What I'm going to do is use this as a template and then I'm going to load the template before I send
an email and I'll substitute something that I put in here that I know is never going to appear in an
actual email message, some kind of text that I can search and replace on.
So I'll use square brackets and a percent sign and I will call it body.
There and I can have as many of those placeholders as I want, but that's what I'm going to do right
now, is just use one so square bracket, percent body, percent square bracket, and I'll get rid of
this button that I don't want.
And I'll go down to the very bottom of the message and I'll update my footer to be whatever I want.
So copyright is spelled wrong.
Copy, right.
I'll just put copy.
Right.
And then some year I'll put 20/20 and whatever else I could change the rest of this to whatever I wanted
it to be.
And that's all that I need to do.
So I've now have a template I can use in my mail package.
Let's go back to our go package here.
I think I just change it in the wrong spot.
I did.
So I'll copy all of this, go back to my go project, open basic and select all delete and paste there.
And that should be what I want.
That should have my body tag and it does.
All right.
So I've got that.
What I'm going to do when I send email is if I specify a template that I'll use that template.
If I don't specify a template, I'll just send the way we did in the previous couple of lectures.
So in order to make that happen, I need to open my models.
Which is internal models open that up and I'm going to add a member or a field to this male data struct
the type male data and I'll call it a template and it'll just be a string.
And then I need to open my sendmail, go.
I'm going to make some changes to this.
And what I'm going to do is at this point where I set the body, I'm going to check to see if that template
has been specified.
So I'll say if my template is equal to nothing, then just do what we did before.
Set the body using that line of code.
Otherwise, if the template has been specified, I need to load it from disk and fortunately go like
pretty much every decent programming language out there has built into the standard library utilities
to read things from disk.
And this is the simplest way to do it.
I'm going to read some data.
I'm going to read into the very variable data and potentially get an error.
And I'm going to use the built in package.
I owe util, input, output, utilities, and the function I want is called read file.
And I'm going to read from a particular location.
I know that my templates are going to be stored in my newly created folder called Email Templates,
which is right up here.
So I'll specify the path to that dot, which has started to level the project, go to email templates
and then load a file.
I don't use a placeholder as a string, which means I can just use format dot as print F and replace
that placeholder with the name of the template.
So that reads the template in the memory and it reads it in a way that we need to convert it, we need
to convert it to a string.
But first, let's check for our error.
I'll just put if error is not equal to nil, then
apt error log dot print line.
I'll just print there.
I know it's going to work because I know it's there.
So now I need to convert this, this template that we've read off of disk.
And if you look at the read file, what it returns is an array of bytes.
I need to convert that to a string and we already know how to do that.
So I'll just say male template is assigned the value of convert my string or convert into a string the
data that I read off disk.
So now I have that template, a memory, and now I want to replace that body placeholder that I put
in there with the content that I have specified in the message mail data that I've sent to this particular
function.
And we can you think you could do that using format as print, but you can't I can't put a percent in
there and substitute it.
It just doesn't work.
So we'll do it a different way.
Message to send, which is a variable I'm creating, is going to be assign the value of from the strings
package use replace.
And that takes a number of arguments, the first one that takes is what is the string you want to do
to replace in?
Well, that's called Bill Template, the one we just read off disk.
And then what is the text that you want to replace?
And in this case, it's square bracket percent body percent closing square bracket.
And what do you want to replace it with?
And what I want to replace it with is whatever I passed as the content of my e-mail message, which
is my content.
And how many times do you want to do it?
Once.
So that will give me a message properly formatted using the template I read off disk and now I just
assign that to the mail message I'm sending.
And I do that by saying email don't set body.
The type is from the mails, package, mail, dot, text, html and the content is message to send and
that should do it.
So let's go back and try formatting from our handlers.
Let's try formatting the message we're sending to the Jeff's guest.
OK, so up here, instead of saying just send this content, I'll now add a template and the template
will be equal to the name of the template I want to use.
That's inside my email templates folder, which I called basic HTML.
That's going to be in quotation marks because the string basic HTML.
All right, now let's stop our application, run our application, make sure it all compiles.
And it does and I'll go back to my Web browser and I'll open mailbag, there's nothing in there.
Good.
I'll go to my nice page, I'll go to the home page and I'll create a new reservation and I'll do it
for sometime in March.
I'll make it the 10th to the 12th.
Search availability, generals quarters, first name is Clark, last name is Kent Clark at Daily Planet
Dotcom and some phone number, and that should send a nicely formatted e-mail message.
And as I said, I'm not putting a lot of effort into making it pretty.
I'll leave that up to you.
You can read the documentation for the Foundation emails package and it's got great documentation,
but let's see if it works.
So we have a notification reservation.
Notification should be the one for the the property owner, which should be ugly.
And it is.
And let's look at this one and that's format a little bit better.
Now, obviously, I left some things in there that I should probably pull out those placeholders.
But the point is now I have complete control over the way that my email message looks, and it seems
to work very, very well.
So you can modify that basic HTML template to look exactly the way that you want and you're off to the
races and that's all there is to it.
So experiment with that.
If you're interested in making nicely formatted email message and bear in mind that the foundation package
is not the only one out there.
You may like it.
You may not.
I find it works really, really well for my purposes, but it's entirely up to you.