I'm sorry to tell you, but this is not exactly the place to get a coding lesson. This is a place to ask for precise coding questions. So it's very likely that your question will get flagged and I will be amongst those who will ask for closing it. But…
…I've been there, learning to code at your age at a time when Internet was just a small portion of what it is today. So let me give you some insight and pointers.
The code you pasted is very straightforward, given you know a bit of programming and understand the SMTP protocol.
When you send a mail, you're doing a TCP connection to a server on — usually — the port 25 (but 587 in your case). There you have to follow a precise protocol to have the server understand what you want to do. This protocol has been defined by the RFC821.
The same way you follow a protocol when you meet someone (which is like "hi!", then shake hands, then say "how're you doing?") before asking a question, the server does the same:
EHLO example.org
MAIL FROM alice@example.org
RCPT TO bob@example.org
DATA
Date: Fri, 21 Aug 2015 23:12:29 +0000
From: Alice <alice@example.org>
To: Bob <bob@example.org>
Go RTFM!
--
Bob
.
Where you say to the server:
- Hi! I'm Alice!
- I Want to send a message to Bob!
- Can you tell him to go RTFM?
Which is what you're actually doing in your code.
Now let's get more on the programming side of things. First of all, you import modules, one for handling an email the other one for the smtp connection:
import email
import smtplib
to understand how to use it, you can launch the python REPL on the command line and do dir(email)
to show its objects, and help(email)
to get the integrated help:
python
>>> import email
>>> dir(email)
['Charset', 'Encoders', 'Errors', 'FeedParser', 'Generator', 'Header', 'Iterators', 'LazyImporter', 'MIMEAudio', 'MIMEBase', 'MIMEImage', 'MIMEMessage', 'MIMEMultipart', 'MIMENonMultipart', 'MIMEText', 'Message', 'Parser', 'Utils', '_LOWERNAMES', '_MIMENAMES', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_name', 'base64MIME', 'email', 'importer', 'message_from_file', 'message_from_string', 'mime', 'quopriMIME', 'sys']
>>> help(email)
Help on package email:
NAME
email - A package for parsing, handling, and generating email messages.
FILE
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/__init__.py
MODULE DOCS
http://docs.python.org/library/email
…
the objects are all the actions and objects being developed by the community that you can use. And the help is giving a bit more insight, as well as the URL you want to visit to know more about what you can do with it, along with examples.
Then, you create a mail message:
msg = email.message_from_string('warning')
msg['From'] = "example@hotmail.fr"
msg['To'] = "example@hotmail.fr"
msg['Subject'] = "helOoooOo"
on which you setup the Subject
, From
and To
lines, as well as the body, in the SMTP protocol that would look like:
From: example@hotmail.fr
To: example@hotmail.fr
Subject: helOoooOo
warning
.
Then you open a connection to the SMTP server of Live.com:
s = smtplib.SMTP("smtp.live.com",587)
that connection gives an object knowing the details of where you're connecting to, and offers methods you can apply on that connection. The methods are actions you can do on it, i.e.:
You say "hi!"
s.ehlo()
Then, you start encrypting the communication:
s.starttls()
it is doing the "encryption handshake" protocol on your smtp connection, so that further communication is now "secure", i.e. nobody can read your password by looking at the packets between your computer and the server.
You can read more about it there:
https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.starttls
And you can read its source code here:
https://hg.python.org/cpython/file/3.4/Lib/smtplib.py#l654
You say "hi!" again, not only because you're very polite, but because you switched from unencrypted to encrypted, your mail server is expecting you to say hi again:
s.ehlo()
You identify yourself, so that live.com knows that you're who you're prentending to be:
s.login('example@hotmail.fr', 'pass')
so there you want to change for your actual account's email address and password
Then, you send your mail to/from example@hotmail.fr:
s.sendmail("example@hotmail.fr", "example@hotmail.fr", msg.as_string())
In the first part of my answer, I told you about the "SMTP" protocol, where you have to say who you are and who you want to talk to? well this is what you're doing here. This method will translate into:
MAIL FROM example@hotmail.fr
RCPT TO example@hotmail.fr
And then, your mail object is a python "instance" that has no sense for your smtp server. You have to have it translated from the form that your mail server can understand, which is full text. You can have a look at what it is by doing in the python REPL:
python
>>> import email
>>> msg = email.message_from_string('warning')
>>> msg['From'] = "example@hotmail.fr"
>>> msg['To'] = "example@hotmail.fr"
>>> msg['Subject'] = "helOoooOo"
>>> print(msg)
<email.message.Message instance at 0x10dcb1560>
which means "python knows about an object called msg, which is made thanks to the class email.message.Message and lives at the memory address 0x10dcb1560".
But if you do the following, then you'll get the mail you'll send:
>>> print(msg.as_string())
From: example@hotmail.fr
To: example@hotmail.fr
Subject: helOoooOo
warning
Which is what actually you give to the SMTP handler instance that takes care of sending it properly.
And because you're polite, you're telling the server you're leaving by saying "bye", instead of leaving silently.
s.quit()
All that being said, I can only advice you to open a few books, take some online courses and most importantly do practice, starting from the simple stupid and boring examples to stuff way more complex. Start first by doing textual interfaces before doing graphical ones which are a bit more complex.
Here are a few pointers:
You might want to follow online courses as well:
just to give a few ones.
As your code suggests, it looks like you might be french, then I can only advice you to read the excellent book on algorithmic written by a former teacher I had, which is teaching programming principles and thinking without being tied to a programming language:
HTH