Skip to content

NafTasmi/Attachment_Send

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Email with Attachment Sender

A Python script to send emails with file attachments via Gmail's SMTP server.

✨ Features

  • Send emails with file attachments (PDF, images, documents, etc.)
  • Secure TLS connection using port 587
  • Clean error handling for file operations and email sending
  • Professional email formatting with body text
  • Support for various file types

📋 Requirements

  • Python 3.x
  • Gmail account with App Password enabled

🔧 Setup

1. Gmail Configuration

  1. Enable 2-Factor Authentication on your Google Account
  2. Generate an App Password:

2. Script Configuration

Edit these variables in the script:

sender_email = "your_email@gmail.com"
receiver_email = "recipient@example.com"
password = "your_app_password_here"
file_path = "path/to/your/file.pdf"  # Update with actual file path

🚀 Usage

Basic Usage

python email_attachment_sender.py

Supported File Types

  • PDF documents (.pdf)
  • Images (.jpg, .png, .gif)
  • Documents (.docx, .txt, .xlsx)
  • Any file readable in binary mode

📁 File Path Examples

# Windows
file_path = "C:\\Users\\Name\\Documents\\file.pdf"
file_path = r"D:\Files\document.pdf"

# Mac/Linux
file_path = "/Users/name/Documents/file.pdf"
file_path = "~/Documents/image.jpg"

⚠️ Important Security Notes

🔒 CRITICAL: Protect Your Credentials

# ❌ NEVER do this in production:
password = "your_password_here"  # Hardcoded password

# ✅ Recommended alternatives:
# 1. Use environment variables
import os
password = os.getenv("GMAIL_APP_PASSWORD")

# 2. Use input() for local testing
password = input("Enter app password: ")

# 3. Use configuration files (.env)

Before Sharing/Committing:

  1. Remove or replace actual email addresses
  2. Replace real passwords with placeholders
  3. Use .gitignore to exclude config files

🐛 Common Issues & Solutions

Issue Solution
FileNotFoundError Check file path, use raw strings (r"path") for Windows
Authentication failed Verify App Password is correct, enable 2FA
SMTPAuthenticationError Allow less secure apps (temporarily) or use App Password
File attachment fails Check file permissions, ensure file exists
Connection refused Check firewall, try port 465 with SMTP_SSL()

🔧 Alternative: Port 465 (SSL)

If port 587 fails, use this alternative:

# Replace the sending block with:
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message.as_string())

📄 Email Content Customization

Edit these sections to customize:

message["Subject"] = "Your Custom Subject"
body = """Your custom email body text here..."""

📊 Error Handling Features

  • File attachment validation
  • Connection error reporting
  • Authentication feedback
  • Success/failure notifications with emojis

🧪 Testing

  1. Test with a small text file first
  2. Verify the file opens correctly before sending
  3. Send to yourself first for testing

📝 Best Practices

  1. Always use App Passwords, never regular passwords
  2. Store credentials securely (env vars, keyring, etc.)
  3. Validate file paths before attempting to attach
  4. Add size limits for large attachments
  5. Include informative email subjects and body text

🚫 Limitations

  • Gmail has a 25MB attachment limit
  • Rate limiting may apply for multiple emails
  • Some file types may be blocked by recipient's email server

📄 License

Educational use only. Comply with Gmail's Terms of Service and anti-spam regulations.

🔍 Troubleshooting Checklist

  • 2-Factor Authentication enabled
  • App Password generated for "Mail"
  • Correct file path (use os.path.exists() to verify)
  • Internet connection active
  • No special characters in file name causing issues
  • File not open in another program

⚠️ Warning: This script contains hardcoded credentials. Never deploy with real credentials in production environments.

About

This Python script sends an email with a file attachment using Gmail's SMTP server.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages