Simple Mail Client in Python
Skills:
AI Pair Programming80%
Key Takeaways
The video demonstrates building a simple mail client using Python, showcasing how to send test emails to an actual email address.
Full Transcript
[Music] what is going on guys welcome to this python tutorial for networking in today's video we're going to learn how to send mails with python so we're going to build a script that automates the process of sending a mail so we're going to learn how to send ordinary text with a python script via mail we're going to learn how to manipulate the header and we're also going to learn how to add attachments to mails so let us get right into it now let's get started with the code the first thing we need to do is we need to import the smtp library of python smtp is a protocol that we're going to use to send mails because what we need to do here is we need to take our script and log into an existing mailing account so make sure you have one you log into an existing mailing account and then you use the smtp protocol with your python script in order to send mails from that account to other accounts so you cannot just send mails directly from the python script you use the python script to log into your existing mail account and then use the smtp protocol to send mails from there so this is what we're going to do and for this we need to define a server the server is defined by smtplib.smtp and here you have to specify the smtp server and a port and the port we're going to use is 25 for smtp and the smtp server address is usually something that you can find out by googling so you just type in smtp server gmail smtp server yahoo whatever provider you're using so smtp.gmail.com for example is the one for google it's a very uh basic format actually you always write smtp dot most of the time it's just going to be dot whatever your provider's domain is uh in my case i'm using a an austrian uh provider here where i'm hosting my website which is world4you.com so smtp.world smtp.world4you.com is the address of my smtp server here or of the smtp server of my provider and once we have that we're just going to start it to start the whole service here by calling the ehlo command so this is the function you need to call to start the whole process and now the next thing that we need to do is we need to log into the account so you have your smtp server at gmail or whatever it is and then you just log into your account and to do that of course you need the information first you need the email and the password and i would never recommend saving the password clear text in your um script because what you essentially need to do is you need to say server.login and then you know you have your mail here mail at mail.com whatever and then your password here password123 whatever it is uh you can do it like that if you want if you're just using the script with a crap email account that you don't need just for experimentation you can do it like that but i would not recommend it what you should usually do is you should save your your password encrypt it in a text file then load that text file decrypt it and then use that password for this tutorial i'm just going to save the clear text password or actually save the clear text password already in a text file and i'm going to read it here so that you as a viewer are not able to see it so what i'm going to do is i'm going to see with open i'm going to open a text file stream uh with openpassword.txt in readingmode sf and we're going to say password equals f.read so now i have the password that i saved in this password txt file i now have it in the script and i'm going to say server dot login and now you have to specify your mail so whatever maybe yours is alex gmail.com whatever it is in my case i created a separate email for this tutorial so mail testing at neural9.com and the password is the one we just loaded so this is how you log into your server into the smtp server into your mail account and now we can start creating the message now before we can start creating the mail message what we need to do is we need to import some additional libraries because what we're going to do is we're going to create a message a mail consisting of multiple parts of attachments of messages of headers and for this we need to import some additional libraries so let's start with from email import encoders which is something that we're going to need later on this video not uh not yet and also let's say import not frozen set from email import um mime text which is the ordinary text that we're going to use um actually from email.mime.txt import mimetext then from email dot mime dot base we're going to import mime base which is what we're going to use for the attachment and then for the whole thing we're going to use mime dot multi-part import multi-part that's it and now we can start creating the message using these libraries here so the first thing that we're going to do is we're going to define the message as a my multi-part and we're going to define the header and the header is essentially consisting of a from to and a subject so we're going to say message equals or actually message from equals because message can be treated like a dictionary we can just say okay message from equals and here we're going to say neural nine or your name whatever you want you can put here um all kind of creative stuff and we can say two and here you usually just specify the target email in my case i'm going to use a 10 minute mail but i'm not going to use the 10 minute mail domain because the 10 minute mail domain uh for some reason is not able to receive attachments i tried at least when i was experimenting with my script i couldn't receive attachments there so i'm going to use a german spam mail provider which is providing 10 minute mails and the domain is called spammel.de so i'm going to just say mail testing at spamo or actually this is probably not that that intelligent because my mail is already mail testing let's say uh test mails at spammel.de you can use a different one you can also use your existing email a real email i'm just going to use a spam mail for this video then we're also going to say subject equals um whatever you want it to be just a test this is essentially what you're going to see once you receive the mail in the subject and uh that's essentially it now you can specify a text for your message and i usually do this in a file so we're going to create a new text file here we're going to say message dot txt and here we're going to just write our mail message hello world this is a mail sent with python uh subscribe to neural nine always got to do this as we're used to it to it already from the other videos kind regards uh neural nine there you go then you have the message here and what you do then is you just load the message the same way you loaded the password if you loaded it so with open message dot txt in reading mode s f we're going to say message equals f dot read that's essentially it and this is something that you just need to attach to the message object here so message.attach which is not the attachment we're going to add the attachment later on so we are not adding oh sorry not.text.txt you're not adding the um txt file to the mail you're adding it as a text and we're later on also going to add an image as an attachment so what you need to do now is you need to attach mime text and you're just going to pass the message here and you're going to attach it as plain text so this is how you add a header and a text to your message object now one more thing that want to do before we send the actual message is you want to attach an image to it so in this case i downloaded the royalty free image from pixabay you can also use google images if you're just using it for your personal experimentation you're not going to get sued for that so i just picked this coding.jpg file from pixabay because i'm doing a video and i shouldn't be using copyrighted images here however you get this image put it into the same directory and then you specify the file name file name in this case coding.jpg or essentially the file path if it's somewhere else you can also specify the whole path and then we're going to open up attachment equals open we're just going to open a file stream of this file name and now we're going to open it in reading binary so we're not going or reading bytes we're not going to open it up in reading text mode but in reading byte mode because we're using um we're working with image data here not with text data anymore uh so the next thing is we're going to create a payload object so we're going to say p equals and now we're going to use this mime base object so we're going to say minebase and here we're just going to start a so-called don't ask me what it stands for it's a application and then it's called an o c t e t stream which is essentially the scene uh the stream that we're going to use to uh process this image data here and then we're going to set the payload of this payload to attachment to the file stream dot read or actually like that attachment.read i'm not sure if we even need those parentheses here i don't think so so attachment.read and this is what you set as the payload you just read the content of this attachment with reading byte modes uh and you set the payload now the next thing you want to do is you're now going to use the encoders so we're going to say encoders and code base64 so we're going to encode the image data that we just read and that we set as a payload uh we're going to encode p and then we're going to add a header to p so we're going to say p dot add header content disposition and we're going to use an f string here to say application or attachment sorry attachment semicolon and then file name and now of course we add the file name here which is just file name and this is how you add the attachment of course now the payload the attachment itself has to be attached to the message so i'm going to say message.attach yeah message.attach p and that's how you add the actual payload to the message now the last thing we need to do is we need to say text equals message dot um as string so we finally get the whole thing as a string and this string can then be sent by the server so we're going to say server dot send mail and we're going to send it from mail testing at neural nine dot com to our target what was it test males test males dot at spamal.de and we're going to send the message which is the text now this is the whole script and we're now going to test it so now what i did is i created a spam mail on spammel.de it's german don't get confused by that but you can also use any other spam mail provider or you can just use a real mail as a target um in this case i just didn't want to use or show any of my personal emails so i used this spam test mails at spamo.te and now what we're going to do is we're going to run our script so we're going to say run and if we didn't make any mistakes this should now send the email without any errors wow we didn't get an exception i'm surprised to be honest and now let's wait for it and see if we get something here and you get it here just a test hello world this is a mail sent with python subscribe to neural nine kind regard to neural nine you can also see that avas or avest was checking this mail so if you don't want this message to appear just if you're using the same anti-virus software just turn it off and you can also see that there is an attachment in here coding.jpg and if i download it you can see that it's the exact image that we wanted to send so it worked the script worked you have neural nine at an unknown i don't know if we have the mail header here somewhere as well but if you're using a normal mail account this should appear as neural nine as uh or your mail not neural nine as in this case mail testing at in this case it says neural nine at unknown i don't know why it does that but essentially it works the subject is here the text is here as a normal text and then we also have the attachment so it worked perfectly fine and that's basically it so that's it for today's video i hope you enjoyed and hope you learned something if so let me know by hitting the like button leaving some comments in the comment section down below and also subscribe to this channel if you haven't done it yet to see more future videos for free other than that thank you very much for watching see you in the next video and [Music] bye [Music] you
Original Description
In this video we are building a simple mail client using Python. After that we use it to send some test mails to an actual e-mail address.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🖥️ My Coding Setup 🖥️
⌨️ Keyboard: http://hyperurl.co/neuralkeyboard
🖱️ Mouse: http://hyperurl.co/neuralmouse
🖥️ Monitor: http://hyperurl.co/neuralmonitor
🎙️ Microphone: http://hyperurl.co/neuralmicrophone
✏️ Drawing Tablet: http://hyperurl.co/neuraldraw
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →
🎓
Tutor Explanation
DeepCamp AI