How To Send Email In Python | Smtplib Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Electronic mail is a popular communication  medium to send text, images, videos and files   to hundreds and thousands of people around  the world. And as pythoneers ourselves,   we may come across the question,  can we send emails using python ?. So in this video, I am going to show exactly how   you can programmatically send emails in  python. If you are into python like me,   you can join our community “All about  python” by subscribing on YouTube,   following on Instagram or joining our newsletter  through our blog site allaboutpython.tech. So without any further ado, let’s begin. The task of sending mails in python  can be achieved using a few libraries,   if you want to send a simple text mail,  without any HTML content or image,   you can just use the smtplib package. But if you  want to send mails with HTML content or images,   you can use the email package along with smtplib. For this demo, we will use both smtplib and email   package to send mail. We will discuss two  scripts, html mail.py and text mail.py,   each having code using and without  using email package respectively. For all the viewers following this tutorial  for learning, I would recommend using an   outlook account to send mail rather than  a gmail account because of two reasons: Unlike gmail accounts, you don’t need to  specify a phone number to create the account.  With the updated gmail settings, it's much more  difficult to send mail from a gmail account,   compared to sending mail from an outlook account. I have already created a sample outlook  account that will send mail to the   allaboutpython channel’s official  mail ID using the python script.   With the setup instructions clear,  let’s start writing some code. I have my vscode editor opened here and i   will start by creating a file  and naming it “text_mail.py”. Within the file, I will first  import all the required modules. We will import getpass to get the outlook  account password from the user using the console.  We will import smtplib to send  email through the mail server. Next we will create a few variables. Create a variable named HOST with value  “smtp-mail.outlook.com”. This is the SMTP   server domain name of outlook. If you are  using another provider like gmail or yahoo,   then you would need to change this value.  You can view the list of providers and   their associated smtp server domain name. Create a variable named PORT with value 587.   This the port at which we will connect to the SMTP  server. Most of the serves use port 587 for smtp,   but you can view in this list which  providers use the alternate 465 port.  Create a variable named FROM_EMAIL with value  “allaboutpythontestacc2@outlook.com”. This   the test email account I am using to send  the mail. You can change this value with   the email ID of the account you are using. Create a variable named TO_EMAIL with value   “allaboutpythoninfo@gmail.com”. This is the  allaboutpython channel’s official email ID.   We will send our test email onto this ID. You can  change this email id to send mail to another ID.  Create a variable named password and get its  value from getpass.getpass() function. Within   this function, we will pass a string value.  The function will print the prompt message that   we have passed here and will take password from  user. The password will not be visible on console,   but will be entered and understood by the  program, and saved inside the password variable.  Create a variable named message which will  contain the message we need to send through   the mail. Now there’s a specific format  you need to set to send the message. The   first line of the message should start with  Subject: followed by the subject of the mail   you want to set. From the next line, you can  write the body of the message in plain text. With all the variables created,  let’s start connection to the server. We will create a variable named smtp which will  store an object of smtplib.SMTP class. The class   will take two required parameters, the host and  the port, which we have already created above. Next we will call the ehlo method of smtp  object. This function will ping the server   and check if the server is up and running  or not. This function will return a tuple   containing the status code of response and  the response message received from server.   We will also add a print statement  just to view how the response looks. Next we will call the smtp.starttls function. This  function will establish tls connection between   our system and the server. This is especially  important to perform if you want to send your   mails securely over the internet. This function  will also return a tuple containing the status   code and the response message. We will again  add a print statement just to view the response. Next we will login to server using  our account’s email ID and password.   We will call the smtp.login() function  and pass the email ID and password here.   Just like the previous functions, this function  will also return a tuple containing a status   code and a response message. We will again  add a print statement to view the response. After successful login, we can send  our mail using smtp.sendmail function,   The function will take in two parameters  from email, to email and the message.  Finally, to close the connection with the  server, we will call the smtp.quit() function. Now let’s the run the script and see how it works. As you can see, the script first asked me  for the password, which i entered here.   Next it echoed the server and displayed  its information. Next it displayed the   information on starting tls connection.  And finally it displayed the information   on logging on the server. You can add more print  statements to view more information if you like. Now if I open allaboutpython gmail  account, you can see that we have   received a mail from our test account. On  opening the mail, we can see the subject   we wrote here and the message we wrote here.  This is how you can send a simple text mal. For the next step, we will create a script   capable of sending html content  on mail using the email package. We will start by creating a new python  file and naming it html_mail.py. We   will also create a mail.html file which  will store the html mail we want to send.   For this we will use a simple html file with  all the necessary tags like html, head, body   etc. and we will add two paragraph tags and  write a simple message here. If you want,   you can add more content and css  here to make it more attractive. Within the html_mail.py file, we wil start  by importing the packages. The getpass and   smtplib package will remain the same, but we  will also import two modules in email package,   namely MIMEMultipart and MIMEText.  MIMEMultipart is used to create the   email message object and MIMEText is  used to add html content onto the mail. Just like the previous script, we will  create few variables like host, port,   from_email, to_email and password. You can  change these values according to your needs. Next we will create a variable by the name  message which will contain the object of   class MIMEMultipart. We will pass in a  string parameter with value ‘alterative’,   which refers to mime type mime/alternative. Within the message object, we will set some data  regarding the mail like subject, from, to, cc, bcc   etc. For this, we will treat this object like  a dictionary, and add key value pairs to it.   Here i have specified the subject of the mail,  the from email address, the to email address,   the email addresses to be added in cc and  finally the email addresses to be added in bcc. Next we have written a block of code to  just read the html content that we have   saved inside mail.html file. We will  save the content within html variable. Next we will create new variable html_part and  store the object of class MIMEText in it. MIMEText   class is used to convert paintext content onto  the correct format based on the mime type passed   onto it. In this case, we are passing the html  content here and specifying the mime type as html. Once the html part is ready, we will  simply attach the html content onto   the message by writing message.attach html_part. The rest of the code remains similar to the one  we had written in the previous python script.   We create an smtp class object, echo the server,  start tls connection, login using account details,   send the mail and then quit. The one thing  that has changed is in the send mail code. In this line, we previously just passed  the message in plain text. But since we   are using the MIMEMultipart class object,  we will call the as_string function to   get a plain text string format of the mail,  which we will pass to the send mail function. Let’s save and run this  script to see how it works. As you can see, we got a console  message similar to the previous one,   but if we check the email content, we  can see that the message now has the   html content which we write  within the mail.html file. With this, we end today’s  tutorial, hope you liked it. If you did, don’t forget to  subscribe to this channel and   check out some of the other videos of  this channel on other useful topics. If you are more of a reader type,  you can also check out our blog site,   we have created a blog on the same topic,  along with blogs on other interesting topic. With this, its time for me to go, this  is Vishesh Dvivedi signing off.
Info
Channel: All About Python
Views: 21,649
Rating: undefined out of 5
Keywords: how to send email in python, how to send email using python, how to send email with python, how to send an email using python, sending an email using programming, python email sending, python send email, python email, smtp, smtplib, email, smtp package, smtplib package, email package, email python script, email python outlook, send email python, smtp email python, email python tutorial, python email script, send email automatically python, itsallaboutpython, allaboutpython
Id: S465v4mWsRg
Channel Id: undefined
Length: 11min 19sec (679 seconds)
Published: Fri Feb 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.