Python Email Automation Tutorial: Sending HTML Emails with Python and Outlook

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] thank you hello there and welcome to this tutorial in this tutorial I'll be walking you through on how to send email with python and Outlook so here I've created a folder send mail and I have opened it folder on subline so you can choose to use your preferred editor for this so right on my folder I'm going to right click and create a new file this file I'm going to call it send mail.py so this is going to be a python file so before sending mail or proceeding with this tutorial it is assumed that you have python installed on your system so if you haven't installed python make sure you install the latest version of python and as well you have added python to your system parts so the first thing I'm going to do I'm going to import a module so I will say import and the module I want to import is smt believe SM TB leave so the SMTP lib module comes with a standard installation of python so you don't need to download it separately so once you have python installed all you just need to do is to import this SMTP lip module next I'm going to import a class and this is going to be from email dot meme m-i-m-e dot text and I'm going to import the class meme text and this module is used for sending a plain text message so next I'm equally going to import another module which is meme from email dot meme dot multi parts and I'm going to import the class meme multi parts and this module is responsible for sending both plane and HTML messages next I'm going to define a function and let's call it send underscore email and then this function is going to take some parameters I'm going to take sender underscore email next is going to take sender underscore password next this is going to take recipient recipient email and then the subjects and finally the message and then we enter our colon and you can see that my console has been indented so anything will type here is going to represent the body of this function so right here I'm going to make a comment so let's create a multi-part message objects so I'm going to create a variable MSG and this will be called to the class and in this class I will pass in alternative and what this basically means is that this is going to create an instance of the main multi-part class and then the alternative simply indicates that this is going to be both a plain text and HTML so next I will say MSG from equal to Sender email and this will basically set the from header to the sender email next I'm going to set the tool header I'm going to set it to the recipient email just like this and then this subject header I'm going to set this to subjects next I'm going to create both plain text and HTML version of this email and the reason is because most email clients support just the plain text and other supports both the plain text and HTML so for that we are going to create two versions so I'm going to make a comment create boats plane and HTML version so the email so I can save part 1 to Part One is going to equal to the meme text and I'm going to pass in the text as a parameter and then plane and this is going to be part two and this will be equal to meme meme text and then this will be HTML then HTML and before that we need to create some sample text and then a sample html text also so just before that we're going to create the variable text and this will be equal to so let's say this is a sample plane text and then let's create another variable HTML and this we're going to Define some HTML tags so I'm going to concatenate this F I'm going to create some HTML so HT HTML and then I'm going to close this HTML time and inside the HTML tag I'm going to create a body tag and then I'm also going to close the body tag and then within the body tag I'm going to Define H2 and then I'm also going to close the H2 so then in between the H2 the variable message so our message is going to go in between these tags and then so here the text is passed on to the meme text object and assigned to the variable part one and then the HTML variable is being passed to the mean text object and assigned to the part 2 variable so now we can attach the pattern and part 2 to the message object so MSG dots attach and I'm going to attach the part one and then I'll do the same for the part two dot attach then part two next we are going to create our smt server settings for Outlook so we are going to be using Microsoft Outlook email clients so feel free to check out Gmail and other email clients let's make some comment SMTP server settings so here I'm going to create a variable SM CP underscore server so and this will be equal to SMTP hyphen Mill dot Outlook .com and then next we are going to set the port so we say s MTP ports and the ports used for transmission of secured email is 587. so next I'm going to create a try block try and I'm going to make a comment create create a secured SSL slash TLS connection so I'll create a variable server is equal to SM TP lib dot SMTP and then I'm going to pass on the SMTP server and then the SMTP ports and basically what we have done here is that we have created an instance of the SMTP class which is coming from the SMTP lib module and we have passed in two parameters which is the SMTP server coming from here and then the SMTP ports so next so next we're going to say server server Dot stats TLS connection so this is going to start the TLs connection next we are going to login to our Outlook email so login to your Outlook email so here you need to specify your Outlook email and then your Outlook email password so here I'm going to say server server.login so basically we are using the login method of the server object created earlier so this is going to take two parameters and it's going to take the sender email so which is email coming from you as the sender and then it's going to take your the sender password or your password and finally we are going to send the email send email so call This Server object with a method of send email send mail so the send me method will basically send the email for us so here we are going to assign the sender email comma and then the recipient email which is a receiving email we equally specify the message MSG as strange um so the content is going to be converted to a strange then so what we can do we can print to the console if the email was sent successfully so we can see email sent successfully now otherwise we can handle an exception in case we encountered an error so say accept and exception will be SMTP lib dot s m t p exception as e so now that we have referenced it as e so hopefully want to print out the error so we can see you can say error sending email so we can concatenate the error so St Arrow e and then we're going to have a finally block which is going to close a connection so this will close connection so we say server the server object dot the quit method finally we are going to pass in or specify our values for the sender email I'm going to specify my email so this is going to be coming from my email so you can specify yours I'm entering my email my Outlook email viewing at outlook.com so yours is going to be different so you're going to enter your actual email address and then your password so next I'm going to enter my email password so send that password will be equal to my password so I'm basically hiding this for security reasons just know that you're going to specify your sender password as your email password then the recipient who is receiving this email so I'm going to specify my Gmail so I want this message to go to my Gmail so it's going to um no see you see let's see cards gmail.com then the subject of this email so this subject I'm just gonna say um let's just say hello from python from python email so let's say this is our subject and then the message I'm gonna say this email was sent via a tutorial on how to send email with python and Outlook and then finally we are going to pass this to our function so we have a function send email so I'm going to pass this to that function send email email so I want to make sure I'm typing the correcting send email okay so I'm going to specify this sender email the sender password your recipients email the subject and then finally the message and here we go so we have finished writing our code for sending email so I'm going to save this file and then make sure that you have an internet connection so I'm going to connect to my network so make sure you save your file so now I'm going to navigate to the folder send mail and then here it is so I'm going to right click and then open in terminal so if you are using Windows 11 so you can open this in terminal or you can simply open the folder and then click on this to copy your parts so once your part is copied then you can fire up your terminal and then you CD to that part but because you are already on the path so you can see we are running out the parts I'm going to I'm not going to do that so if I LS so you can see we just have the send email so before we send this email I just realized I don't have access to this recipient email for now so I'm just going to enter another Gmail Jesus Jesus at gmail .com okay so I'm going to save this file and then go back to my terminal I'm going to run or execute this by saying python followed by the name of your file so send mail send mail.py so if I hit enter so let's wait for a success message oops it says attribute mean multi-pass object has no attributes as strange or did you mean as strange so we had some we had a typo right here so let's go back and make correction so this is as strange just like this so if I save this and then let's go back and simply clicking on my up Arrow key I can populate the previous code that we wrote so if I hit enter and let's check this again and here we go email sent successfully so let's check our email so if I go to Gmail wow and here we go so we have just received our email so you can see this email was sent via a tutorial on how to send email with python and Outlook so if I click on the show details Arrow so you can see so this and from this Outlook so you can see the subject made by signed by and all of that and you can see standard description security so this is how simple it is we can send email by utilizing the SMTP lib module in Python and then Outlook SMTP in future lectures I'm going to show you how to create a web form and send email using python thank you for watching this video I hope you found it informative and useful if you did please give it a thumbs up leave a comment below and consider subscribing to the channel for more content on digital technology and programming skills stay curious and I will see you in the next video
Info
Channel: NextTechGem
Views: 2,804
Rating: undefined out of 5
Keywords: Python email automation, Python smtplib tutorial, Sending HTML emails with Python, Python email automation tutorial, Outlook integration with Python, HTML email templates in Python, Automating email sending with Python, Python email library, Sending formatted emails with Python, Python email automation with smtplib and Outlook, Cross-platform email automation, Email automation on Windows, macOS, Linux, Python email automation for all operating systems
Id: ds9SNPfelw0
Channel Id: undefined
Length: 21min 3sec (1263 seconds)
Published: Sat Jun 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.