Send Email Using Python - Aman Aadi

Sending Email Using Python: Automate Your Communication

Introduction:

In today’s fast-paced world, automation is the key to efficiency. What if you could automate the process of sending emails using Python? In this blog post, we’ll explore how to harness the power of Python to send emails effortlessly. Whether you’re notifying clients, updating team members, or simply staying in touch, sending emails programmatically can save you time and streamline your communication efforts. Let’s dive into the world of Python-powered email automation!

Understanding Email Automation with Python:

Automating email sending through Python offers a myriad of benefits, from sending notifications and alerts to distributing newsletters and reports. Python’s simplicity and versatility make it an excellent choice for streamlining email communication.

How would the python script work? It will take your and recipient email, also your google app password,  subject and message of the email. 

Requirements:

Ensure you have Python installed on your system. You can download the latest version from the official Python website (https://www.python.org/downloads/) and follow the installation instructions. Additionally, a code editor such as Visual Studio Code or PyCharm, will make coding and testing more convenient.

We’ll start by importing the smtplib module allows you to interact with Simple Mail Transfer Protocol (SMTP) servers, enabling you to send emails programmatically. Note that you’ll need to set “google app password” in your Gmail settings for this to work.

Python Script:

# importing module
import smtplib


# creating a function that take some arguments and send email
def sendmail(from_email, to_email, password, subject, message):
    with smtplib.SMTP("smtp.gmail.com", 587) as server:
        server.starttls()
        server.login(from_email, password)
        server.sendmail(from_addr=from_email, to_addrs=to_email, msg=f"Subject:{subject}\n\n{message}")


# taking input
if __name__ == "__main__":
    print("\n")
    from_e = input("Enter your email: ")
    to_e = input("Enter the email of recipient: ")
    app_password = input("Enter your google app password: ")
    e_subject = input("Enter the subject of the email: ")
    e_message = input("Enter the messade of the email: ")
    sendmail(from_email=from_e, to_email=to_e, password=app_password, subject=e_subject, message=e_message)
    print("\n")

Commands and Output:

Output of code of send email using python

Conclusion:

Congratulations! You’ve successfully created a Python script to send emails. With this automated email sender, you can streamline your communication efforts and stay connected with clients, colleagues, and friends. While this example uses Gmail’s SMTP server, you can adapt the code to work with other email providers as well. Always remember to handle sensitive information, such as passwords, with care and prioritize security in your email automation practices. Happy coding, and may your automated emails make your communication tasks a breeze!

Discover more from Aman Aadi

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top