How to Send an Email with Python

Tech With Tim · Beginner ·🧠 Large Language Models ·7y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This tutorial teaches how to send a simple email using Python by connecting to an SMTP server and logging in with email credentials. It covers the basics of sending emails using Python and demonstrates how to use the smtplib and ssl libraries.

Key Takeaways
  1. Import the smtplib and ssl libraries
  2. Define the sender and receiver email addresses
  3. Define the email message
  4. Create an SSL context
  5. Connect to the SMTP server
  6. Log in with email credentials
  7. Send the email
💡 To send an email using Python, you need to connect to an SMTP server and log in with your email credentials, then use the smtplib library to send the email.

Related Reads

📰
How AI‍ Learned‌ to‌ Render Photorealistic​ Worlds in Real-Tim‍e And‌ What Th‍at​ Means for the…
Learn how AI renders photorealistic worlds in real-time using NeRF, a coordinate network trained on a differentiable image formation model
Medium · AI
📰
The Tool We Built to Measure AI Visibility Couldn’t Find Itself
Learn how a tool built to measure AI visibility failed to find itself, and what this means for AI development and testing
Medium · AI
📰
IA local vs ChatGPT para empresas: qué usar y cuándo
Learn when to use local AI vs ChatGPT for businesses and how to choose the best option for your needs
Dev.to AI
📰
Positional Encodings — Deep Dive + Problem: Binary Cross-Entropy Loss
Learn about positional encodings and binary cross-entropy loss in deep learning, and how to apply them to solve problems
Dev.to AI
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →