Skip to main content

A simple email sender package with SMTP

Project description

SMTP-Email-Sender

License: MIT Python Version

SMTP-Email-Sender is a lightweight Python module that simplifies sending emails via SMTP. It supports plain text and HTML content, file attachments with proper RFC2231 encoding, multiple recipients (including CC), and even batch email sending using templates. Its built-in debug mode and detailed logging make testing and troubleshooting a breeze!

Features

  • Simple Email Composition: Create emails with plain text or HTML content.
  • File Attachments: Easily attach files with correct encoding for diverse file names.
  • Multiple Recipients & CC: Send emails to multiple recipients and include CC lists.
  • Template-Based Batch Emails: Use dynamic placeholders to personalize bulk emails.
  • Debug Mode: Log email content and recipients without sending, perfect for testing.
  • Robust Error Handling: Clear, descriptive error messages and logging for easier debugging.

Requirements

  • Python: 3.6+
  • Standard Libraries: smtplib, email, logging, typing

No external dependencies are required!

Installation

You can install SMTP-Email-Sender using pip:

pip install smtp-email-sender

Alternatively, you can clone the repository and integrate it into your project:

git clone https://github.com/ErhanTurker01/SMTP-Email-Sender.git

Or, you can copy the source file into your project directory.

Usage

1. Sending a Simple Email

Below is a basic example of sending a plain text email:

from smtp_email_sender import EmailSender, email_text

# Initialize the EmailSender with your SMTP credentials.
sender = EmailSender(
    sender="your_email@example.com",
    password="your_password",
    smtp_server="smtp.example.com",
    smtp_port=587,
    use_tls=True,
    debug=True  # Set to False to actually send the email.
)

# Create and send the email message.
sender.create_message(
    receiver="recipient@example.com",
    subject="Hello from SMTP-Email-Sender"
).attach(
    email_text("This is a test email sent using SMTP-Email-Sender!", "plain")
).sendmail()

# Finalize and close the SMTP connection.
sender.finish()

2. Sending Emails Using a Template

Send personalized emails to multiple recipients with dynamic content:

from smtp_email_sender import EmailSender

# Initialize the EmailSender.
sender = EmailSender(
    sender="your_email@example.com",
    password="your_password",
    smtp_server="smtp.example.com",
    smtp_port=587,
    use_tls=True,
    debug=True  # Toggle debug mode as needed.
)

# Define your email template with placeholders.
template = "Hi {name},\n\nWe are excited to invite you to {event}! See you there."

# Define the recipient data.
mails = ["alice@example.com", "bob@example.com"]
subjects = ["Invitation to Python Meetup", "Invitation to Python Meetup"]
placeholders = [
    {"name": "Alice", "event": "the Python Meetup"},
    {"name": "Bob", "event": "the Python Meetup"}
]

# Optionally, define CC lists and file attachments (here, none).

# Send the templated emails.
sender.sendmail_from_template(
    template=template,
    mails=mails,
    subjects=subjects,
    text_type="plain",
    placeholders=placeholders
)

# Finalize the process.
sender.finish()

3. Attaching Files

Attach files to your email with proper encoding:

from smtp_email_sender import EmailSender, email_file, email_text

# Initialize the EmailSender.
sender = EmailSender(
    sender="your_email@example.com",
    password="your_password",
    smtp_server="smtp.example.com",
    smtp_port=587,
    use_tls=True,
    debug=True
)

# Create a message and attach both text content and a file.
sender.create_message("recipient@example.com", "Monthly Report") \
      .attach(email_text("Please find the monthly report attached.", "plain")) \
      .attach(email_file("path/to/report.pdf", "Monthly_Report.pdf")) \
      .sendmail()

# Close the connection.
sender.finish()

4. Using SMTP-Email-Sender with Gmail

Steps to Use Gmail with SMTP-Email-Sender:

  1. Enable 2-Step Verification:
    Sign in to your Google account and enable 2-Step Verification.

  2. Generate an App Password:
    After enabling 2-Step Verification, go to the App Passwords section. Select the app (e.g., "Mail") and device (e.g., "Other" and name it "SMTP-Email-Sender") and generate an app password. Save this password securely.

  3. Update Your Code:
    Use your full Gmail address as the sender and the generated app password in your code. Here's an example:

from smtp_email_sender import EmailSender, email_text

# Replace these values with your Gmail details.
gmail_address = "your_gmail@gmail.com"
app_password = "your_generated_app_password"

# Initialize the EmailSender using Gmail's SMTP server.
sender = EmailSender(
    sender=gmail_address,
    password=app_password,
    smtp_server="smtp.gmail.com",
    smtp_port=587,
    use_tls=True,
    debug=True  # Set to False to send the email for real.
)

# Create and send the email message.
sender.create_message(
    receiver="recipient@example.com",
    subject="Hello from SMTP-Email-Sender via Gmail"
).attach(
    email_text("This email was sent using Gmail's SMTP server and an App Password.", "plain")
).sendmail()

# Finalize and close the SMTP connection.
sender.finish()

Follow these steps to securely send emails via Gmail using SMTP-Email-Sender.

Logging & Debugging

SMTP-Email-Sender uses Python's built-in logging module to provide detailed feedback. When in debug mode (debug=True), the module logs the recipients, email content, and any errors without actually sending emails—ideal for development and testing.

Check your console or log files to see these detailed messages, which can help diagnose issues with SMTP connection, authentication, or file attachments.

Contributing

Contributions are welcome! If you have suggestions or improvements, please:

  • Fork the repository.
  • Create a new branch (git checkout -b feature/YourFeature).
  • Commit your changes (git commit -m 'Add some feature').
  • Push to the branch (git push origin feature/YourFeature).
  • Open a pull request.

Feel free to open issues for bug reports or feature requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Happy emailing with SMTP-Email-Sender!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smtp_email_sender-1.0.4.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smtp_email_sender-1.0.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file smtp_email_sender-1.0.4.tar.gz.

File metadata

  • Download URL: smtp_email_sender-1.0.4.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for smtp_email_sender-1.0.4.tar.gz
Algorithm Hash digest
SHA256 4634916e73539b1fe28ae9cdf2a79f1dc8dffa7a2043723977ce9a6c27e9ebda
MD5 9ee14ebb10763ccc728564fa91b7286f
BLAKE2b-256 91d4c87c27e9329b5c0587329efed6b57ecbbea1ba52990b414e1533e7b7c2fc

See more details on using hashes here.

File details

Details for the file smtp_email_sender-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for smtp_email_sender-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ec1c8e02ad9b5519d037867b2d81249d4264bf772d154cdaa3226cdff5a6092b
MD5 9e707889917e5068cc886092907fd19e
BLAKE2b-256 c579d4fa47d81df739f126259adeda0ff1ea4553b854994db5dc055ed915e788

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page