In this lecture, you'll learn how to send an email from an outlook or a Hotmail address.
For that, we're going to be using smtplib.
This is a standard Python library, meaning you don't need to install it.
This comes shipped with Python.
Once you import the library, then you want to prepare the email, which means you want to define
who the sender is.
In my case, that is a Hotmail address, so you can either put a Hotmail address or an outlook address
here.
This is a throwaway account that I have created.
It's a Hotmail address.
And then the receiver, the receiver in my case, would be a Gmail account, but it could be any email
address.
And the passwords of the sender.
So the password of your Hotmail or outlook account.
Now you can either place the password in here.
I've created a temporary passwords for this account, so you can either place the password in here as
a plain string or if you want to be more secure.
So it's up to you.
It is recommended to make it more secure.
Like, I'll show you just now.
But it's up to you.
You can also leave it your passwords here.
If that's not your main email account, for example.
So if you want to be more secure and if you are using a Repl, then you can go to secrets.
This icon here and then here add a key such as passwords, and the value. The value will be your actual
passwords.
So there.
And Press on add new secret.
So you have just created an environment variable in the Repl system, then you can delete that and
use os.getenv and then place the name you gave to that environment variable.
So passwords in capital letters, that's what I entered.
And then import os.
Now this is what you do in Repl.
If you're using your local computer and you don't want to save the password as a plain python string
here.
Then you want to go to
pythonhow.com.
And then search for here in the box, search for passwords securely.
And this is the article you should follow how to create a password securely on Windows and also on Mac
or Linux.
And then once you follow those instructions, then you write this line here.
So the password will get the value from the environment variables.
For simplicity, though, I'm just going to write python12345678.
The plain password in here and then carry on with the message.
So the message or the content would be a multi-line string.
Now what we're sending this time in this video is of plain text email.
So it's not an email that contains different fonts.
For that, you'd need an HTML email, which is what we're going to do in the next video.
So this is just plain text.
No fonts, no attachments.
And the syntax for this is you need here a back slash and then you write with a capital S subject, colon,
space and then write the subject of the email.
For example, hello, hello.
Then you want to make a break line and leave an empty line there and then carry on with the body of
email.
So the content.
This is Ardit.
Just wanted to say hi.
So you should follow.
You must follow this syntax with triple quotes here.
Triple quotes here.
So it's a multiplying string.
Then you have the backslash here.
Subjects, column, space, of the subject and then a brake line, empty line and then the content.
The body of the email.
So that concludes the preparation of the email content.
Now we want to start up the server, the SMTP server and send out the email. An SMTP server is a server
whose role is to send and receive emails.
Now we're going to use the outlook SMTP servers, and for that, we need to the domain of that server
and the port of the server.
And these two are publicly known.
So all we have to do is create a server variable and point to smtplib.SMTP.
So that is a class and it expects two arguments, the domain.
So smtp.office365.com, that's the domain of outlook and the port is
587.
Once you have instantiated the server, you want to say server.starttls.
So you are starting the tls protocol, which is a protocol to send emails in an encrypted form.
And then you want to say server, so you always point to the server now.
dott login, with the log in method
you are logging into your email accounts, outlook or Hotmail.
So I'm logging into this email account now using the sender variable so that email address and the password
variable, that password there.
Once you log in, we are ready to use the send mail method, which expects again the sender.
And the receiver.
And the message.
So that message that string, and then finally, we can say server.quit to quit the server.
And yeah, you are ready to run the code.
If there are no errors, then you probably already received the email, so go and check if you got an
email in your receiver account
which is this one here.
Now, be careful if you're using a new outlook or Hotmail account, then it's probable that the email
might be ending up in your spam folder or junk folder.
So that is the case for me, and that's the email.
So you see, the subject is Hello, hello, this is the body of the email I can report.
This is not spam so that next time I'll probably get this email in my inbox.
So this is a Gmail account that's I entered here in the receiver and that's the complete code.
In the next video, I'll show you how to send an e-mail that has HTML instead of plain text.
So basically, you can enter rich elements. You can enter fonts and other elements using HTML.
So let me show you that's in the next video.
See you.