Instructor: The first thing we have
to figure out is how exactly does email work?
We all know that we can log onto Gmail
or yahoo.com and we can start filling out an email.
It's pretty much second nature to everybody by now.
But what actually happens behind the scenes?
Well, let's say that we have a sender,
angela@gmail.com and a recipient, timmy@yahoo.com.
Now, in order to send this email from my Gmail account
to another email account, what happens behind the scene is
that there's a Gmail mail server,
which will receive my message.
And then there's a Yahoo Mail server,
which will store the message until Timmy logs
onto his computer and logs onto yahoo.com,
which downloads the email from the Yahoo Mail server.
So this email is gonna move between all of these steps.
And in order to do this,
it relies on something called SMTP,
the Simple Mail Transfer Protocol.
And this contains all of the rules that determine
how an email is received by mail servers passed
onto the next mail server
and how email can be sent around the internet.
Now, a good analogy for SMTP is
if you imagine these mail servers as a post office
and Timmy's computer being the mailbox.
Then SMTP is basically the postman who knows how to handle
the email and take it to various post offices
and eventually put it into Timmy's computer.
So in Python there's a module called smtplib,
which allows us to use SMTP to send our email
to any address on the internet.
To start, I recommend setting up two fresh email accounts.
Create a new email account with Gmail
and also create a new email account with Yahoo.
These new email accounts will be perfect
for testing your code
and following along with the video tutorials.
Plus, we'll be making those email accounts
a little bit less secure to test our code.
So that's another reason to set up
some testing email addresses for now.
After you've set up your new email accounts,
head over to the course resources and download the zip file
with the starting code for today's lessons.
And then we're gonna open that up using PyCharm.
Now I want you to take a look inside the starting project.
There is a main.py file
and there's also a quotes.txt file.
Don't worry about this file for now.
We're gonna come back to it
when we explore the datetime module.
For now, we're gonna be working within the main.py.
And I wanna show you how you can use this smtplib library
to start sending emails straight from your Python code.
As always, we import the module smtplib
and then we can start using it.
Once we've imported this SMTP library essentially,
we can use it to create a new SMTP object.
So we're gonna call that object a new connection
because it's basically a way for us to be able
to connect to our email provider's SMTP email server.
We're gonna do this by tapping into the smtplib
and then creating a object from the SMTP class.
Now, when we create this object,
one of the things that we should specify is the location
of our email providers SMTP server.
Now for Gmail, it's simply smtp.gmail.com
but it's different for every email provider.
So that means if your email ends in @gmail.com
then this would be how you would connect
to your email server.
In our case, I've created
a testing email called appbreweryinfo@gmail.com.
And this part that's before the @ sign is
the identity of my email account.
And the part after the @ sign is
the identity of my email provider.
So in my case, I need to connect to smtp.gmail.com.
But if you have a different email provider,
for example if you're with Hotmail, it's smtp.live.com.
And if you are with Yahoo, it's smtp.mail.yahoo.com.
And if you're with a completely different email provider
than simply just Google your email provider
and the SMTP information.
And you should find an article somewhere
that describes a URL that looks something like this.
Once I've created my connection,
the next thing I need to do is to go ahead
and call start TLS.
Now, TLS stands for Transport Layer Security
and it's a way of securing
our connection to our email server.
So that way when we're sending an email,
if somebody else intercepts our email
somewhere along the line and they try to read it.
Because this is enabled that message will be encrypted
and it'll be impossible for them to read
what is in the content of our email.
So this line basically will make this connection secure.
Now, once we've secured our connection,
the next thing to do is to actually log in.
So we'll call connection.login
and here we have to provide a username and a password.
So the username is simply the email
that we use to log on to our email service.
So in my case, it's just my email and the password.
I've just made up a new password.
So my made up password is ABCD1234 and then two brackets.
And once I've logged in to my email provider,
the final thing I wanna do is to actually send my mail.
Now the from address is my email
and the to address is the person
who I want to send the email to.
So I've set up
a new dummy account called appbrewerytesting@yahoo.com.
And make sure that you haven't got any typos in your email,
your password, the SMTP URL or the recipient email address.
Now finally, the last thing we wanna add is the message.
So this is what we actually want to send in our email.
And just like we did with our file
when we opened it at the very end, once we're done with it
we're going to close it off as well.
So now we can go ahead and hit run.
And you might get a number of errors at this point.
Now it's important that if you do get an error,
then you first check to make sure that you haven't got
any typos here, here, here, or here.
But once you've checked that through, then the next thing
you can do is you can actually look at this error code
and follow the URL.
Now in our case, the reason is because by default,
Gmail doesn't just let anybody access your email account.
And you have other ways of making
your account even more secure.
In order to send email from a Gmail account with Python,
the first thing you have to do is create
a special password for your application.
This means you have to go to the security settings
for your Google account.
So if you're inside Gmail,
go ahead and click on your profile.
Go to manage your Google account and then go to security.
Now while you're here you first have
to turn on two step verification.
Once you've confirmed using your phone,
go ahead and turn on two step verification
and then go back to the security page.
And now you should be able to see
this section app passwords show up.
Go ahead and click on it and enter your password
for your Google account again.
(rapid typing)
And then go ahead and create a other type of app.
And we'll name it Birthday Wisher and click generate.
Now here's the really important part.
You're gonna get your app password show up here.
I want you to select all of it and copy it
and then you'll be able to paste it inside your code.
Once we've done all of this,
then we can go back and hit run again.
And you'll see this time we see
the process finish with exit code zero,
which means all of the code ran successfully.
And now if I take a look at my sent box,
you can see that I've got this message that's been sent.
And if I take a look at the email address
which the email was sent to,
then you can see that in my inbox
there's actually nothing here.
But if you take a look inside spam,
then there is our brilliant email
that came from our Python code.
So first thing I'm gonna do is change that to not spam
to make sure that it goes into the actual inbox.
And the next thing we wanna be able to do is
how can we make our email seem less like spam?
So an email without a subject headline is prime target
for being filtered as spam.
So let's go into our email
and see how we can add a subject line.
It's pretty simple.
It goes inside the message parameter
and all we have to do is just write the word subject, colon.
And then we can put in whatever it is
that we wanna use as the subject of the email like this.
Now, how do you put in the content or the body of the email?
Well, you add two new lines using /n /n
and then you can put the content.
So this is the body of my email.
And let's just split this up
so it's a little bit easier to read.
And now I can hit run again.
And once that's done, you can see
that in my Gmail in the sent folder
that this message now has a subject line and a body.
And it's the same thing when I go to my...
And when I take a look at this new email that came through,
you can see the subject line
and the body of the email being separated.
Now we can actually avoid having
to write this line connection.close
if we do the same trick as we did with file opening.
We use the with keyword
so we can say that with smtp.SMTP to create the connection
and then we save that as the connection.
Then we can indent all of the rest
of the code inside this block.
And once it's done with sending the email,
it will close off that connection automatically.
So this is how you could send email using Python
and smtplib.
Now there's quite a few things here that are prone
to errors, especially given that we're typing a lot
of things in plain text, like our email,
our password and a bunch of things.
So if you are getting errors when you are running your code
or if it's not doing what you expect it to,
make sure that you've checked against all of these strings
and it's actually what you expected it to be.
Secondly, make sure that whichever account
you're sending from that you actually go ahead
into the account and modify the security settings.
For example, let's say I wanted to send
from this email address, so I'm gonna put that in here.
And then in this case, the email server I need
to connect to will be Yahoo's email server,
which is under the URL that I showed you before,
smtp.mail.yahoo.com.
And that is actually not enough.
So let me just change my recipient
and I hit run.
This is the error that you'll see.
It says SMTP server disconnected,
connection unexpectedly closed.
Now this could be down to a number of reasons.
For example, a typo in this part.
But also if your account doesn't
actually allow less secure apps.
So on Yahoo, the process is a little bit different.
You have to go into your account, go to account info
and then go to account security.
And you'll have to log in again
and generate a new app password.
So we're gonna create a new app
and we're gonna give it a custom name, Python code.
And then click generate.
And now we have a app password to use for our Python code.
So now back over here, we change this password
to the one that we just copied over.
And now if I hit run, that error should go away
and we should get process finished with exit code zero.
And when I take a look over here in my inbox,
I've got my email from appbrewerytesting@yahoo.com.
So check your security settings, check your spam
and check that you haven't made any typos.
And if none of that works,
then just simply try doing everything I did in the video
but with a new email account that you set up with Gmail.
Then you can use the same SMTP address
and the same process that you saw
in the video to try this out.