The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a CLI for sending birthday messages

To build a simple CLI (Command Line Interface) for sending birthday messages, you can use Python with libraries like argparse for handling command-line arguments and smtplib for sending emails. Below is a basic implementation of such a CLI that sends birthday messages via email.

Prerequisites

  • Python installed on your machine.

  • An email service (like Gmail) for sending emails.

  • Ensure you have the necessary permissions (less secure apps enabled for Gmail or app password for 2-factor authentication).

Step 1: Install Required Libraries

You’ll need argparse and smtplib, both of which are included in Python’s standard library. If you want to send personalized messages, you can use jinja2 for template rendering, though it is optional.

To install jinja2, you can run:

bash
pip install jinja2

Step 2: Write the CLI Script

python
import smtplib import argparse from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from jinja2 import Template import datetime def send_birthday_message(email, name, birthday_message, smtp_server, smtp_port, sender_email, sender_password): # Create the message msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = email msg['Subject'] = f"Happy Birthday, {name}!" # Render message with Jinja2 template template = Template(birthday_message) message = template.render(name=name) # Attach the message msg.attach(MIMEText(message, 'plain')) # Connect to the email server and send the message try: server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # Secure the connection server.login(sender_email, sender_password) text = msg.as_string() server.sendmail(sender_email, email, text) server.quit() print(f"Birthday message sent to {name} at {email}") except Exception as e: print(f"Error: {e}") def main(): # Set up the argument parser parser = argparse.ArgumentParser(description="Send birthday messages via email.") parser.add_argument("--email", required=True, help="Recipient's email address") parser.add_argument("--name", required=True, help="Recipient's name") parser.add_argument("--message", required=True, help="Custom birthday message template") parser.add_argument("--smtp-server", required=True, help="SMTP server address (e.g., smtp.gmail.com)") parser.add_argument("--smtp-port", type=int, default=587, help="SMTP server port (default: 587)") parser.add_argument("--sender-email", required=True, help="Your email address") parser.add_argument("--sender-password", required=True, help="Your email password or app password") args = parser.parse_args() # Send the birthday message send_birthday_message( args.email, args.name, args.message, args.smtp_server, args.smtp_port, args.sender_email, args.sender_password ) if __name__ == "__main__": main()

Step 3: Usage Example

To run this script from the command line, you’d use the following format:

bash
python send_birthday_message.py --email "recipient@example.com" --name "John Doe" --message "Happy Birthday, {{ name }}! Wishing you a fantastic year ahead." --smtp-server "smtp.gmail.com" --smtp-port 587 --sender-email "youremail@gmail.com" --sender-password "yourpassword"

Explanation:

  1. Command-Line Arguments: The script uses argparse to handle user input for various command-line arguments (email, name, message, SMTP server settings, etc.).

  2. Jinja2 Template: The message passed in --message is processed with Jinja2 for templating. For example, {{ name }} will be replaced with the recipient’s name.

  3. Email Sending: The smtplib.SMTP class is used to connect to an SMTP server, log in with the sender’s credentials, and send the email. For Gmail, you would use "smtp.gmail.com" and port 587.

  4. Security: Make sure you use an app-specific password for your email account if 2-factor authentication is enabled, and avoid hardcoding passwords directly into your script.

Step 4: Set up SMTP for Gmail (If Using Gmail)

  1. Enable Less Secure Apps: For a Gmail account, you may need to enable “Less secure apps” (though this is not recommended for long-term use).

  2. Use App Passwords: If you have two-factor authentication enabled, you’ll need to create an app-specific password via Google’s security settings.

Step 5: Customize

  • You can add more arguments to customize your script further, like setting up different templates, adding attachments, or even scheduling the script to run on specific dates.

Let me know if you’d like to add more features to this CLI or need help with the configuration!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About