Let's send our first email.
Now, this requires a lot of configuration that again, you'll just have to get used to.
So if it looks overwhelming, don't worry.
Once you have this basic setup, you can send any type of email you want.
So the first thing we're going to do is I'm going to say email is going to equal this email message
that I just imported this email message class.
And this is going to be our email object from here.
We can add to this object the from key, which will be, hey, who's this email from?
This email is going to be from myself, from Andre Nagwa.
Next is who is this email going to be to?
Well, this email is going to be to well let's say Andre at zero to mastery dot io.
So we want to send the email to this email address.
And then the email subject line, we'll say, what should we say?
Let's create the worst spam email possible.
I think this will be funny.
Let's say you won.
$1,000,000.
There you go.
That's a spammy subject line, that's for sure.
So that's our subject.
The next thing we're going to do is say email dot set.
Content method.
Which essentially says, what's the content of the email?
In our case, let's just say I am a Python master.
Now in here we can actually set any type of content we want.
We can do text, we can do HTML, we can do images.
We're going to get to that a little bit later on.
But for now, this is fine.
There you go.
That's our email.
Object with the from to subject and content.
We have our email, but we need to send it right.
So what we need to do is to use this SMTP server to log in to our Gmail client.
And then send that email from there.
So all we going to do is say with SMTP, lib dot SMTP.
And in here we're going to say what is going to be our SMTP server?
And the reason I got you to create a Gmail account is just so you can follow along because this is going
to be custom to whatever email client you're using.
In our case, we want our host to be Gmail.
So I'm going to say SMTP dot Gmail, dot com.
This is going to be our host.
And then this SMTP is going to be running on port.
Five, eight, seven.
Again.
You don't have to know this.
Exactly.
If we go back to SMTP, you see over here that typically submit outgoing email to the mail server on
port five, eight, seven.
Remember, SMTP is a protocol.
It's a standard for machines to communicate with each other by using port 587 is just a standard way
for SMTP server to be run.
Like I said, this is one of those things that you can usually just Google around and copy and paste
because it's going to be the same if you have a different client than Gmail.
Then again, you can Google and see what type of host you need.
In our case.
Let's continue here.
We're going to say with this host as SMTP.
Because we're going to use this now.
Let's make this a little bit.
Bigger so you can see.
And in here, this is the part that's going to look confusing.
But again, it's just you do it once and once you do it, you have an email server that you can use.
I'm going to say SMTP dot e flow kind of looks like hello, but it's not.
Hello.
This is actually part of the protocol of the SMTP.
Again, the agreement, it starts with a little hello message that well, it's type like this you can
actually google why this method is like this and not actually says hello.
It's actually a pretty funny story.
So we have SMTP.
Hello.
So that's the initial.
Hey, this is a server.
I'm awake.
Hello?
Message.
Next we do the smtp dot.
Start.
T ls.
And TLS is an encryption mechanism, so we want to connect securely to the server.
And now this is the critical part.
Up until this point, you might be confused as to what we're actually doing here.
We're essentially with this next line, we're going to connect to this email account, this Gmail account
that I created, so that instead of me opening this page, hitting, compose and writing that email
that we already wrote, I can do it from code.
So what am I going to do?
I'm going to say SMTP dot login and I'm going to log in with my credentials.
So the first one is going to be this dummy email that I created, which is zero to March 31 at Gmail
dot com.
And next is going to be the password that I used for this login.
Again, this is something that because I've created a dummy account, I don't mind sharing it with you.
The password is.
Hello, XHTML.
My old friend.
One.
So this way I'm able to log in to this server.
And guess what we're doing next?
Well, next we're going to say send.
Message and we're going to send why are we going to send this whole email that we've created?
So let's do email.
And just for fun.
At the end, we'll say print.
All good, boss.
There we go.
Now, if we didn't make any errors, this should actually work.
Let's give it a go.
I'm going to say Python three and then run our email center dot pi.
I'm going to give you a warning here.
If you've named your pi file email pi.
This actually might give you an error, so make sure you name it something other than email dot pi.
Let's give it a go.
There's a bit of a pause and we have all good boss.
There was a pause here because we had to connect to our server, log in to our account, and then send
the email and then all good boss.
So it looks like this code worked.
So let's test it out.
I logged into this dummy account and I sent an email to Andre at zero to Mastery Audio.
Let's check that email account and look at that.
You want $1,000,000?
I am a python master from the email address of our dummy account.
Now I actually had to pull this out of the spam folder because it was marked at spam because well,
it definitely looks like spam email.
So that's awesome.
We're just able to send an email through Python.
How cool is that?
In the next video, we're going to explore this a little bit more and make this a little bit more dynamic
so we can customize it to each individual that we send email to.
I'll see you in the next one.
Bye bye.