Categories We Write About

Create a terminal-based email composer

Creating a terminal-based email composer involves writing a simple program that lets the user input email details—like recipient, subject, and body—in the terminal, then composes the email and optionally sends it via an SMTP server.

Below is a Python example of a terminal-based email composer with these features:

  • Input for To, Cc, Bcc, Subject, and Body

  • Supports multi-line body input until a specific keyword (e.g., a single dot . on a line)

  • Option to send the email using SMTP (with configurable SMTP settings)

  • Validation for email fields

python
import smtplib from email.message import EmailMessage import getpass import re def is_valid_email(email): # Basic email validation pattern = r'^[^@]+@[^@]+.[^@]+$' return re.match(pattern, email) is not None def get_email_addresses(prompt): addresses = input(prompt).strip() if not addresses: return [] emails = [email.strip() for email in addresses.split(',')] for email in emails: if not is_valid_email(email): print(f"Invalid email address detected: {email}") return None return emails def main(): print("=== Terminal Email Composer ===") while True: to_emails = get_email_addresses("To (comma-separated emails): ") if to_emails is not None and to_emails: break print("Please enter at least one valid recipient email.") cc_emails = get_email_addresses("Cc (comma-separated emails, optional): ") if cc_emails is None: cc_emails = [] bcc_emails = get_email_addresses("Bcc (comma-separated emails, optional): ") if bcc_emails is None: bcc_emails = [] subject = input("Subject: ").strip() print("Enter email body (end with a single dot '.' on a line):") lines = [] while True: line = input() if line.strip() == ".": break lines.append(line) body = "n".join(lines) print("n--- Email Preview ---") print(f"To: {', '.join(to_emails)}") if cc_emails: print(f"Cc: {', '.join(cc_emails)}") if bcc_emails: print(f"Bcc: {', '.join(bcc_emails)}") print(f"Subject: {subject}") print("n" + body) print("---------------------") send = input("Send email? (y/N): ").strip().lower() if send != 'y': print("Email discarded.") return # SMTP details (modify as needed) smtp_server = input("SMTP server (e.g., smtp.gmail.com): ").strip() smtp_port = input("SMTP port (default 587): ").strip() if not smtp_port: smtp_port = 587 else: smtp_port = int(smtp_port) smtp_user = input("SMTP username (your email): ").strip() smtp_password = getpass.getpass("SMTP password: ") msg = EmailMessage() msg['From'] = smtp_user msg['To'] = ", ".join(to_emails) if cc_emails: msg['Cc'] = ", ".join(cc_emails) if bcc_emails: # Bcc is usually hidden from headers, but needed for sending: all_recipients = to_emails + cc_emails + bcc_emails else: all_recipients = to_emails + cc_emails msg['Subject'] = subject msg.set_content(body) try: with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() server.login(smtp_user, smtp_password) server.sendmail(smtp_user, all_recipients, msg.as_string()) print("Email sent successfully!") except Exception as e: print(f"Failed to send email: {e}") if __name__ == "__main__": main()

How to use:

  1. Save this as email_composer.py.

  2. Run python3 email_composer.py in your terminal.

  3. Input recipients, subject, and the body (end body input with a single . line).

  4. Enter SMTP details (for example, Gmail SMTP server: smtp.gmail.com port 587).

  5. The email will be sent if all details are correct.

Note:

  • For Gmail, you may need to create an app password or enable “less secure apps” (which Google has been disabling) to allow SMTP login.

  • Modify SMTP settings to your email provider’s info as needed.

Would you like me to help create a version that stores drafts or has a richer editor in terminal?

Share This Page:

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

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About