How to Send an Email with Python
Skills:
Tool Use & Function Calling80%
Key Takeaways
This video tutorial by Tech With Tim demonstrates how to send a simple email using Python by connecting to an SMTP server, logging in with email credentials, and sending an email. The tutorial uses the smtplib and ssl libraries in Python.
Full Transcript
hey guys and welcome back to another YouTube video so in today's video I'm going to be showing you how to send emails from a python script this is actually extremely simple it's not going to take us very long but I'm just going to be showing you very basic emails so just like plain text emails with a subject and a message line if you want some more advanced emails with like HTML and like making them look nice I can do that but I need you guys to let me know in the uh comments down below so anyways let's get started so this function you guys aren't going to need but I'm just using it to read in my uh email and password from a text file so that you guys can't see it but obviously on your own computer you can just type out your username and password or your email and password when we get to that point I'm also importing SMTP lib and SSL this is what we're going to do to connect to the server so we can actually send the email so our first step is going to be to Define our username password and then who we're going to send the email to so I'm going to say sender is so sender password is equal to read creds Now read creds simply returns to me obviously my username and the password now you guys can just type out like sender equals and then just put your Gmail address here and then password is going to be the password to that Gmail account now I don't know how this works with two-factor authentication uh but if you're running into an issue with credentials it might be because your email account is two Factor authenticated and you can't connect to it from here uh so in that case I would say maybe just create like a new email account that you want to send it from or disable the two Factor authentication at least for this example okay so now we're just going to uh Define receive and this will be equal to the email that you want to send it to now in my instance I'm just going to actually send it to myself so to the sender so that I don't have to bother going into a new email account but for you guys you can literally just type any Gmail account or whatever other account you want in here and it should work as long as it's a valid address okay so our next thing is to Define our message and this is what is obviously we're going to be sending to the person so we're just going to do a multi-line string here and I'm just going to do this to Define that we're going to the next line and we're going to do subject colon and then whatever you want the subject to be so I'm just going to say python email tutorial and then in the main line so this is actually going to show up as the subject line for us this and then down here will be the message content so just say this is from python exclamation point and we'll just say Tech with Tim as like the little goodbye message or whatever okay so we have that so we have our sender receiver um message what else do we need we need to Define a variable now called context and what this is going to be is SSL doc creatore default context and we're going to use this in a second you'll see why I'll explain let me just move up here and then down here what we're going to do is we're going to actually connect to the server and send the email we're almost actually done this is super fast so we'll just say with and then SMTP dot or not DOT sorry lib do SM SMTP SMTP my bad guys _ SSL and then in here we're going to put our actual email server so in this case I'm using Gmail and I assume most of you will probably be using Gmail but if you're using like Hotmail you're going to have to find what that server is to put in here but I believe uh it's just smtp.gmail.com so this is the email server we're going to connect to and send email emails from now I don't know if you can use this with uh what do you call it other emails that aren't Gmail if that's your sender email uh but if you try it and it doesn't work then you'll have to find whatever this kind of SMTP thing for like Hotmail or Outlook or whatever it is email that you have okay so next thing I'm going to do is I'm just going to say port and then context equals context and this reminds me actually up at the top here we got to Define Port now so I'm just going to say Port is equal to four five six and this is just the standard port for SSL um if this for some reason doesn't work then that probably means that you will router is blocking the connection to SSL and in that case there's not really much you can do you can try to find another port on your router that uh accepts SSL or that's empty maybe but for now that's all I know is 456 so it works for me so we'll see if it works for you guys okay so after that um we've kind of connected now we'll just say this is as server and sorry one second we're going to say server. login so just simply logging into our credentials so to log in here I'm just going to say sender and then password and then we're going to say server. send email and then we're simply just going to put our sender which is who's sending the email Our receiver which is who's going to receive the email and then the message which is message and that should be about it I'm just going to add some print statements in here quickly so we get a little bit of output to console saying starting to send and then at the end here I'll just say print send email just so we know you know if something hangs here and we run into an issue or if it's taking a long time to send the email so uh yeah that's pretty much all we do Port 465 uh we just get your username and password again this is going to be like your sender is going to be your Gmail account um so the exact address and then password is going to be whatever the password is for that uh receive this is any email that you want to send it to message this is what you're going to be sending context we just need this uh to create a new SSL thing not going to explain exactly what that does and then we're just going to open up a connection to the email server log in with our credentials and simply send an email so let's run this now and see what happens uh no such fileer directory credentials. txt okay so give me one second while I create that directory um and then I'll be right back okay so I just realized I made a really small mistake and essentially my file was just in the wrong directory but I also tried to run it and realized that we have two issues that I need to fix uh the first one here is down here this originally said pass W I just had to change this to password uh so just add that org there because I forgot that I named this password and then here where it says send email we're just going to make this send mail uh I don't know why I added that e there but anyways that should be working now so if I run this we see starting to send and then send email you know that's great uh it's showing us this output but let me prove to you that it actually did send that by opening up the email address here so you can see right now it's actually well I mean you guys can't really see the time but it is 8:14 a.m. and you can see that we've just received an email that says test send email this is just whatever the email I'm using this is from Python and then it signs it with tech with Tim and if I go to my sent mail you can see that it was sent from this address and received here as well so that is essentially how you send an email with python really straightforward really basic if you guys have any questions or anything it's not working please let me know in the description below and with that being said I will see you again in a new video [Music]
Original Description
This tutorial will show you how to send an email with python. We will simply connect to a smtp server, login with our email credentials and send an email. This tutorial will focus mainly on sending simple emails but it is possible to do thing like embed HTML into your emails.
Source Code: https://techwithtim.net/tutorials/sending-emails-with-python/
◾◾◾◾◾
💻 Enroll in The Fundamentals of Programming w/ Python
https://tech-with-tim.teachable.com/p...
📸 Instagram: https://www.instagram.com/tech_with_tim
🌎 Website https://techwithtim.net
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/pr2k55t
📝 LinkedIn: https://www.linkedin.com/in/tim-rusci...
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
💵 One-Time Donations: https://www.paypal.com/donate/?token=...
💰 Patreon: https://www.patreon.com/techwithtim
◾◾◾◾◾◾
⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡
Tags:
- Tech With Tim
- How to send an email with python
- Python email
- Python Tutorials
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 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
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
How AI Learned to Render Photorealistic Worlds in Real-Time And What That Means for the…
Medium · AI
The Tool We Built to Measure AI Visibility Couldn’t Find Itself
Medium · AI
IA local vs ChatGPT para empresas: qué usar y cuándo
Dev.to AI
Positional Encodings — Deep Dive + Problem: Binary Cross-Entropy Loss
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI