Implementing SMTP Protocol in Python (Amazon Price Tracking bot)


In the last post I started working on a bot that would track live prices from the Amazon server and would send a mail to the user indicating the price drop. In this post I am going to explain how to configure our bot to send a mail whenever there is a price update of a desired product.

We will configure our bot to send mail using SMTP, which is nothing but a simple mail transfer protocol that works on the application layer of the networks OSI model, it uses TCP (Transfer control protocol, a connection-oriented ) as the underlying protocol which ensures that mail is delivered to the given e-mail address. SMTP in itself is a pretty interesting and vast topic with variations in it like POP3 and IMAP, we will restrict this discussion to implementing of SMTP on python.

Steps to implement SMTP in Python

  1. The first thing is to import the SMTP library to your python environment so that we can make use of all the functions provided by SMTP, this can be done by using the following code –
    import smtplib
  2. We now need to establish the connection with our SMTP mail server which is done using the function  smtplib.SMTP(‘smtp.mail-provider.com’, port-number) , here you can use any mail provider like gmail to extablish smtp connection.
    server = smtplib.SMTP('smtp.gmail.com', 587)
  3. This is followed by commands to do the initial handshake and to make our connection secure using STARTTLS. This is done using the following code –
    server.ehlo()
    server.starttls()
    server.ehlo()
    
  4. After making a secure connection with the server we can now send our details like username and password to authenticate our account on the mail-provider used, which is gmail in this case
    server.login('username@gmail.com', 'password')
  5. After following these initials step we can now customise our mail contents according to our needs and include subject, body which are displayed through a message. Our amazon bot uses this mail service to send product details so we have included title of the product and the link to the product in our mail
    subject = "AMAZON PRICE UPDATE" +title
    body = "HEY THERE IS A PRICE UPDATE IN YOUR PRODUCT" +title+ "link" +URL
    msg = f"Subject: {subject}\n\n{body}"
  6. The final step is to invoke sendmail( “from-mail”, “to-mail”, msg) function to send our mail to the given “to-mail” address from “from-mail” address along with the message. Now we can also quit our SMTP server.
    server.sendmail('rounak2k@gmail.com','rounak2k@gmail.com',msg)
    server.quit()
    

This is the way to implement SMTP on your python environment, we have used this as a separate function in our amazon price tracking bot and you can call this function given below in any kind of python program to send mail using SMTP protocol and hence it’s application is not just limited to this program

def send_mail():
print("Sending mail.........")
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('mail-id@gmail.com', 'password')
subject = "SUBJECT OF YOUR MAIL"
body = "BODY OF YOUR MAIL"
msg = f"Subject: {subject}\n\n{body}"
server.sendmail('from-mail-id@gmail.com','to-mail-id@gmail.com',msg)
server.quit()
print("Mail sent successfully")

That’s all on this article on how to use SMTP in Python. Follow me to get more posts on interesting programs and how-to guides.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.