0% found this document useful (0 votes)
7 views3 pages

A

This document provides a step-by-step guide on sending emails with attachments in Python, specifically for sending one email per recipient using a Gmail account. It includes requirements, code snippets for setting up email details, composing the email, and sending it to multiple recipients, along with common errors and their fixes. Bonus tips are also provided for managing recipient lists and avoiding Gmail rate limits.

Uploaded by

ashishsingh.web3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

A

This document provides a step-by-step guide on sending emails with attachments in Python, specifically for sending one email per recipient using a Gmail account. It includes requirements, code snippets for setting up email details, composing the email, and sending it to multiple recipients, along with common errors and their fixes. Bonus tips are also provided for managing recipient lists and avoiding Gmail rate limits.

Uploaded by

ashishsingh.web3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sure!

Here's a concise **class notes-style document** on how to send emails with


attachments using Python, specifically sending one email per recipient:

---

## 📘 Class Notes: Sending Emails with Attachments in Python (One-by-One)

### 🧰 Requirements

* Python 3.x
* `smtplib` and `email` modules (built-in)
* Gmail account with **App Password** (required if 2FA is enabled)

---

### ✅ Step-by-Step Guide

#### 1. **Enable App Passwords in Gmail**

If 2-Step Verification is ON:

* Visit: [https://myaccount.google.com/apppasswords](https://myaccount.google.com/
apppasswords)
* Choose "Mail" and "This device"
* Generate and copy the 16-character App Password

---

#### 2. **Script Overview**

```python
import smtplib
from email.message import EmailMessage
import os
```

* `smtplib`: Sends the email


* `email.message`: Builds email with attachment
* `os`: For file handling

---

#### 3. **Setup Email Details**

```python
EMAIL_ADDRESS = "[email protected]"
EMAIL_PASSWORD = "your_app_password"

recipients = [
"[email protected]",
"[email protected]",
"[email protected]",
]
```

* Replace `EMAIL_ADDRESS` with your Gmail


* Replace `EMAIL_PASSWORD` with your 16-digit app password
* Update the `recipients` list
---

#### 4. **Compose Email Body & Subject**

```python
subject = "Exploring Opportunities – Backend Software Engineer"
body = """
I wanted to reach out to discuss potentially working together.
I’m a Backend software engineer working in LendingKart and currently seeking new
opportunities.
PFA my resume for your reference
LinkedIn: https://www.linkedin.com/in/ashish-singh-39127b288

Thanks and Regards


Ashish Singh
"""
resume_file = "Ashish_Resume.pdf"
```

---

#### 5. **Send Email to Each Recipient**

```python
for recipient in recipients:
try:
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = EMAIL_ADDRESS
msg['To'] = recipient
msg.set_content(body)

# Add attachment
with open(resume_file, 'rb') as f:
file_data = f.read()
msg.add_attachment(file_data, maintype='application', subtype='pdf',
filename=os.path.basename(resume_file))

# Send
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)

print(f"Email sent to {recipient}")

except Exception as e:
print(f"Failed to send to {recipient}: {e}")
```

---

### ⚠️ Common Errors

| Error | Meaning
| Fix |
| ---------------------------------------------- |
--------------------------------------- |
--------------------------------------------------------------------- |
| `535 5.7.8 Username and Password not accepted` | Wrong password or App Password
not used | Use [App Password](https://support.google.com/mail/?p=BadCredentials) |
| `ConnectionRefusedError` | Gmail blocking login
| Use port `465` with `SMTP_SSL` |

---

### 📝 Bonus Tips

* Keep a `.txt` or `.csv` file for recipient emails and load them dynamically
* Add a sleep (`time.sleep(1)`) to avoid hitting Gmail rate limits

---

Let me know if you want this exported as a PDF or markdown file!

You might also like