In an increasingly digital world, automation plays a vital role in enhancing productivity and streamlining communication. Today, I'm excited to share a Python script that automates sending emails and SMS messages, making it easier than ever to stay connected. Let’s dive into how it works!
Overview of the Script
This script leverages two powerful libraries:
smtplib: A built-in Python library used to send emails through an SMTP server, specifically Gmail in this case.
Twilio: A cloud communications platform that allows you to send SMS messages easily through their API.
Key Features
Send Emails: Utilize your Gmail account to send personalized emails with ease.
Send SMS: Reach out via SMS using the Twilio API, ensuring your messages get to the intended recipients quickly.
Code Walkthrough
Here’s the complete code for automating email and SMS communication:
import smtplib
from twilio.rest import Client
def send_email():
my_mail = "your_email@gmail.com" # Replace with your email
passcode = "your_app_password" # Replace with your app password or Gmail password
# Set up the SMTP server
connection = smtplib.SMTP("smtp.gmail.com", 587)
connection.starttls()
try:
connection.login(user=my_mail, password=passcode)
mail_content = "Subject: Trip on This Weekend:\n\nHey, aao kbhi haweli pe"
connection.sendmail(from_addr=my_mail, to_addrs="recipient_email@gmail.com", msg=mail_content)
print("Email sent successfully.")
except Exception as e:
print("Something went wrong while sending email:", e)
finally:
connection.close()
def send_sms():
# Your Twilio Account SID and Auth Token
account_sid = 'your_account_sid' # Replace with your Twilio Account SID
auth_token = 'your_auth_token' # Replace with your Twilio Auth Token
# Create a Twilio client
client = Client(account_sid, auth_token)
# Send a message
try:
message = client.messages.create(
to='+recipient_phone_number', # Destination phone number
from_='+your_twilio_phone_number', # Your Twilio phone number
body='Hello, this is a test message!'
)
print("SMS sent successfully:", message.sid)
except Exception as e:
print("Something went wrong while sending SMS:", e)
if __name__ == "__main__":
send_email()
send_sms()
How It Works
Sending an Email:
The
send_email()
function sets up a connection to Gmail's SMTP server usingsmtplib
.It logs in using your email and app password, then sends a structured email with a subject and body.
Sending an SMS:
The
send_sms()
function uses Twilio's client to send a text message.You’ll need to provide your Twilio Account SID, Auth Token, and the numbers involved.
Usage Instructions
Set Up Your Environment:
Ensure Python is installed on your machine.
Install the Twilio library using:
pip install twilio
Configure Email Settings:
- Replace placeholders in the
send_email()
function with your actual email credentials and recipient address.
- Replace placeholders in the
Configure Twilio Settings:
Sign up for a Twilio account and obtain your Account SID and Auth Token.
Replace placeholders in the
send_sms()
function with your Twilio credentials and recipient phone number.
Run the Script:
Execute the script using:
python your_script_name.py
Conclusion
This Python script provides a straightforward solution for automating communication tasks. By integrating email and SMS functionalities, you can keep in touch with friends, family, or colleagues without the hassle of manual messaging. Embrace the power of automation and improve your communication efficiency today needs!