Automate Gmail using Python

Automate Gmail using Python

Introduction

Efficiently managing our Gmail inbox is vital in today's fast-paced digital landscape. Python enthusiasts can take advantage of the powerful smtplib library to automate their Gmail workflows and optimize productivity. In this article, we will explore how leveraging Python with smtplib can revolutionize the way we interact with Gmail, streamlining communication and organizing our inbox effectively.


Prerequisite

Turn on 2-step verification & get app code

  1. Sign in to Gmail: Ensure you are signed in to your Gmail account using your email address and password.
  2. Go to Google Account Settings: Click on your profile picture in the top-right corner of the Gmail interface and then select "Manage your Google Account." This will take you to your Google Account settings page.
  3. Navigate to Security: In your Google Account settings, click on the "Security" tab on the left-hand side.
  4. Turn on 2-Step Verification: Under the "Signing in to Google" section, find the "2-Step Verification" option. Click on "Get started" and follow the prompts to set up 2-step verification.
  5. Verify Your Identity: Google will ask you to enter your password to verify your identity.
  6. Choose 2nd Step Verification Method: Select a method for receiving verification codes. You can choose between "Text message," "Voice call," or "Google prompt" to get codes on your phone.
  7. Receive Verification Code: Once you've selected a method, you'll receive a verification code on your phone. Enter the code to confirm and activate 2-step verification.
  8. Generate App Password: If you want to use Gmail with applications that don't support 2-step verification, such as email clients, you'll need to generate an app password. In the Security settings, click on "App passwords," select the app and device type, and click "Generate." Use this app password in the application where you can't use 2-step verification.


1st Method

Smtplib Library

  • smtplib is a built-in Python library that allows for simple mail transfer protocol (SMTP) communication, facilitating email functionality within Python scripts.
  • It enables sending email messages from your Python program to an SMTP server, which then forwards the email to the recipient's mail server.
  • smtplib provides a straightforward interface to send plain text or HTML-formatted email messages.
  • With smtplib, you can easily connect to an SMTP server, authenticate using credentials, and send emails programmatically.
  • Install smtplib using pip command

pip install secure-smtplib        


Automate Gmail using python code

import smtplib
host = "smtp.gmail.com"
port = 587
from_mail = "[email protected]"
to_mail = input("Receiver gmail : ")
password = "paste the 16 letter code you copied without space"
msg = """Subject: Testing Mail 

Hi Bro,
This email is sent using python
Thanks"""

smtp = smtplib.SMTP(host, port)

status_code, response = smtp.ehlo()
print(f"[*] Echoing the server: {status_code} {response}")
status_code, response = smtp.starttls()
print(f"[*] Starting TLS connection : {status_code} {response}")
status_code, response = smtp.login(from_mail, password)
print(f"[*] Logging in : {status_code} {response}")
smtp.sendmail(from_mail, to_mail, msg)
print("Mail Sent Successfully!")
smtp.quit()        


2nd Method

Pywhatkit Library

  • Pywhatkit is a Python library that simplifies WhatsApp automation.
  • It enables users to send WhatsApp messages programmatically using Python.
  • The library allows for the scheduling of WhatsApp messages at specific times.
  • It can play YouTube videos by providing the video's URL or search keywords.
  • Pywhatkit is easy to use with straightforward functions, making it suitable for beginners and experienced developers alike.
  • Install pywhatkit using pip command

pip install pywhatkit        


Automate Gmail using python code

import pywhatki
pywhatkit.send_mail("[email protected]", "your passcode e.g ubhfierxxlnaykcw", "Subject", "Hello Pra!, Are you all right?", "your friend's mail you want to send mail")t        


Conclusion

  • Pywhatkit's send_mail() function provides a simple way to send emails programmatically using Python.
  • By using this function, you can automate the process of sending emails to desired recipients.
  • The send_mail() function requires your Gmail credentials (email and passcode) to authenticate and send the email on your behalf.
  • It allows you to customize the email subject and body with the message you want to convey.
  • With Pywhatkit, you can streamline email communications and integrate email functionalities into your Python applications.
  • Remember to use this function responsibly and keep your Gmail credentials secure to maintain the privacy and security of your account.


Feedback/queries

I hope this article will be useful for you and that you learned something new Please, feel free to drop any questions in the comments below. I would be happy to answer them.

Thanks for reading???

要查看或添加评论,请登录

Chandan Kumar Singh的更多文章

  • NumPy Image Creation

    NumPy Image Creation

    Introduction Are you fascinated by the endless possibilities of Python programming? Ready to delve into the realm of…

  • Sunglass Attach using CV2

    Sunglass Attach using CV2

    Introduction In this LinkedIn article, we'll explore how to use Python's OpenCV library to automatically add stylish…

    2 条评论
  • Multiple Video Streaming Concurrently

    Multiple Video Streaming Concurrently

    Introduction In the realm of computer vision and image processing, OpenCV (Open Source Computer Vision Library) stands…

  • Magic of Speech-to-Text Transcription

    Magic of Speech-to-Text Transcription

    Introduction Are you ready to dive into the incredible world of Speech-to-Text Transcription? ?? This technology is…

  • Running Docker Inside Docker

    Running Docker Inside Docker

    Introduction Docker has revolutionized how we develop and deploy applications, but what if you could take it a step…

  • CURD Operation on Amazon S3 Bucket

    CURD Operation on Amazon S3 Bucket

    Introduction Amazon Simple Storage Service (S3) is one such prominent cloud storage service offered by Amazon Web…

    1 条评论
  • Launching an Amazon EC2 Instance Using Python and Boto3

    Launching an Amazon EC2 Instance Using Python and Boto3

    Introduction Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the…

    1 条评论
  • Inside docker container install Firefox and start Firefox

    Inside docker container install Firefox and start Firefox

    Introduction Docker has revolutionized the way we deploy and manage applications, and now, with just a few simple…

  • Install Docker on Local System

    Install Docker on Local System

    Introduction Docker has revolutionized the way we build, ship, and run applications. Its lightweight and…

  • Power of Concurrent Python Programming

    Power of Concurrent Python Programming

    Introduction In this article, we'll embark on an exciting journey to explore the world of concurrent Python programming…

    1 条评论

社区洞察

其他会员也浏览了